Structured Web Document XML
Structured Web Document XML
<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>
In HTML
<h2>Relationship matter-energy</h2>
<i> E = M × c2 </i>
In XML
<equation>
<meaning>Relationship matter
energy</meaning>
<leftside> E </leftside>
<rightside> M × c2 </rightside>
</equation>
9 Chapter 2 A Semantic Web Primer
HTML vs XML: Different Use of Tags
1. Introduction
2. Detailed Description of XML
3. Structuring
a) DTDs
b) XML Schema
4. Namespaces
5. Accessing, querying XML documents: XPath
6. Transformations: XSLT
12 Chapter 2 A Semantic Web Primer
The XML Language
<lecturer>
<name>David Billington</name>
<phone> +61 − 7 − 3875 507 </phone>
</lecturer>
<order>
<orderNo>23456</orderNo>
<customer>John Smith</customer>
<date>October 15, 2002</date>
<item>
<itemNo>a528</itemNo>
<quantity>1</quantity>
</item>
<item>
<itemNo>c817</itemNo>
<quantity>3</quantity>
</item>
</order>
20 Chapter 2 A Semantic Web Primer
XML Elements vs Attributes
Comments
– A piece of text that is to be ignored by parser
– <!-- This is a comment -->
Processing Instructions (PIs)
– Define procedural attachments
– <?stylesheet type="text/css"
href="mystyle.css"?>
<email>
<head>
<from name=“hameed"
address=“[email protected]"/>
<to name=“shahzaib"
address="[email protected]"/>
<subject>Where is your draft?</subject>
</head>
<body>
shahzaib, where is the draft of the paper you promised me
last week?
</body>
</email>
1. Introduction
2. Detailed Description of XML
3. Structuring
a) DTDs
b) XML Schema
4. Namespaces
5. Accessing, querying XML documents: XPath
6. Transformations: XSLT
27 Chapter 2 A Semantic Web Primer
Structuring XML Documents
<lecturer>
<name>David Billington</name>
<phone> +61 − 7 − 3875 507 </phone>
</lecturer>
DTD for above element (and all lecturer elements):
<!ELEMENT lecturer (name,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<order orderNo="23456"
customer="John Smith"
date="October 15, 2002">
<item itemNo="a528" quantity="1"/>
<item itemNo="c817" quantity="3"/>
</order>
#REQUIRED
– Attribute must appear in every occurrence of the element
type in the XML document
#IMPLIED
– The appearance of the attribute is optional
#FIXED "value"
– Every element must have this attribute
"value"
– This specifies the default value for the attribute
<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>
40 Chapter 2 A Semantic Web Primer
A DTD for an Email Element
<!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>
42 Chapter 2 A Semantic Web Primer
Interesting Parts of the DTD
1. Introduction
2. Detailed Description of XML
3. Structuring
a) DTDs
b) XML Schema
4. Namespaces
5. Accessing, querying XML documents: XPath
6. Transformations: XSLT
46 Chapter 2 A Semantic Web Primer
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
<complexType name="lecturerType">
<sequence>
<element name="firstname" type="string"
minOccurs="0“ maxOccurs="unbounded"/>
<element name="lastname" type="string"/>
</sequence>
<attribute name="title" type="string"
use="optional"/>
</complexType>
<complexType name="extendedLecturerType">
<sequence>
<element name="firstname" type="string"
minOccurs="0" maxOccurs="unbounded"/>
<element name="lastname" type="string"/>
<element name="email" type="string"
minOccurs="0" maxOccurs="1"/>
</sequence>
<attribute name="title" type="string" use="optional"/>
<attribute name="rank" type="string" use="required"/>
</complexType>
<complexType name="restrictedLecturerType">
<restriction base="lecturerType">
<sequence>
<element name="firstname" type="string"
minOccurs="1" maxOccurs="2"/>
</sequence>
<attribute name="title" type="string"
use="required"/>
</restriction>
</complexType>
<simpleType name="dayOfMonth">
<restriction base="integer">
<minInclusive value="1"/>
<maxInclusive value="31"/>
</restriction>
</simpleType>
<simpleType name="dayOfWeek">
<restriction base="string">
<enumeration value="Mon"/>
<enumeration value="Tue"/>
<enumeration value="Wed"/>
<enumeration value="Thu"/>
<enumeration value="Fri"/>
<enumeration value="Sat"/>
<enumeration value="Sun"/>
</restriction>
</simpleType>
<complexType name="emailType">
<sequence>
<element name="head" type="headType"/>
<element name="body" type="bodyType"/>
</sequence>
</complexType>
<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>
<complexType name="nameAddress">
<attribute name="name" type="string"
use="optional"/>
<attribute name="address"
type="string" use="required"/>
</complexType>
1. Introduction
2. Detailed Description of XML
3. Structuring
a) DTDs
b) XML Schema
4. Namespaces
5. Accessing, querying XML documents: XPath
6. Transformations: XSLT
64 Chapter 2 A Semantic Web Primer
Namespaces
<gu:academicStaff gu:title="lecturer"
gu:name="Mate Jones"
gu:school="Information Technology"/>
</vu:instructors>
1. Introduction
2. Detailed Description of XML
3. Structuring
a) DTDs
b) XML Schema
4. Namespaces
5. Accessing, querying XML documents: XPath
6. Transformations: XSLT
68 Chapter 2 A Semantic Web Primer
Addressing and Querying XML
Documents
1. Introduction
2. Detailed Description of XML
3. Structuring
a) DTDs
b) XML Schema
4. Namespaces
5. Accessing, querying XML documents: XPath
6. Transformations: XSLT
102 Chapter 2 A Semantic Web Primer
Displaying XML Documents
<author>
<name>Grigoris Antoniou</name>
<affiliation>University of Bremen</affiliation>
<email>[email protected]</email>
</author>
may be displayed in different ways:
Grigoris Antoniou Grigoris Antoniou
University of Bremen University of Bremen
[email protected] [email protected]
103 Chapter 2 A Semantic Web Primer
Style Sheets
<xsl:template match="/author">
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b><xsl:value-of select="name"/></b><br>
<xsl:value-of select="affiliation"/><br>
<i><xsl:value-of select="email"/></i>
</body>
</html>
</xsl:template>
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b>Grigoris Antoniou</b><br>
University of Bremen<br>
<i>[email protected]</i>
</body>
</html>
<html>
<head><title>An author</title></head>
<body bgcolor="white">
<b>...</b><br>
...<br>
<i>...</i>
</body>
</html>
<xsl:template match="/">
<html>
<head><title>Authors</title></head>
<body bgcolor="white">
<xsl:apply-templates select="authors"/>
<!-- Apply templates for AUTHORS
children -->
</body>
</html>
</xsl:template>
<xsl:template match="authors">
<xsl:apply-templates select="author"/>
</xsl:template>
<xsl:template match="author">
<h2><xsl:value-of select="name"/></h2>
Affiliation:<xsl:value-of
select="affiliation"/><br>
Email: <xsl:value-of select="email"/>
<p>
</xsl:template>
114 Chapter 2 A Semantic Web Primer
Multiple Authors Output
<html>
<head><title>Authors</title></head>
<body bgcolor="white">
<h2>Grigoris Antoniou</h2>
Affiliation: University of Bremen<br>
Email: [email protected]
<p>
<h2>David Billington</h2>
Affiliation: Griffith University<br>
Email: [email protected]
<p>
</body>
</html>
Wrong solution:
<xsl:template match="person">
<person firstname="<xsl:value-of
select="@firstname">"
lastname="<xsl:value-of select="@lastname">"/>
</xsl:template>
<xsl:template match="/">
<?xml version="1.0" encoding="UTF-16"?>
<authors>
<xsl:apply-templates select="authors"/>
</authors>
</xsl:template>
<xsl:template match="authors">
<author>
<xsl:apply-templates select="author"/>
</author>
</xsl:template>
<xsl:template match="author">
<name><xsl:value-of select="name"/></name>
<contact>
<institution>
<xsl:value-of select="affiliation"/>
</institution>
<email><xsl:value-of select="email"/></email>
</contact>
</xsl:template>