You are on page 1of 46

Module#5

XML
Prof. Ashish Revar

eXtensible Markup Language (XML)


Document Type Definition (DTD)
XML Schema (XS)
eXtensible Stylesheet Language (XSL)
XSL Transformations (XSLT)

eXtensible Markup Language


Markup language much like HTML, but not a
replacement for HTML
XML tags are not predefined. You must define
your own tags
Primary use of XML is the description of data
rather than presentation
W3C Recommendation

Used to transport and store data


Both human and machine can read
Useful in swapping data between different
applications
Self-descriptive

<?xml version="1.0"?>
<book>
<title>Developing Web
Application</title>
<author>Ralph Moseley</author>
<price>500</price>
</book>

<bookstore>
<book isbn=>
<title></title>
<author></author>
<price></price>
</book>
<book>
</book>

<bookstore>

Elements : <price>500</price>
Attributes : <book isbn=2960-5>
Entities : <, >, &
PCDATA : parsed character data

CDATA : character data

Tags inside the text will be treated as markup


Tags inside the text will NOT be treated as markup

Comments : <!-- comment text -->

Elements must have a closing tag


Tags are Case Sensitive
Elements must be properly nested
Documents must have a root element
Attribute values must be quoted
White-space is preserved in XML

Provides a method to avoid element name


conflicts
When using prefixes in XML, a namespace for the
prefix must be defined.
The namespace is defined by the xmlns
attribute in the start tag of an element.
The namespace declaration has the following
syntax. xmlns:prefix="URI".
<animal:name xmlns:name=
"http://www.domain.com/animal">
9

XML is concerned with being well formed or


correct in syntax.
Two ways of checking whether a document
follows the expected order and structure.

Document Type Definition (DTD)


XML Schema

10

The purpose of a DTD (Document Type


Definition) is to define the legal building blocks
of an XML document.
Can be declared in XML document itself or as an
external file.

11

With a DTD, each of your XML files can carry a


description of its own format.
With a DTD, independent groups of people can
agree to use a standard DTD for interchanging
data.
Your application can use a standard DTD to verify
that the data you receive from the outside world
is valid.
You can also use a DTD to verify your own data.
12

<?xml version="1.0"?>
<!DOCTYPE NEWSPAPER [
<!ELEMENT NEWSPAPER (ARTICLE+)>
<!ELEMENT ARTICLE (HEADLINE,BODY)>
<!ELEMENT HEADLINE (#PCDATA)>
<!ELEMENT BODY (#PCDATA)>
<!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED>
<!ATTLIST ARTICLE EDITION CDATA #IMPLIED>
]>
<NEWSPAPER>

13

<?xml version="1.0"?>
<!DOCTYPE NEWSPAPER SYSTEM
news.dtd>
<NEWSPAPER>

14

<!ELEMENT ele-name EMPTY>


<!ELEMENT ele-name (#PCDATA)>
<!ELEMENT ele-name (child1,child2,...)>
<!ELEMENT ele-name (child+)>
<!ELEMENT ele-name (child*)>
<!ELEMENT ele-name (child?)>
<!ELEMENT ele-name (child | child2)>

15

<!ATTLIST element-name attribute-name attributetype default-value>


<!ATTLIST payment type CDATA "check">
<!ATTLIST payment type CDATA #REQUIRED>

16

CDATA : The value is character data


(en1|en2|..) : The value must be one from an
enumerated list
ID : The value is a unique id

17

Value : The default value of the attribute


#REQUIRED : The attribute is required
#IMPLIED : The attribute is not required
#FIXED : The attribute value is fixed

18

XML Schema describes the structure of an XML


document
Largely replacing DTDs as they are extensible, richer
and generally more useful

19

<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="heading"
type="xs:string"/>
<xs:element name="body"
type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

20

<?xml version="1.0"?>
<note
xmlns="http://www.domain.com"
xmlns:xsi="http://www.w3.org/2001/XMLSch
ema-instance"
xsi:schemaLocation="http://www.domain.co
m/note.xsd">
<heading>Reminder</heading>
<body>Don't forget me this
weekend!</body>
</note>

21

<xs:element name="xxx" type="yyy"


default="aaa"/>

xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
22

A complex element is an XML element that contains


other elements and/or attributes.
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>

23

<xs:attribute name="lang" type="xs:string"


use="required"/>
<xs:attribute name="lang" type="xs:string"
use=optional"/>
<xs:attribute name="lang" type="xs:string"
fixed="EN"/>
<xs:attribute name="lang" type="xs:string"
default="EN"/>
24

Enumeration, length, maxExclusive, maxInclusive,


maxLength, pattern, whiteSpace
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxInclusive value="120"/>
</xs:restriction>
</xs:simpleType>
</xs:element>

25

XML with correct syntax is "Well Formed" XML.


XML validated against a DTD is "Valid" XML.
An XML parser converts an XML document into
an XML DOM object - which can then be
manipulated with JavaScript.

26

eXtensible Stylesheet Language


CSS = Style Sheets for HTML
XSL = Style Sheets for XML
XSL - More Than a Style Sheet Language

XSLT - a language for transforming XML documents


XPath - a language for navigating in XML documents
XSL-FO - a language for formatting XML documents

27

XSL Transformations
XSLT is used to transform an XML document into
another XML document, or another type of
document that is recognized by a browser, like
HTML and XHTML. Normally XSLT does this by
transforming each XML element into an (X)HTML
element.
Add-remove elements, Rearrange-sort elements,
perform tests, etc

28

XSLT uses XPath to find information in an XML


document.
XPath is used to navigate through elements and
attributes in XML documents.

29

<?xml version="1.0"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>19</price>
</cd>
</catalog>

30

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Trans
form">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>

31

<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>
32

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"
href="cdcatalog.xsl"?>
<catalog>

33

Used to build templates

match attribute is used

Used to define a template for the entire XML document


Used to associate a template with an XML element
<xsl:template match="/">

Value of the match attribute is an XPath


expression

34

Nodename : Selects all child nodes


/ : Selects from root node
// : Selects nodes in the document from the
current node that match the selection no matter
where they are
. : Selects the current node
.. : Selects the parent of the current node
@ : Selects attributes
* : Any element node

35

Predicates are used to find a specific node or a


node that contains a specific value.

/bookstore/book[1]
/bookstore/book[last()]
/bookstore/book[last()-1]
/bookstore/book[position()<3]
/bookstore/book[price>35.00]

36

Used to extract the value of an XML element and


add it to the output stream of the transformation
<xsl:value-of select="catalog/cd/title"/>
Used with <xsl:for-each>

37

Allows you to do looping in XSLT


<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>

38

Used to sort the output


<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>

39

Used to put a conditional test against the content of


the XML file
<xsl:for-each select="catalog/cd">
<xsl:if test="price &gt; 10">
<tr>
<td><xsl:value-of
select="title"/></td>
<td><xsl:value-of
select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>

40

Used in conjunction with <xsl:when> and


<xsl:otherwise> to express multiple conditional
tests
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>

41

Used in conjunction with <xsl:when> and


<xsl:otherwise> to express multiple conditional tests
<xsl:choose>
<xsl:when test="price &gt; 10">
<td bgcolor="#ff00ff">
<xsl:value-of
select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of
select="artist"/></td>
</xsl:otherwise>
</xsl:choose>

42

Applies a template to the current element or to


the current element's child nodes
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

43

<xsl:template match="cd">
<p>
<xsl:apply-templates
select="title"/>
<xsl:apply-templates
select="artist"/>
</p>
</xsl:template>

44

<xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:value-of select="."/></span>
<br />
</xsl:template>

45

You might also like