FIGIS’ML Hands-on training - © FAO/FIGIS 2001 1
An introduction to XML
• Objectives :– what is XML?– XML and HTML– XML documents
• structure• well-formedness
– Validation of XML documents :• the DTDs• creation of a DTD• the schemas
– creating XML documents
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 2
An introduction to XML
• What is XML? :– XML stands for eXtensible Markup Language
• XML is similar to HTML but– HTML deals with presentation– XML deals with content and structure
• XML is extensible : you can create your own tags
– XML is an open standard managed by the World Wide Web Consortium (W3C)
• http://www.w3c.org
– XML is text-based
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 3
An introduction to XML
• What is XML? : an example
<?xml version="1.0" encoding="iso-8859-1"?><ORDER orderref="120136">
<ITEM type="CD" quantity="1"><AVAILABLE STATUS=“yes” TIME=“2days”/><TITLE>Groovy beats</TITLE><AUTHOR>Howie C.</AUTHOR><EDITOR>Average Records</EDITOR>
</ITEM><ITEM type="book" quantity="2">
<AVAILABLE STATUS=“yes” TIME=“1week”/> <TITLE>Piano for Dummies - vol1</TITLE><AUTHOR> K. Board</AUTHOR><EDITOR>Dubious & P. Ano</EDITOR>
</ITEM></ORDER>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 4
An introduction to XML
• XML and HTML– HTML :
• pros : widely used, simple to use;• cons : mixes data and formatting, non-extensible
– XML :• handles only the content - formatting is done by
stylesheets• the tags are defined by the authors• tags definition and structure is handled by dictionnaries
(DTD)
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 5
An introduction to XML
• XML and HTML : examples– HTML
– XML<?xml version="1.0" encoding="iso-8859-1"?><ORDER orderref="120136">
<ITEM type="CD" quantity="1"><AVAILABLE STATUS=“yes” TIME=“2days”/><TITLE>Groovy beats</TITLE><AUTHOR>Howie C.</AUTHOR><EDITOR>Average Records</EDITOR>
</ITEM></ORDER>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><body>
<table width="200" border="1"><tr><td><B>Groovy beats</B></td></tr><tr><td>Howie C.</td></tr><tr><td>Average Records</td></tr>
</table></body></html>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 6
An introduction to XML
• an XML document
<?xml version="1.0" encoding="iso-8859-1"?><ORDER orderref="120136">
<ITEM type="CD" quantity="1"><AVAILABLE status="yes" time="2days"/><TITLE>Groovy beats</TITLE><AUTHOR>Howie C.</AUTHOR><EDITOR>Average Records</EDITOR>
</ITEM><ITEM type="book" quantity="2">
<AVAILABLE status="yes" time="1week"/><TITLE>Piano for Dummies - vol1</TITLE><AUTHOR>K. Board</AUTHOR><EDITOR>Dubious & P. Ano</EDITOR>
</ITEM></ORDER>
declaration
element
entity
attribute
root
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 7
An introduction to XML
• an XML document– Elements
• they define the document’s content• they can contain other elements or text• they can be empty
– Attributes• add information about one element• one attribute can only appear once in one element• attributes can only contain text
– Entities• can be used as “shortcuts”• are similar to variables
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 8
An introduction to XML
• an XML document– Declaration
• always the first line of an XML document• can be omitted (not recommended)• always like : <?xml version="1.0”>• can contain language-related info :
<?xml version="1.0" encoding="iso-8859-1"?>
– Root (element)• mandatory• always unique
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 9
An introduction to XML
• Creating well-formed XML documents– well-formed means that the document
respects the XML rules– the main rules are :
• there must be a root• there must be at least one element inside the root• all elements must be properly imbricated and closed• elements, attributes and entities are case-sensitive• attribute values have to be quoted
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 10
An introduction to XML
• Valid XML documents– an XML document is valid when :
• it follows the structure that has been defined• all the elements respect the defined rules
– valid elements– valid attributes– optional and mandatory elements/attributes
– the validation rules are defined in a “dictionnary” :
• a Document Type Definition (DTD)• a schema (still a proposal)
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 11
An introduction to XML
• Valid XML documents– well-formed doesn’t imply valid
• well-formed means syntaxically correct• valid is similar to grammatically correct
– but a valid document is always well-formed
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 12
An introduction to XML
• Valid XML documents : DTD– Creating a DTD : sample XML
<?xml version="1.0" encoding="iso-8859-1"?><ORDER orderref="120136">
<ITEM type="CD" quantity="1"><AVAILABLE status="yes" time="2days"/><TITLE>Groovy beats</TITLE><AUTHOR>Howie C.</AUTHOR><EDITOR>Average Records</EDITOR>
</ITEM><ITEM type="book" quantity="2">
<AVAILABLE status="yes" time="1week"/><TITLE>Piano for Dummies - vol1</TITLE><AUTHOR>K. Board</AUTHOR><EDITOR>Dubious & P. Ano </EDITOR>
</ITEM></ORDER>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 13
An introduction to XML
• Valid XML documents : DTD– Creating a DTD :
• insert the declaration<?xml version="1.0" encoding="iso-8859-1"?>
• identify the root elementORDER
• identify its direct childrenITEM with TYPE and Quantity as attributes
• define children grouping and repetitionany number of ITEM elements
• repeat these steps for each level of elementsITEM contains one mandatory occurrence of AVAILABLE, TITLE, AUTHOR and
EDITOR
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 14
An introduction to XML
• Valid XML documents : DTD– Creating a DTD :
• define root element :<!ELEMENT ORDER
• define its direct children and their order/number:<!ELEMENT ORDER (ITEM+)>
• repeat those steps for each element :<!ELEMENT ITEM (AVAILABLE,TITLE,AUTHOR,EDITOR)>
• define the attributes of each element :<!ATTLIST ORDER orderref CDATA #REQUIRED>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 15
An introduction to XML
• Valid XML documents : DTD– Creating a DTD : grouping an repeating
elements• specifiying the number of elements :
– 0 or 1 : <!ELEMENT ORDER(ITEM?)>– 1 or more : <!ELEMENT ORDER(ITEM+)>– 0 or more : <!ELEMENT ORDER(ITEM*)>– 1 exactly : <!ELEMENT ORDER(ITEM)>
• specifying the order of elements :– any order : <!ELEMENT ITEM(AUTHOR | TITLE)>– specified order : <!ELEMENT ITEM(TITLE,AUTHOR)>
• and any combination of those :<!ELEMENT HISTORY(ORDER*,DATE?,(CUSTOMER+ | COMPANY+))>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 16
An introduction to XML
• Valid XML documents : DTD– Creating a DTD : elements content
• elements can be empty :– example : <AVAILABLE status="yes" time="2days"/>– they should be declared using EMPTY :
<!ELEMENT AVAILABLE EMPTY>
• elements can contain other elements :<ORDER orderref="120136">
<ITEM type="CD" quantity="1">...
</ORDER>
• elements can contain text :– example : <TITLE>Groovy beats</TITLE>– they should be declared using (#PCDATA):
<!ELEMENT TITLE (#PCDATA)>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 17
An introduction to XML
• Valid XML documents : DTD– Creating a DTD : adding attributes
• using ATTLIST :<!ATTLIST ITEM
TYPE (CD | book) “CD”
QUANTITY CDATA #REQUIRED
>
• this defines :– two attributes of the element ITEM– gives the list of possible TYPE values (“CD”/“book”)– defines “CD” as the default value for TYPE– defines QUANTITY as a mandatory attribute
• there are three attribute types :– #REQUIRED : a value is mandatory– #IMPLIED : a value is optional– #FIXED : the attribute always has the same value
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 18
An introduction to XML
• Valid XML documents : DTD– Creating a DTD : example
<?xml version="1.0" encoding="iso-8859-1"?><ORDER orderref="120136">
<ITEM type="CD" quantity="1"><AVAILABLE status="yes" time="2days"/><TITLE>Groovy beats</TITLE><AUTHOR>Howie C.</AUTHOR><EDITOR>Average Records</EDITOR>
</ITEM><ITEM type="book" quantity="2">
<AVAILABLE status="yes" time="1week"/><TITLE>Piano for Dummies - vol1</TITLE><AUTHOR>K. Board</AUTHOR><EDITOR>Dubious & P. Ano </EDITOR>
</ITEM></ORDER>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 19
An introduction to XML
• Valid XML documents : DTD– Creating a DTD : example
• hands-on : creating a DTD with XMLSpy• this is the DTD defined for the sample XMl document :
<?xml version="1.0" encoding="iso-8859-1"?>
<!ELEMENT ORDER (ITEM+)>
<!ATTLIST ORDER
orderref CDATA #REQUIRED
>
<!ELEMENT ITEM (AVAILABLE, TITLE, AUTHOR, EDITOR)>
<!ATTLIST ITEM
TYPE (CD | book) "CD"
QUANTITY CDATA #REQUIRED
>
<!ELEMENT AVAILABLE EMPTY>
<!ATTLIST AVAILABLE
TIME (2days | 1week | normal) #IMPLIED
STATUS (YES | NO) #REQUIRED
>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT AUTHOR (#PCDATA)>
<!ELEMENT EDITOR (#PCDATA)>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 20
An introduction to XML
• Valid XML documents : schemas– schemas are still a proposal– schemas are written in XML
• they don’t require learning a new language• they can be handled and transformed as normal XML
documents
– schemas can contain more complex rules that DTDs
• conditions• tests• ...
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 21
An introduction to XML
• creating XML documents– XML is pure text– XML documents can be created manually– XML editing softwares make the task easier
• check for well-formedness and validity• provide a graphical user interface
– Various solutions exist :• XMLSpy by Altova (Windows)• Xmetal by SoftQuad(Windows)• Morphon XML Editor Suite by Morphon (MacOS)• ElfData XML Editor by ElfData (MacOS)
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 22
An introduction to XML
• Creating XML documents :– hands-on : creating a document with XMLSpy
• example of an order form :– 1 order : reference 120136– two items in the order :
» 1 CD, “Groovy beats” by Howie C., ed. “Average Records”, available with 2-days delivery
» 2 copies of the book “Piano for dummies - vol1” by K. Board, published by “Dubious & P. Ano”, available with 1-week delivery
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 23
An introduction to XML
• Creating XML documents :– hands-on : creating a document with XMLSpy
<?xml version="1.0" encoding="iso-8859-1"?><ORDER orderref="120136">
<ITEM type="CD" quantity="1"><AVAILABLE status="yes" time="2days"/><TITLE>Groovy beats</TITLE><AUTHOR>Howie C.</AUTHOR><EDITOR>Average Records</EDITOR>
</ITEM><ITEM type="book" quantity="2">
<AVAILABLE status="yes" time="1week"/><TITLE>Piano for Dummies - vol1</TITLE><AUTHOR>K. Board</AUTHOR><EDITOR>Dubious & P. Ano</EDITOR>
</ITEM></ORDER>
FIGIS’ML Hands-on training - © FAO/FIGIS 2001 24
An introduction to XML
• using XML documents– exchanging data between systems
• from a DB to another DB• data manipulation by softwares
– submitting data to a system• automatically (software)• manually (creation of content)
– presenting data to the user• web pages• data export in any format (text, pdf, etc.)