Module 8 (XML)
Module 8 (XML)
APPLICATIONS
MODULE 7 – XML
<note date="12/11/2007">
<to>Tove</to>
<from>Jani</from>
</note>
Entity References
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will generate
an error because the parser interprets it as the start of a new element.
2.DTD (Schema)
<!ELEMENT note (to, from, heading, body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
XML information structures (1)
Example 1: A possible book structure
Book
FrontMatter
BookTitle
Author(s)
PubInfo
Chapter(s)
ChapterTitle
Paragraph(s)
BackMatter
References
Index
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML information structures (2)
▪ Premise: A text is the sum of its component
parts
➢ A <Book> could be defined as containing:
<FrontMatter>, <Chapter>s, <BackMatter>
➢ <FrontMatter> could contain:
<BookTitle> <Author>s <PubInfo>
➢ A <Chapter> could contain:
<ChapterTitle> <Paragraph>s
➢ A <Paragraph> could contain:
<Sentence>s or <Table>s or <Figure>s …
▪ Components chosen for book markup language
should reflect anticipated use ….
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML information structures (3)
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML Syntax: Elements & Attributes
• Uses less-than and greater-than characters (<…>) as
delimiters
• Every opening tag must having an accompanying closing
tag
– <First Name>Sanjay</First Name>
– Empty tags do not require an accompanying closing tag.
– Empty tags have a forward slash before the greater-than sign e.g.
<Name/>
• Tags can have attributes which must be enclosed in double
quotes
– <name first=“Sanjay” last=“Goel”)
• Elements should be properly nested
– The nesting can not be interleaved
– Each document must have one single root element
• Elements and attribute names are case sensitive
Tree Structure - Elements
• XML documents have a tree structure containing multiple levels of nested
tags.
– Root element is a single XML element which encloses all of the other
XML elements and data in the document
– All other elements are children of the root element
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
Comments
• XML comments begin with “<!--”and end with “-->”
– All data between these delimiters is discarded
– <!-- This is a list of names of people -->
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
XML Namespaces provide a method to avoid element name conflicts.
▪ <h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
http://www.webster.ch/ - http://tecfa.unige.ch
http://www.webster.ch/ http://tecfa.unige.ch
Display XML
Style Sheets
• <!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
The DTD above is interpreted like this:
•!DOCTYPE note - Defines that the root element of
the document is note
•!ELEMENT note - Defines that the note element
must contain the elements: "to, from, heading,
body"
•!ELEMENT to - Defines the to element to be of type
"#PCDATA"
•!ELEMENT from - Defines the from element to be
of type "#PCDATA"
•!ELEMENT heading - Defines the heading element
to be of type "#PCDATA"
•!ELEMENT body - Defines the body element to be
of type "#PCDATA"
XML Schema
• An XML Schema describes the structure of an
XML document, just like a DTD.
• An XML document with correct syntax is
called "Well Formed".
• An XML document validated against an XML
Schema is both "Well Formed" and "Valid".
XML Schema
• XML Schema is an XML-based alternative to
DTD:
• <xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
XML Schema
• The Schema above is interpreted like this:
• <xs:element name="note"> defines the element called
"note"
• <xs:complexType> the "note" element is a complex type
• <xs:sequence> the complex type is a sequence of
elements
• <xs:element name="to" type="xs:string"> the element
"to" is of type string (text)
• <xs:element name="from" type="xs:string"> the
element "from" is of type string
• <xs:element name="heading" type="xs:string"> the
element "heading" is of type string
• <xs:element name="body" type="xs:string"> the
element "body" is of type string
XML Schema Data types
• There are two types of data types in XML schema.
• simpleType
– The simpleType allows you to have text-based
elements. It contains less attributes, child elements,
and cannot be left empty.
• complexType
– The complexType allows you to hold multiple
attributes and elements. It can contain additional sub
elements and can be left empty.
Key Concepts in XML Schemas
(XSD):
<xs:choice>
<xs:element name="phone" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
</xs:choice>
Sample XML Schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person" type="personType"/>
<xs:complexType name="personType">
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>
<xs:element name="gender" type="genderType"/>
</xs:sequence>
<xs:attribute name="age" type="xs:int"/>
</xs:complexType>
<xs:simpleType name="genderType">
<xs:restriction base="xs:string">
<xs:enumeration value="Male"/>
<xs:enumeration value="Female"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
Introduction to XSLT
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The <xsl:for-each> Element
The corresponding xml code is :
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
XSLT <xsl:sort> Element
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:sort select="artist"/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
</catalog>
XSLT <xsl:sort> Element
The corresponding xml code is :
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>