Prof. Arjun V. Bala
9624822202
arjun.bala@darshan.ac.in
2160708
Web Technology
Unit-6
XML
Unit – 6: XML Darshan Institute of Engineering & Technology
2
Outline
1. Introduction
• Introduction to XML
• Features of XML
• XML Key Component
2. Document Type Definition (DTD)
3. XML Schemas
4. XSL
5. XSLT
Unit – 6: XML Darshan Institute of Engineering & Technology
3
Introduction to XML
 XML stands for eXtensible Markup Language
 XML is a language to describe other languages.
 Its main purpose is to allow the sharing of data across different type of systems
and it is particularly useful in this sense for applications that do this over the
internet.
 Example :
<?xml version=“1.0”>
<person>
<first>Narendra</first>
<last>Modi</last>
<birthdate>01/01/45</birthdate>
<employed started=“01/02/03”>
Prof. @ Darshan college
</employed>
</person>
Here person is root element
Here started is an attribute of
element employed
Unit – 6: XML Darshan Institute of Engineering & Technology
4
Features of XML
 It is in a format that both human and machines can read.
 It supports Unicode.
 It supports data structures.
 It is self-documenting.
 It has a strict format that makes it easy for parsing to take place.
 It can be understood and exchanged between dissimilar systems.
 It can be useful for swapping data between different applications.
Unit – 6: XML Darshan Institute of Engineering & Technology
5
XML Key Component
 One of the key aspects of XML is how strict the syntax is.
 There are mainly 3 components of the XML
1. Elements
2. Attribute
3. Namespace
Unit – 6: XML Darshan Institute of Engineering & Technology
6
1) Elements
 The strict syntax of XML contains a few rules about elements that
must be adhered to:
• Elements must have a closing tag.
• Tags are case sensitive
• Elements must be nested correctly
• XML documents must have a root element.
 Example :
<birthdate>26th
October 1788</birthdate> ü
<Birthdate>26th
October 1788</birthdate> û (elements are case sensitive)
<b><i>Hello<b></i> û (elements not nested properly)
<b><i>Hello</i></b> ü
Unit – 6: XML Darshan Institute of Engineering & Technology
7
2) Attributes
 Attributes can be added to elements in XML but must always be
quoted.
 For Example, here employed is a element and started is the
attribute of the element employed.
<employed started=“10/11/12”>Darshan, Rajkot </employed> ü
<employed started=10/11/12>Darshan, Rajkot </employed> û
Value must be quoted
Unit – 6: XML Darshan Institute of Engineering & Technology
8
3) Namespace
 Sometimes in XML there is a danger of conflicting names between
documents.
 Example :
• You create one document with name element for the professor, it may also
possible someone else create a document with name element for the
animal name, so to avoid the conflict we can use namespaces.
 Namespace usually take the form of a URL, beginning with a
domain name, an optional namespace label in the form of a
directory name and finally a version number, which is also
optional.
xmlns = “http://www.mydomain.com/ns/animals/1.1”
Unit – 6: XML Darshan Institute of Engineering & Technology
9
3) Namespace (Example)
 This XML carries information
about a table (a piece of
furniture):
<table>
<name>Saag table</name>
<width>3</width>
<length>6</length>
<weight>5kg</weight>
</table>
 This XML carries HTML table
information:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
Unit – 6: XML Darshan Institute of Engineering & Technology
10
3) Namespace (Example) (Cont.)
 To solve the conflict problem
we can use namespace of
furniture table.
 Example :
<table>
<name>Saag table</name>
<width>3</width>
<length>6</length>
<weight>5kg</weight>
</table>
 To solve the conflict problem
we can use namespace of html
table.
 Example :
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<table
xmlns:h=“http://www.w3.org
/TR/html4/”>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
<h:table
xmlns:h=“http://www.w3.org
/TR/html4/”>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
<table
xmlns:f=“http://darshan.ac.in/fu
rntiture”>
<name>Saag table</name>
<width>3</width>
<length>6</length>
<weight>5kg</weight>
</table>
<f:table
xmlns:f=“http://darshan.ac.in/fu
rntiture”>
<f:name>Saag table</f:name>
<f:width>3</f:width>
<f:length>6</f:length>
<f:weight>5kg</f:weight>
</f:table>
Unit – 6: XML Darshan Institute of Engineering & Technology
11
XML Key Component (Cont.)
 White space is preserved in XML where as in HTML it is truncated
down to just a single space.
 One thing does remain in common with HTML though is
comments,
• Comments can be added using the triangle brackets like this:
<!-- here are some remarks -->
Unit – 6: XML Darshan Institute of Engineering & Technology
12
Document Type Definition (DTD)
 XML is particularly concerned with being well formed or correct in
syntax.
 There are two ways of checking whether the document follows
expected order and structure
• Document Type Definitions (DTDs)
• Schemas
 A Document Type Definition (DTD) defines the legal building
blocks of an XML document.
 A DTD can be declared inline inside an XML document, or as an
external reference
Unit – 6: XML Darshan Institute of Engineering & Technology
13
Why Use a DTD?
 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.
Unit – 6: XML Darshan Institute of Engineering & Technology
14
DTD (Example)
<?xml version="1.0"?>
<!DOCTYPE note [
<!ELEMENT note (to,from,title,message)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT message (#PCDATA)>
]>
Unit – 6: XML Darshan Institute of Engineering & Technology
15
DTD (Cont.)
 DTD can be internal or external
 If it is internal than simply put previous code to the top of the
XML file
 If it is external than save it as .dtd file extension and refer it from
XML,
<!DOCTYPE note SYSTEM “note.dtd”>
Unit – 6: XML Darshan Institute of Engineering & Technology
16
DTD Elements
 We can specify the number of occurrences of the elements using +,
*, ? and | operators (works ~ similar to Regular Expression)
 Example :
• <!ELEMENT note(to+,from,title?,message*) />
 Above example suggest that root element of the xml must be note and
should have one or more (+) recipients, sender should be only one,
title must be one or zero(?) and messages can be zero or more(*).
 We can also specify to have either one of the elements using |
operator
• <!ELEMENT note(to,from,title,message|information) >
 In above declaration we have specified that either message should be
there or the information element should be there in the note
Unit – 6: XML Darshan Institute of Engineering & Technology
17
DTD Attribute
We can specify the attributes also using DTD using ATTLIST
declaration.
 Syntax:
<!ATTLIST element-name attribute-name attribute-type default-value >
 Example :
<!ATTLIST employed started CDATA “01/01/01”>
We can also specify required or fixed for the attribute
 Example :
<!ATTLIST employed started CDATA #REQUIRED>
<!ATTLIST employed started CDATA #FIXED “01/01/01”>
This is the default value for started
This suggest that started is mandatory
field
Unit – 6: XML Darshan Institute of Engineering & Technology
18
XML Schema
 XML Schema is an XML-based alternative to DTD
 An XML schema describes the structure of an XML document.
 The XML Schema language is also referred to as XML Schema
Definition (XSD)
 An XML Schema
• defines elements that can appear in a document
• defines attributes that can appear in a document
• defines which elements are child elements
• defines the order of child elements
• defines data types for elements and attributes
• defines default and fixed values for elements and attributes
Unit – 6: XML Darshan Institute of Engineering & Technology
19
XML Schema (cont.)
 XML Schemas are the Successors of DTDs
• XML Schemas are extensible to future additions
• XML Schemas are richer and more powerful than DTDs
• XML Schemas are written in XML
• XML Schemas support data types
• XML Schemas support namespaces
Unit – 6: XML Darshan Institute of Engineering & Technology
20
XML Schema (Example)
note.xsd
<?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="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>
</xs:schema>
Unit – 6: XML Darshan Institute of Engineering & Technology
21
XML Schema (Example) (cont)
<?xml version="1.0"?>
<note
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“note.xsd“>
<to>Darshan</to>
<from>Student</from>
<heading>Reminder</heading>
<body>Don't forget to attend lecture thisweekend!</body>
</note>
Unit – 6: XML Darshan Institute of Engineering & Technology
22
Data Types in XSD
 xs:string
 xs:decimal
 xs:integer
 xs:boolean
 xs:date
 xs:time
 Etc…..
Unit – 6: XML Darshan Institute of Engineering & Technology
23
Complex Types in XSD
 Complex elements can be built that contain other elements and
attributes.
 For example,
<xs:complexType name=“productinfo”>
<xs:sequence>
<xs:element name=“item” type=“xs:string” />
<xs:element name=“itemcode” type=“xs:string” />
</xs:sequence>
</xs:complexType>
<xs:element name=“food” type=“productinfo”/>
<xs:element name=“magazine” type=“productinfo”/>
<xs:element name=“clothes” type=“productinfo”/>
Unit – 6: XML Darshan Institute of Engineering & Technology
24
Default ,Fixed and Required Values
 To define attribute a similar style is adopted, for example for the
XML element :
<firstname lang=“English”>Narendra</firstname>
 XSD would be
<xs:attribute firstname="lang" type="xs:string"/>
 Default value attribute in XSD
<xs:attribute firstname="lang" type="xs:string" default="EN"/>
 Fixed value attributes in XSD
<xs:attribute firstname ="lang" type="xs:string" fixed="EN"/>
 Required value attributes in XSD
<xs:attribute firstname ="lang" type="xs:string" use="required"/>
Unit – 6: XML Darshan Institute of Engineering & Technology
25
What is XSL
 XSL stands for eXtensible Stylesheet Language.
• CSS = Style Sheets for HTML
• XSL = Style Sheets for XML
 XSL describes how the XML document should be displayed!
 XSL - More Than a Style Sheet Language
 XSL consists of three parts:
• XSLT - a language for transforming XML documents
• XPath - a language for navigating in XML documents
• XSL-FO - a language for formatting XML documents
Unit – 6: XML Darshan Institute of Engineering & Technology
26
What is XSLT?
 XSLT stands for XSL Transformations
 XSLT is the most important part of XSL
 XSLT transforms an XML document into another XML document
(ex. (X)HTML)
 XSLT uses XPath to navigate in XML documents
Unit – 6: XML Darshan Institute of Engineering & Technology
27
XSL Transformation
The style sheet provides the template that transforms the
document from one structure to another
 <xsl:template> starts the definition of the actual template, as the
root of the source XML document
 The match = “/” attribute makes sure this begins applying the
template to the root of the source XML document
 The style sheet is linked into the XML by adding the connecting
statement to the XML document:
<?xml stylesheet type=”text/xsl” href=”abc.xsl” ?>
‐
Unit – 6: XML Darshan Institute of Engineering & Technology
28
XSLT Example
book.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "book.xsl"?>
<library>
<book>
<title>Developing Web Application</title>
<author>Ralph Moseley</author>
<price>109</price>
</book>
<book>
<title>Software engineering</title>
<author>Roger Pressmen</author>
<price>120</price>
</book>
<book>
<title>Java head first</title>
<author>Bob Dylan</author>
<price>400</price>
</book>
</library>
Unit – 6: XML Darshan Institute of Engineering & Technology
29
XSLT Example (Cont)
book.xsl
<?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>Library Book Collection</h2>
<table border="1">
<xsl:for-each select="library/book">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="author"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Unit – 6: XML Darshan Institute of Engineering & Technology
30
XSL Elements
 stylesheet
 template
 value of
‐
 for each
‐
 sort
 If
 choose
 when
 otherwise
Unit – 6: XML Darshan Institute of Engineering & Technology
31
XSL Elements (cont.)
 <xsl:stylesheet>
• <xsl:stylesheet version="1.0”
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
• <xsl:stylesheet>, defines that this document is an XSLT style sheet document
(along with the version number and XSLT namespace attributes).
 <xsl:template match="/">
• The <xsl:template> element is used to build templates.
• Attribute match is used to set the starting point for the XPath
• Match=“/” specifies that XPath is calculated from the root of the document
 <xsl:value-of>
• The <xsl:value-of> element can be used to extract the value of an XML element
• <xsl:value-of select=“XPath"/>
• It will provide output to the stream of transformation
• XPath index will start from 1 (not 0)
Unit – 6: XML Darshan Institute of Engineering & Technology
32
XSL Elements (cont.)
 <xsl:for-each>
• The XSL <xsl:for-each> element can be used to select every XML element of
a specified node-set
<xsl:for-each select=“XPath">
</xsl:for-each>
• We can also filter the output from the XML file by
<xsl:for-each select=“rajkotcolleges/chat[to=‘ABC']">
• Legal filter operators are:
• = (equal)
• ! = (not equal)
• &lt; less than
• &gt; greater than
Unit – 6: XML Darshan Institute of Engineering & Technology
33
XSL Elements (cont.)
 <xsl:sort>
• The <xsl:sort> element is used to sort the output.
• <xsl:sort> is always within <xsl:for-each>
<xsl:for-each select=“rajkotcolleges/chat">
<xsl:sort select=“message“ order=“ascending|descending”/>
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
Unit – 6: XML Darshan Institute of Engineering & Technology
34
XSL Elements (cont.)
 <xsl:if>
• To put a conditional if test against the content of the XML file, add an
<xsl:if> element to the XSL document.
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>
• <xsl:if test="price &gt; 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
Unit – 6: XML Darshan Institute of Engineering & Technology
35
XSL Elements (cont.)
 <xsl:choose>
• The <xsl:choose> element is 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>

programming with xml for graduate students

  • 1.
    Prof. Arjun V.Bala 9624822202 arjun.bala@darshan.ac.in 2160708 Web Technology Unit-6 XML
  • 2.
    Unit – 6:XML Darshan Institute of Engineering & Technology 2 Outline 1. Introduction • Introduction to XML • Features of XML • XML Key Component 2. Document Type Definition (DTD) 3. XML Schemas 4. XSL 5. XSLT
  • 3.
    Unit – 6:XML Darshan Institute of Engineering & Technology 3 Introduction to XML  XML stands for eXtensible Markup Language  XML is a language to describe other languages.  Its main purpose is to allow the sharing of data across different type of systems and it is particularly useful in this sense for applications that do this over the internet.  Example : <?xml version=“1.0”> <person> <first>Narendra</first> <last>Modi</last> <birthdate>01/01/45</birthdate> <employed started=“01/02/03”> Prof. @ Darshan college </employed> </person> Here person is root element Here started is an attribute of element employed
  • 4.
    Unit – 6:XML Darshan Institute of Engineering & Technology 4 Features of XML  It is in a format that both human and machines can read.  It supports Unicode.  It supports data structures.  It is self-documenting.  It has a strict format that makes it easy for parsing to take place.  It can be understood and exchanged between dissimilar systems.  It can be useful for swapping data between different applications.
  • 5.
    Unit – 6:XML Darshan Institute of Engineering & Technology 5 XML Key Component  One of the key aspects of XML is how strict the syntax is.  There are mainly 3 components of the XML 1. Elements 2. Attribute 3. Namespace
  • 6.
    Unit – 6:XML Darshan Institute of Engineering & Technology 6 1) Elements  The strict syntax of XML contains a few rules about elements that must be adhered to: • Elements must have a closing tag. • Tags are case sensitive • Elements must be nested correctly • XML documents must have a root element.  Example : <birthdate>26th October 1788</birthdate> ü <Birthdate>26th October 1788</birthdate> û (elements are case sensitive) <b><i>Hello<b></i> û (elements not nested properly) <b><i>Hello</i></b> ü
  • 7.
    Unit – 6:XML Darshan Institute of Engineering & Technology 7 2) Attributes  Attributes can be added to elements in XML but must always be quoted.  For Example, here employed is a element and started is the attribute of the element employed. <employed started=“10/11/12”>Darshan, Rajkot </employed> ü <employed started=10/11/12>Darshan, Rajkot </employed> û Value must be quoted
  • 8.
    Unit – 6:XML Darshan Institute of Engineering & Technology 8 3) Namespace  Sometimes in XML there is a danger of conflicting names between documents.  Example : • You create one document with name element for the professor, it may also possible someone else create a document with name element for the animal name, so to avoid the conflict we can use namespaces.  Namespace usually take the form of a URL, beginning with a domain name, an optional namespace label in the form of a directory name and finally a version number, which is also optional. xmlns = “http://www.mydomain.com/ns/animals/1.1”
  • 9.
    Unit – 6:XML Darshan Institute of Engineering & Technology 9 3) Namespace (Example)  This XML carries information about a table (a piece of furniture): <table> <name>Saag table</name> <width>3</width> <length>6</length> <weight>5kg</weight> </table>  This XML carries HTML table information: <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
  • 10.
    Unit – 6:XML Darshan Institute of Engineering & Technology 10 3) Namespace (Example) (Cont.)  To solve the conflict problem we can use namespace of furniture table.  Example : <table> <name>Saag table</name> <width>3</width> <length>6</length> <weight>5kg</weight> </table>  To solve the conflict problem we can use namespace of html table.  Example : <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <table xmlns:h=“http://www.w3.org /TR/html4/”> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> <h:table xmlns:h=“http://www.w3.org /TR/html4/”> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> <table xmlns:f=“http://darshan.ac.in/fu rntiture”> <name>Saag table</name> <width>3</width> <length>6</length> <weight>5kg</weight> </table> <f:table xmlns:f=“http://darshan.ac.in/fu rntiture”> <f:name>Saag table</f:name> <f:width>3</f:width> <f:length>6</f:length> <f:weight>5kg</f:weight> </f:table>
  • 11.
    Unit – 6:XML Darshan Institute of Engineering & Technology 11 XML Key Component (Cont.)  White space is preserved in XML where as in HTML it is truncated down to just a single space.  One thing does remain in common with HTML though is comments, • Comments can be added using the triangle brackets like this: <!-- here are some remarks -->
  • 12.
    Unit – 6:XML Darshan Institute of Engineering & Technology 12 Document Type Definition (DTD)  XML is particularly concerned with being well formed or correct in syntax.  There are two ways of checking whether the document follows expected order and structure • Document Type Definitions (DTDs) • Schemas  A Document Type Definition (DTD) defines the legal building blocks of an XML document.  A DTD can be declared inline inside an XML document, or as an external reference
  • 13.
    Unit – 6:XML Darshan Institute of Engineering & Technology 13 Why Use a DTD?  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.
  • 14.
    Unit – 6:XML Darshan Institute of Engineering & Technology 14 DTD (Example) <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,title,message)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT title (#PCDATA)> <!ELEMENT message (#PCDATA)> ]>
  • 15.
    Unit – 6:XML Darshan Institute of Engineering & Technology 15 DTD (Cont.)  DTD can be internal or external  If it is internal than simply put previous code to the top of the XML file  If it is external than save it as .dtd file extension and refer it from XML, <!DOCTYPE note SYSTEM “note.dtd”>
  • 16.
    Unit – 6:XML Darshan Institute of Engineering & Technology 16 DTD Elements  We can specify the number of occurrences of the elements using +, *, ? and | operators (works ~ similar to Regular Expression)  Example : • <!ELEMENT note(to+,from,title?,message*) />  Above example suggest that root element of the xml must be note and should have one or more (+) recipients, sender should be only one, title must be one or zero(?) and messages can be zero or more(*).  We can also specify to have either one of the elements using | operator • <!ELEMENT note(to,from,title,message|information) >  In above declaration we have specified that either message should be there or the information element should be there in the note
  • 17.
    Unit – 6:XML Darshan Institute of Engineering & Technology 17 DTD Attribute We can specify the attributes also using DTD using ATTLIST declaration.  Syntax: <!ATTLIST element-name attribute-name attribute-type default-value >  Example : <!ATTLIST employed started CDATA “01/01/01”> We can also specify required or fixed for the attribute  Example : <!ATTLIST employed started CDATA #REQUIRED> <!ATTLIST employed started CDATA #FIXED “01/01/01”> This is the default value for started This suggest that started is mandatory field
  • 18.
    Unit – 6:XML Darshan Institute of Engineering & Technology 18 XML Schema  XML Schema is an XML-based alternative to DTD  An XML schema describes the structure of an XML document.  The XML Schema language is also referred to as XML Schema Definition (XSD)  An XML Schema • defines elements that can appear in a document • defines attributes that can appear in a document • defines which elements are child elements • defines the order of child elements • defines data types for elements and attributes • defines default and fixed values for elements and attributes
  • 19.
    Unit – 6:XML Darshan Institute of Engineering & Technology 19 XML Schema (cont.)  XML Schemas are the Successors of DTDs • XML Schemas are extensible to future additions • XML Schemas are richer and more powerful than DTDs • XML Schemas are written in XML • XML Schemas support data types • XML Schemas support namespaces
  • 20.
    Unit – 6:XML Darshan Institute of Engineering & Technology 20 XML Schema (Example) note.xsd <?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="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> </xs:schema>
  • 21.
    Unit – 6:XML Darshan Institute of Engineering & Technology 21 XML Schema (Example) (cont) <?xml version="1.0"?> <note xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=“note.xsd“> <to>Darshan</to> <from>Student</from> <heading>Reminder</heading> <body>Don't forget to attend lecture thisweekend!</body> </note>
  • 22.
    Unit – 6:XML Darshan Institute of Engineering & Technology 22 Data Types in XSD  xs:string  xs:decimal  xs:integer  xs:boolean  xs:date  xs:time  Etc…..
  • 23.
    Unit – 6:XML Darshan Institute of Engineering & Technology 23 Complex Types in XSD  Complex elements can be built that contain other elements and attributes.  For example, <xs:complexType name=“productinfo”> <xs:sequence> <xs:element name=“item” type=“xs:string” /> <xs:element name=“itemcode” type=“xs:string” /> </xs:sequence> </xs:complexType> <xs:element name=“food” type=“productinfo”/> <xs:element name=“magazine” type=“productinfo”/> <xs:element name=“clothes” type=“productinfo”/>
  • 24.
    Unit – 6:XML Darshan Institute of Engineering & Technology 24 Default ,Fixed and Required Values  To define attribute a similar style is adopted, for example for the XML element : <firstname lang=“English”>Narendra</firstname>  XSD would be <xs:attribute firstname="lang" type="xs:string"/>  Default value attribute in XSD <xs:attribute firstname="lang" type="xs:string" default="EN"/>  Fixed value attributes in XSD <xs:attribute firstname ="lang" type="xs:string" fixed="EN"/>  Required value attributes in XSD <xs:attribute firstname ="lang" type="xs:string" use="required"/>
  • 25.
    Unit – 6:XML Darshan Institute of Engineering & Technology 25 What is XSL  XSL stands for eXtensible Stylesheet Language. • CSS = Style Sheets for HTML • XSL = Style Sheets for XML  XSL describes how the XML document should be displayed!  XSL - More Than a Style Sheet Language  XSL consists of three parts: • XSLT - a language for transforming XML documents • XPath - a language for navigating in XML documents • XSL-FO - a language for formatting XML documents
  • 26.
    Unit – 6:XML Darshan Institute of Engineering & Technology 26 What is XSLT?  XSLT stands for XSL Transformations  XSLT is the most important part of XSL  XSLT transforms an XML document into another XML document (ex. (X)HTML)  XSLT uses XPath to navigate in XML documents
  • 27.
    Unit – 6:XML Darshan Institute of Engineering & Technology 27 XSL Transformation The style sheet provides the template that transforms the document from one structure to another  <xsl:template> starts the definition of the actual template, as the root of the source XML document  The match = “/” attribute makes sure this begins applying the template to the root of the source XML document  The style sheet is linked into the XML by adding the connecting statement to the XML document: <?xml stylesheet type=”text/xsl” href=”abc.xsl” ?> ‐
  • 28.
    Unit – 6:XML Darshan Institute of Engineering & Technology 28 XSLT Example book.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type = "text/xsl" href = "book.xsl"?> <library> <book> <title>Developing Web Application</title> <author>Ralph Moseley</author> <price>109</price> </book> <book> <title>Software engineering</title> <author>Roger Pressmen</author> <price>120</price> </book> <book> <title>Java head first</title> <author>Bob Dylan</author> <price>400</price> </book> </library>
  • 29.
    Unit – 6:XML Darshan Institute of Engineering & Technology 29 XSLT Example (Cont) book.xsl <?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>Library Book Collection</h2> <table border="1"> <xsl:for-each select="library/book"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="author"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
  • 30.
    Unit – 6:XML Darshan Institute of Engineering & Technology 30 XSL Elements  stylesheet  template  value of ‐  for each ‐  sort  If  choose  when  otherwise
  • 31.
    Unit – 6:XML Darshan Institute of Engineering & Technology 31 XSL Elements (cont.)  <xsl:stylesheet> • <xsl:stylesheet version="1.0” xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> • <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).  <xsl:template match="/"> • The <xsl:template> element is used to build templates. • Attribute match is used to set the starting point for the XPath • Match=“/” specifies that XPath is calculated from the root of the document  <xsl:value-of> • The <xsl:value-of> element can be used to extract the value of an XML element • <xsl:value-of select=“XPath"/> • It will provide output to the stream of transformation • XPath index will start from 1 (not 0)
  • 32.
    Unit – 6:XML Darshan Institute of Engineering & Technology 32 XSL Elements (cont.)  <xsl:for-each> • The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set <xsl:for-each select=“XPath"> </xsl:for-each> • We can also filter the output from the XML file by <xsl:for-each select=“rajkotcolleges/chat[to=‘ABC']"> • Legal filter operators are: • = (equal) • ! = (not equal) • &lt; less than • &gt; greater than
  • 33.
    Unit – 6:XML Darshan Institute of Engineering & Technology 33 XSL Elements (cont.)  <xsl:sort> • The <xsl:sort> element is used to sort the output. • <xsl:sort> is always within <xsl:for-each> <xsl:for-each select=“rajkotcolleges/chat"> <xsl:sort select=“message“ order=“ascending|descending”/> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each>
  • 34.
    Unit – 6:XML Darshan Institute of Engineering & Technology 34 XSL Elements (cont.)  <xsl:if> • To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document. <xsl:if test="expression"> ...some output if the expression is true... </xsl:if> • <xsl:if test="price &gt; 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="price"/></td> </tr> </xsl:if>
  • 35.
    Unit – 6:XML Darshan Institute of Engineering & Technology 35 XSL Elements (cont.)  <xsl:choose> • The <xsl:choose> element is 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>