03a XML
03a XML
Documents
in XML
(a)
<book>
<title>Nonmonotonic Reasoning: Context-Dependent
Reasoning</title>
<author>V. Marek</author>
<author>M. Truszczynski</author>
<publisher>Springer</publisher>
<year>1993</year>
<ISBN>0387976892</ISBN>
</book>
HTML versus XML: Similarities
In XML
<equation>
<gloss>Relationship matter energy </gloss>
<leftside> E </leftside>
<rightside> M × c^2 </rightside>
</equation>
HTML vs. XML: Different Use of Tags
l If
there is no content, then element is called
empty; it can be abbreviated as follows:
<lecturer/> = <lecturer></lecturer>
XML Attributes
<lecturer
name="David Billington"
phone="+61 − 7 − 3875 507" />
XML Attributes: An Example
<order orderNo="23456“
customer="John Smith"
date="October 15, 2017" >
<item itemNo="a528" quantity="1" />
<item itemNo="c817" quantity="3" />
</order>
The Same Example without Attributes
<order>
<orderNo>23456</orderNo>
<customer>John Smith</customer>
<date>October 15, 2017</date>
<item>
<itemNo>a528</itemNo>
<quantity>1</quantity>
</item>
<item>
<itemNo>c817</itemNo>
<quantity>3</quantity>
</item>
</order>
XML Elements vs. Attributes
l Comments
–A piece of text that is to be ignored by parser
<!-- This is a comment -->
<order orderNo="23456"
customer="John Smith"
date="October 15, 2017">
<item itemNo="a528" quantity="1” />
<item itemNo="c817" quantity="3” />
</order>
Corresponding DTD
<!ELEMENT order (item+)>
<!ATTLIST order
orderNo ID #REQUIRED
customer CDATA #REQUIRED
date CDATA #REQUIRED >
<family>
<person id="bob" mother="mary" father="peter">
<name>Bob Marley</name>
</person>
<person id="bridget" mother="mary">
<name>Bridget Jones</name>
</person>
<person id="mary" children="bob bridget">
<name>Mary Poppins</name>
</person>
<person id="peter" children="bob">
<name>Peter Marley</name>
</person>
</family>
Email Element DTD 1/2
<!ELEMENT email (head,body)>
<!ELEMENT head (from,to+,cc*,subject)>
<!ELEMENT from EMPTY>
<!ATTLIST from
name CDATA #IMPLIED
address CDATA #REQUIRED>
<!ELEMENT to EMPTY>
<!ATTLIST to
name CDATA #IMPLIED
address CDATA #REQUIRED>
Email Element DTD 2/2
<!ELEMENT cc EMPTY>
<!ATTLIST cc
name CDATA #IMPLIED
address CDATA #REQUIRED>
<!ELEMENT subject (#PCDATA) >
<!ELEMENT body (text,attachment*) >
<!ELEMENT text (#PCDATA) >
<!ELEMENT attachment EMPTY >
<!ATTLIST attachment
encoding (mime|binhex) "mime"
file CDATA #REQUIRED>
Outline
(1) Introduction
(2) Description of XML
(3) Structuring
– DTDs
– XML Schema
(4) Namespaces
(5) Accessing, querying XML documents: XPath
(6) Transformations: XSLT
XML Schema (XSD)
l XML Schema is a significantly richer language
for defining the structure of XML documents
l Syntax based on XML itself, so separate tools
to handle them not needed
l Reuse and refinement of schemas => can
expand or delete existing schemas
l Sophisticated set of data types, compared to
DTDs, which only supports strings
l XML Schema recommendation published by
W3C in 2001, version 1.1 in 2012
XML Schema
<element name="email"/>
<element name="head“
minOccurs="1“
maxOccurs="1"/>
<element name="to" minOccurs="1"/>
Cardinality constraints:
– minOccurs="x" (default value 1)
– maxOccurs="x" (default value 1)
– Generalizations of *,?,+ offered by DTDs
Attribute Types
<complexType name="emailType">
<sequence>
<element name="head" type="headType"/>
<element name="body" type="bodyType"/>
</sequence>
</complexType>
XML Schema: The Email Example
<complexType name="headType">
<sequence>
<element name="from" type="nameAddress"/>
<element name="to" type="nameAddress"
minOccurs="1" maxOccurs="unbounded"/>
<element name="cc" type="nameAddress"
minOccurs="0" maxOccurs="unbounded"/>
<element name="subject" type="string"/>
</sequence>
</complexType>
XML Schema: The Email Example
<complexType name="nameAddress">
<attribute name="name" type="string"
use="optional"/>
<attribute name="address"
type="string" use="required"/>
</complexType>