Introduction to XML Svetlin Nakov Bulgarian Association of Software Developers www.devbg.org
Contents What is XML? XML and HTML When to use XML? Namespaces Schemes and validation – DTD  and XSD schemes XPath Language XSL XSL transformations
What is XML
What is XML? Universal language (notation) for describing structured data The data is stored together with the meta-information about it Looks like HTML – text based, uses tags  and attributes It is used to describe other languages (formats) for data representation
What is XML? (2) Worldwide-affirmative   standard, supported by the W3C (www.w3c.org) Platform, programming languages and OS independent
XML - Example <?xml version=&quot;1.0&quot;?> <library name=&quot;.NET Developer's Library&quot;> <book> <title>Programming Microsoft .NET</title> <author>Jeff Prosise</author> <isbn>0-7356-1376-1</isbn> </book> <book> <title>Microsoft .NET for Programmers</title> <author>Fergal Grimes</author> <isbn>1-930110-19-7</isbn> </book> </library>
XML  –  Example <?xml version=&quot;1.0&quot;?> <library  name=&quot;.NET Developer's Library&quot; > <book> <title>Programming Microsoft .NET</title> <author>Jeff Prosise</author> <isbn>0-7356-1376-1</isbn> </book> <book> <title>Microsoft   .NET for Programmers</title> <author>Fergal Grimes</author> <isbn> 1-930110-19-7 </isbn> </book> </library> attribute header part  ( prolog ) element closing tag element value opening tag
XML   and HTML Likenesses between   XML   and   HTML : Both are text based Both use tags and attributes Differences between XML and HTML: HTML is a language, and XML is a  syntax for describing other languages HTML describes the formatting of information, and XML describes structured information XML requires the documents to be  well-formed
Well-Formed Documents XML requires the documents to be well-formed Tags should always be closed in the right order Attributes should always be closed The document should contain only one root element There are restrictions of the tag and attribute names
Well-Formed Documents (2) Example of bad-defined XML document < xml> <button bug! value=&quot;OK name=&quot;b1&quot;> <animation source=&quot;demo1.avi&quot;>  1 < 2 < 3 </click-button> < / xml >
When to Use   XML? Use XML : For information exchange between different systems For storing structured data To create custom-defined languages for describing information Configuration data
XML Disadvantages XML disadvantages: Data is more massive (take more space) Decreased performance Frequently there is a need of more memory Increased network traffic
Namespaces
Namespaces Namespaces in XML documents add the possibility to use different tags with the same name : <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <country:towns xmlns:country=&quot;urn:nakov-com:country&quot; xmlns:town=&quot;http:// www. nakov.com/town&quot;> <town:town> <town:name> Sofia </town:name> <town:population>1 200 000</town:population> <country:name> Bulgaria </country:name> </town:town> <town:town> <town:name>Plovdiv</town:name> <town:population>700 000</town:population> <country:name>Bulgaria</country:name> </town:town> </country:towns>
Namespaces - Example <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <country:towns  xmlns:country=&quot;urn:nakov-com:country&quot; xmlns:town=&quot;http:// www. nakov.com/town&quot;> <town:town> <town:name>Sofia</town:name> <town:population>1 200 000</town:population> <country:name>Bulgaria</country:name> </town:town> <town:town> <town:name>Plovdiv</town:name> <town:population>700 000</town:population> <country:name> Bulgaria </country:name> </town:town> </country:towns> Namespace with prefix  &quot; country &quot;  and  URI  identifier  &quot; urn:nakov-com:country &quot; Tag with name  &quot; name &quot;  from namespace  &quot; country &quot;,  the full name is  &quot; urn:nakov-com:country:name &quot;
Namespaces (2) It is possible to use default namespaces <?xml version=&quot;1.0&quot; encoding=&quot;windows-1251&quot;?> <order  xmlns=&quot;http://www.hranitelni-stoki.com/orders&quot; > <item> <name>бира &quot;Загорка&quot;</name> <ammount> 8 </ammount> <measure>бутилка</measure> <price> 3 . 76 </price> </item> <item> <name>кебапчета</name> <ammount>12</ammount> <measure>брой</measure> <price> 4.20 </price> </item> </order>
XML Schemes and Validation
Schemes and Validation The XML documents structure is defined  by schemes Schemes describes Possible tags Possible attributes Possible values for the attributes and elements Order of the tags Default values
The   DTD Language DTD  ( Document Type Definition )  is : Formal language for describing XML document structures Contains sum of rules for the tags and their attributes in a document Text-based language, but not XML  based Rarely used because it is substituted  by XSD
The   DTD Language (2) DTD Example: <!ELEMENT library (book+)> <!ATTLIST library name CDATA #REQUIRED > <!ELEMENT book (title, author, isbn)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT  isbn  (#PCDATA)>
XSD Schemes XSD (XML Scheme Definition Language) is: Powerful XML-based language for describing the structure of XML documents Contains sum of rules for the tags  and their attributes is a document XSD schemes have bigger descriptive  power than the DTD XSD consequently replaces   DTD
XSD Scheme Example <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xs:schema   xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:element name=&quot;library&quot;> <xs:complexType> <xs:sequence> <xs:element ref=&quot;book&quot; maxOccurs=&quot;unbounded&quot;/> </xs:sequence> <xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot;   use=&quot;optional&quot;/> </xs:complexType> </xs:element>   (the example continues)
XSD Scheme Example (2) <xs:element name=&quot;book&quot;> <xs:complexType> <xs:sequence> <xs:element ref=&quot;title&quot;/> <xs:element ref=&quot;author&quot;/> <xs:element ref=&quot;isbn&quot;/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;author&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;isbn&quot; type=&quot;xs:string&quot;/> </xs:schema>
XPath
XPath XPath (XML Path Language) is a language for addressing parts of XML documents XPath expressions contains description of paths to nodes and criteria, which the nodes should pass XPath is frequently used with XSLT and XPointer Example for XPath expressions: /library/book[isbn= ' 1-930110-19-7 ' ] /catalog/cd[ @ price < 10.80]
XPath   Expressions /  – addresses the root element /someNode  – addresses all nodes with name &quot; someNode &quot;,  direct  inheritors to the root /books/book  – addresses all nodes  &quot; book &quot;  ,  direct  inheritors to the node &quot; books &quot; /books/book[price<&quot;10&quot;]/author  – addresses all authors ( /books/book/ author ), whose books have price < &quot; 10 &quot;   /items/item[@type=&quot;food&quot;]  – addresses all nodes with name  item , which have attribute &quot; type &quot; with value &quot; food &quot;  and are inheritors to the &quot; items &quot; node
XSL
XSL for Cross - Media Publishing PDF as  digital preprint HTML a s  standard  I nternet  F ormat For Online  P ublishing Author writes in text  processing system using style sheets System saves as/ converts to XML <XML-Document> Catalogue entry MARC-Format Library catalogue XSLT-Stylesheet XSLT-Stylesheet XSLT-Stylesheet
XSL Transformation Language (XSLT)
What is XSLT? XSLT stands for XSL Transformations XSLT transforms an XML document into another  text format  document U ses XPath to navigate in  the  XML document XSLT is a W3C Recommendation
XSL/XSLT Capabilities  Add prefix or suffix text to content Remove, create, re-order and sort elements Re-use elements elsewhere in the document Transform data between XML formats Specify the XSL formatting objects to be applied to each element class Uses a recursive mechanism to go through document
Browsers Support Note:  Not all browsers  have full support for XML and XSLT  Internet Explorer 6  – full support Mozilla 1.7.8   -  is available with an  XSLT implementation Netscape 8  -  based on the Mozilla engine. Same XML / XSLT support as Mozilla Opera 8  -  supports XML + CSS
Simple XML <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <catalog> <cd> <title> Evergreens </title> <artist></artist> <country>USA</country> <company> USA Music Enterprise </company> <price>1 2 . 0 0</price> <year>198 0 </year>  </cd> . . .  </catalog>  First we have a XML document catalog.xml
Create an XSL Style Sheet <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;/&quot;> <html> <body> <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> Then  create an XSL Style Sheet  ( cdc.xsl )
Link the XSL Style Sheet to the XML Document A XML document can be linked explicitly to a XSL transformation document Use processing instruction  <?xml-stylesheet ?> <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <?xml-stylesheet type=&quot;text/xsl&quot;  href=&quot;cdc.xsl&quot;?> <catalog> ... </catalog>
Output Format Control <xsl:stylesheet xmlns:xsl= . . .> <xsl:output method= &quot; html &quot;   encoding= &quot; UTF-8 &quot;  … /> <xsl:template  match = &quot; / &quot; > <HTML>…</HTML> </xsl:template> </xsl:stylesheet> Define output as XML, HTML, or text Can specify the character encoding Control the XML declaration and  DOCTYPE
XSLT Specification Available from  http://www.w3.org/XSL/ Defines 34 elements and their attributes Can build useful style sheets using stylesheet template apply-templates Depends on the XPath specification
Template Element Template element specifies transformation rule The match attribute takes XPath expressions If more than one XPath matches – then use priority rules to determine which is used  <xsl:template match=&quot;/&quot;>   … </xsl:template>
Import Element Use import element to include multiple XSLT files <stylesheet … >   < import  href=&quot;tables.xsl&quot;/>   < import  href=&quot;features.xsl&quot;>   <template … > … </template>   …  </stylesheet>
The <xsl:value-of> Element U sed to extract the value of an XML element  Note: The value of the  select  attribute  is an XPath expression  ... <xsl:value-of select=&quot;catalog/cd/title&quot;/> <xsl:value-of select=&quot;catalog/cd/artist&quot;/> ...
The <xsl:for-each> Element U sed to select every XML element of a specified node-set  (in a loop) Note: The value of the  select  attribute  is an XPath expression  ... <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:for-each> </body> ...
The <xsl:for-each> Element  Filter output We can also filter the output from the XML <xsl:for-each select=&quot;catalog/cd[artist='Bob  Dylan ']&quot;>  We use expressions in square brackets [ ... ] for setting conditions Legal operator are: =  (equal)  != (not equal)  &lt; less than  &gt; greater than
XSLT <xsl:sort> Element U sed to sort the output Add this element  inside the  <xsl:for-each> element  Note: The  select   attribute indicates what XML element to sort on <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <xsl:sort select=&quot;artist&quot;/> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:for-each> </body>
Sort Element Properties The ' order ' attribute ' ascending ' or ' descending ' The ' data-type ' attribute ' text ' (default) or ' number ' The ' case-order ' attribute ' lower-first ' or ' upper-first ' <xsl:sort select=&quot; product/price &quot; data-type=&quot;number&quot; order=&quot;descending&quot; />
The <xsl:if> Element U sed to put a conditional test against  the content of the XML file Note:  The value of the required test attribute contains the expression to be evaluated <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <xsl:if test=&quot;price &gt; 10&quot;>   <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:if> </xsl:for-each> </body>
The <xsl:choose> Element U sed in conjunction with  <xsl:when>  and  <xsl:otherwise> t o express multiple conditional tests  <xsl:choose> <xsl:when test=&quot;XPath  expression &quot;>  ... some output ...  </xsl:when> <xsl:otherwise>   ... some output ....  </xsl:otherwise> </xsl:choose>
The  <xsl:apply-templates>   Element A pplies a template to the current element or to the current  element's  child nodes S elect attribute  –  will process only the child element that matches the value of the attribute  Note:  The value of the required  select  attribute is a XPath expression <xsl:template match=&quot;cd&quot;> <p> <xsl:apply-templates select=&quot;title&quot;/> </p> </xsl:template> <xsl:template  match=&quot;title&quot; >  Title:  <xsl:value-of select=&quot;.&quot;/> <br />  </xsl:template>
Variable Element Variables may be declared and used in XSLT Variable is referenced with  $  notation <xsl:variable name=&quot;colour&quot;>red</xsl:variable> <xsl:value-of select=&quot;$colour&quot;/>
Reusing Templates If same formatting is required many times //   A named template called by call-template <xsl:template  name=&quot;CreateHeader&quot; >   <html:hr/>   <html:h2>***<xsl:apply-templates/>***</html:h2>   <html:hr/> </xsl:template> ... <xsl:template match=&quot;title&quot;>   < xsl:call-template  name=&quot;CreateHeader&quot; /> </xsl:template> <xsl:template match=&quot;head&quot;>   < xsl:call-template  name=”CreateHeader&quot; /> </xsl:template>
Attribute Values Use ' value-of ' element to access attributes Attributes identified by ' @ ' prefix <full-name first=&quot;John&quot; second=&quot;Smith&quot;/> ----------------------------------------- <xsl:template match=&quot;full-name&quot;>   <ajr:person>   <xsl:value-of  select=&quot;@first&quot; >    <xsl:value-of  select=&quot;@second&quot; >   </ajr:person>   <ajr:person name=&quot;{@first} {@second}&quot; /> </xsl:template> ----------------------------------------- <ajr:person>John Smith</ajr:person> <ajr:person name=&quot;John Smith&quot;/>
Creating Elements Use ' Element ' tag to create new elements Most powerful with use of variables <xsl:template match= &quot; name &quot; > <xsl:element name= &quot; {.}&quot;>   Nice person!   </xsl:element> </xsl:template> <name>Gosho</name Input <Gosho> Nice person! </Gosho> Result
XSLT Live Demo
Introduction to XML Questions?
Exercises What does the XML language represents ?  What is it used for?   When do we use it ? Create an XML document  students.xml ,  which contains structured description of students. For each student you should  enter information for his name, sex, birth  date, phone, course, specialty, faculty  number and a list of taken exams (exam name, tutor, grade). What does the namespaces represent in the XML documents? What are they used for? When do we used them?
Exercises (2) Add default namespace for the students  &quot; urn:students &quot; . Write an XML file containing orders. Each order is described by date, customer name and a list of order items. Each order item consists of product name, amount and price.  Write an XSL stylesheet to transform the XML file to a human readable XHTML document. Sort the products in alphabetical order. Test the produced XHTML file in your Web browser.
Exercises (3) Write your CV in XML format. It should have the following structure: Personal information (name, DOB, ...) Education Skills Work experience ... Write a XSL stylesheet for transforming the CV to HTML and XML with other structure.

Introduction to XML

  • 1.
    Introduction to XMLSvetlin Nakov Bulgarian Association of Software Developers www.devbg.org
  • 2.
    Contents What isXML? XML and HTML When to use XML? Namespaces Schemes and validation – DTD and XSD schemes XPath Language XSL XSL transformations
  • 3.
  • 4.
    What is XML?Universal language (notation) for describing structured data The data is stored together with the meta-information about it Looks like HTML – text based, uses tags and attributes It is used to describe other languages (formats) for data representation
  • 5.
    What is XML?(2) Worldwide-affirmative standard, supported by the W3C (www.w3c.org) Platform, programming languages and OS independent
  • 6.
    XML - Example<?xml version=&quot;1.0&quot;?> <library name=&quot;.NET Developer's Library&quot;> <book> <title>Programming Microsoft .NET</title> <author>Jeff Prosise</author> <isbn>0-7356-1376-1</isbn> </book> <book> <title>Microsoft .NET for Programmers</title> <author>Fergal Grimes</author> <isbn>1-930110-19-7</isbn> </book> </library>
  • 7.
    XML – Example <?xml version=&quot;1.0&quot;?> <library name=&quot;.NET Developer's Library&quot; > <book> <title>Programming Microsoft .NET</title> <author>Jeff Prosise</author> <isbn>0-7356-1376-1</isbn> </book> <book> <title>Microsoft .NET for Programmers</title> <author>Fergal Grimes</author> <isbn> 1-930110-19-7 </isbn> </book> </library> attribute header part ( prolog ) element closing tag element value opening tag
  • 8.
    XML and HTML Likenesses between XML and HTML : Both are text based Both use tags and attributes Differences between XML and HTML: HTML is a language, and XML is a syntax for describing other languages HTML describes the formatting of information, and XML describes structured information XML requires the documents to be well-formed
  • 9.
    Well-Formed Documents XMLrequires the documents to be well-formed Tags should always be closed in the right order Attributes should always be closed The document should contain only one root element There are restrictions of the tag and attribute names
  • 10.
    Well-Formed Documents (2)Example of bad-defined XML document < xml> <button bug! value=&quot;OK name=&quot;b1&quot;> <animation source=&quot;demo1.avi&quot;> 1 < 2 < 3 </click-button> < / xml >
  • 11.
    When to Use XML? Use XML : For information exchange between different systems For storing structured data To create custom-defined languages for describing information Configuration data
  • 12.
    XML Disadvantages XMLdisadvantages: Data is more massive (take more space) Decreased performance Frequently there is a need of more memory Increased network traffic
  • 13.
  • 14.
    Namespaces Namespaces inXML documents add the possibility to use different tags with the same name : <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <country:towns xmlns:country=&quot;urn:nakov-com:country&quot; xmlns:town=&quot;http:// www. nakov.com/town&quot;> <town:town> <town:name> Sofia </town:name> <town:population>1 200 000</town:population> <country:name> Bulgaria </country:name> </town:town> <town:town> <town:name>Plovdiv</town:name> <town:population>700 000</town:population> <country:name>Bulgaria</country:name> </town:town> </country:towns>
  • 15.
    Namespaces - Example<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <country:towns xmlns:country=&quot;urn:nakov-com:country&quot; xmlns:town=&quot;http:// www. nakov.com/town&quot;> <town:town> <town:name>Sofia</town:name> <town:population>1 200 000</town:population> <country:name>Bulgaria</country:name> </town:town> <town:town> <town:name>Plovdiv</town:name> <town:population>700 000</town:population> <country:name> Bulgaria </country:name> </town:town> </country:towns> Namespace with prefix &quot; country &quot; and URI identifier &quot; urn:nakov-com:country &quot; Tag with name &quot; name &quot; from namespace &quot; country &quot;, the full name is &quot; urn:nakov-com:country:name &quot;
  • 16.
    Namespaces (2) Itis possible to use default namespaces <?xml version=&quot;1.0&quot; encoding=&quot;windows-1251&quot;?> <order xmlns=&quot;http://www.hranitelni-stoki.com/orders&quot; > <item> <name>бира &quot;Загорка&quot;</name> <ammount> 8 </ammount> <measure>бутилка</measure> <price> 3 . 76 </price> </item> <item> <name>кебапчета</name> <ammount>12</ammount> <measure>брой</measure> <price> 4.20 </price> </item> </order>
  • 17.
    XML Schemes andValidation
  • 18.
    Schemes and ValidationThe XML documents structure is defined by schemes Schemes describes Possible tags Possible attributes Possible values for the attributes and elements Order of the tags Default values
  • 19.
    The DTD Language DTD ( Document Type Definition ) is : Formal language for describing XML document structures Contains sum of rules for the tags and their attributes in a document Text-based language, but not XML based Rarely used because it is substituted by XSD
  • 20.
    The DTD Language (2) DTD Example: <!ELEMENT library (book+)> <!ATTLIST library name CDATA #REQUIRED > <!ELEMENT book (title, author, isbn)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT isbn (#PCDATA)>
  • 21.
    XSD Schemes XSD(XML Scheme Definition Language) is: Powerful XML-based language for describing the structure of XML documents Contains sum of rules for the tags and their attributes is a document XSD schemes have bigger descriptive power than the DTD XSD consequently replaces DTD
  • 22.
    XSD Scheme Example<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xs:schema xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:element name=&quot;library&quot;> <xs:complexType> <xs:sequence> <xs:element ref=&quot;book&quot; maxOccurs=&quot;unbounded&quot;/> </xs:sequence> <xs:attribute name=&quot;name&quot; type=&quot;xs:string&quot; use=&quot;optional&quot;/> </xs:complexType> </xs:element> (the example continues)
  • 23.
    XSD Scheme Example(2) <xs:element name=&quot;book&quot;> <xs:complexType> <xs:sequence> <xs:element ref=&quot;title&quot;/> <xs:element ref=&quot;author&quot;/> <xs:element ref=&quot;isbn&quot;/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name=&quot;title&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;author&quot; type=&quot;xs:string&quot;/> <xs:element name=&quot;isbn&quot; type=&quot;xs:string&quot;/> </xs:schema>
  • 24.
  • 25.
    XPath XPath (XMLPath Language) is a language for addressing parts of XML documents XPath expressions contains description of paths to nodes and criteria, which the nodes should pass XPath is frequently used with XSLT and XPointer Example for XPath expressions: /library/book[isbn= ' 1-930110-19-7 ' ] /catalog/cd[ @ price < 10.80]
  • 26.
    XPath Expressions / – addresses the root element /someNode – addresses all nodes with name &quot; someNode &quot;, direct inheritors to the root /books/book – addresses all nodes &quot; book &quot; , direct inheritors to the node &quot; books &quot; /books/book[price<&quot;10&quot;]/author – addresses all authors ( /books/book/ author ), whose books have price < &quot; 10 &quot; /items/item[@type=&quot;food&quot;] – addresses all nodes with name item , which have attribute &quot; type &quot; with value &quot; food &quot; and are inheritors to the &quot; items &quot; node
  • 27.
  • 28.
    XSL for Cross- Media Publishing PDF as digital preprint HTML a s standard I nternet F ormat For Online P ublishing Author writes in text processing system using style sheets System saves as/ converts to XML <XML-Document> Catalogue entry MARC-Format Library catalogue XSLT-Stylesheet XSLT-Stylesheet XSLT-Stylesheet
  • 29.
  • 30.
    What is XSLT?XSLT stands for XSL Transformations XSLT transforms an XML document into another text format document U ses XPath to navigate in the XML document XSLT is a W3C Recommendation
  • 31.
    XSL/XSLT Capabilities Add prefix or suffix text to content Remove, create, re-order and sort elements Re-use elements elsewhere in the document Transform data between XML formats Specify the XSL formatting objects to be applied to each element class Uses a recursive mechanism to go through document
  • 32.
    Browsers Support Note: Not all browsers have full support for XML and XSLT Internet Explorer 6 – full support Mozilla 1.7.8 - is available with an XSLT implementation Netscape 8 - based on the Mozilla engine. Same XML / XSLT support as Mozilla Opera 8 - supports XML + CSS
  • 33.
    Simple XML <?xmlversion=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <catalog> <cd> <title> Evergreens </title> <artist></artist> <country>USA</country> <company> USA Music Enterprise </company> <price>1 2 . 0 0</price> <year>198 0 </year> </cd> . . . </catalog> First we have a XML document catalog.xml
  • 34.
    Create an XSLStyle Sheet <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> <xsl:template match=&quot;/&quot;> <html> <body> <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet> Then create an XSL Style Sheet ( cdc.xsl )
  • 35.
    Link the XSLStyle Sheet to the XML Document A XML document can be linked explicitly to a XSL transformation document Use processing instruction <?xml-stylesheet ?> <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;cdc.xsl&quot;?> <catalog> ... </catalog>
  • 36.
    Output Format Control<xsl:stylesheet xmlns:xsl= . . .> <xsl:output method= &quot; html &quot; encoding= &quot; UTF-8 &quot; … /> <xsl:template match = &quot; / &quot; > <HTML>…</HTML> </xsl:template> </xsl:stylesheet> Define output as XML, HTML, or text Can specify the character encoding Control the XML declaration and DOCTYPE
  • 37.
    XSLT Specification Availablefrom http://www.w3.org/XSL/ Defines 34 elements and their attributes Can build useful style sheets using stylesheet template apply-templates Depends on the XPath specification
  • 38.
    Template Element Templateelement specifies transformation rule The match attribute takes XPath expressions If more than one XPath matches – then use priority rules to determine which is used <xsl:template match=&quot;/&quot;> … </xsl:template>
  • 39.
    Import Element Useimport element to include multiple XSLT files <stylesheet … > < import href=&quot;tables.xsl&quot;/> < import href=&quot;features.xsl&quot;> <template … > … </template> … </stylesheet>
  • 40.
    The <xsl:value-of> ElementU sed to extract the value of an XML element Note: The value of the select attribute is an XPath expression ... <xsl:value-of select=&quot;catalog/cd/title&quot;/> <xsl:value-of select=&quot;catalog/cd/artist&quot;/> ...
  • 41.
    The <xsl:for-each> ElementU sed to select every XML element of a specified node-set (in a loop) Note: The value of the select attribute is an XPath expression ... <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:for-each> </body> ...
  • 42.
    The <xsl:for-each> Element Filter output We can also filter the output from the XML <xsl:for-each select=&quot;catalog/cd[artist='Bob Dylan ']&quot;> We use expressions in square brackets [ ... ] for setting conditions Legal operator are: =  (equal) != (not equal) &lt; less than &gt; greater than
  • 43.
    XSLT <xsl:sort> ElementU sed to sort the output Add this element inside the <xsl:for-each> element Note: The select attribute indicates what XML element to sort on <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <xsl:sort select=&quot;artist&quot;/> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:for-each> </body>
  • 44.
    Sort Element PropertiesThe ' order ' attribute ' ascending ' or ' descending ' The ' data-type ' attribute ' text ' (default) or ' number ' The ' case-order ' attribute ' lower-first ' or ' upper-first ' <xsl:sort select=&quot; product/price &quot; data-type=&quot;number&quot; order=&quot;descending&quot; />
  • 45.
    The <xsl:if> ElementU sed to put a conditional test against the content of the XML file Note: The value of the required test attribute contains the expression to be evaluated <h2>My CD Collection</h2> <xsl:for-each select=&quot;catalog/cd&quot;> <xsl:if test=&quot;price &gt; 10&quot;> <br/> <xsl:value-of select=&quot;title&quot;/> <xsl:value-of select=&quot;artist&quot;/> </xsl:if> </xsl:for-each> </body>
  • 46.
    The <xsl:choose> ElementU sed in conjunction with <xsl:when> and <xsl:otherwise> t o express multiple conditional tests <xsl:choose> <xsl:when test=&quot;XPath expression &quot;> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>
  • 47.
    The <xsl:apply-templates> Element A pplies a template to the current element or to the current element's child nodes S elect attribute – will process only the child element that matches the value of the attribute Note: The value of the required select attribute is a XPath expression <xsl:template match=&quot;cd&quot;> <p> <xsl:apply-templates select=&quot;title&quot;/> </p> </xsl:template> <xsl:template match=&quot;title&quot; > Title: <xsl:value-of select=&quot;.&quot;/> <br /> </xsl:template>
  • 48.
    Variable Element Variablesmay be declared and used in XSLT Variable is referenced with $ notation <xsl:variable name=&quot;colour&quot;>red</xsl:variable> <xsl:value-of select=&quot;$colour&quot;/>
  • 49.
    Reusing Templates Ifsame formatting is required many times // A named template called by call-template <xsl:template name=&quot;CreateHeader&quot; > <html:hr/> <html:h2>***<xsl:apply-templates/>***</html:h2> <html:hr/> </xsl:template> ... <xsl:template match=&quot;title&quot;> < xsl:call-template name=&quot;CreateHeader&quot; /> </xsl:template> <xsl:template match=&quot;head&quot;> < xsl:call-template name=”CreateHeader&quot; /> </xsl:template>
  • 50.
    Attribute Values Use' value-of ' element to access attributes Attributes identified by ' @ ' prefix <full-name first=&quot;John&quot; second=&quot;Smith&quot;/> ----------------------------------------- <xsl:template match=&quot;full-name&quot;> <ajr:person> <xsl:value-of select=&quot;@first&quot; > <xsl:value-of select=&quot;@second&quot; > </ajr:person> <ajr:person name=&quot;{@first} {@second}&quot; /> </xsl:template> ----------------------------------------- <ajr:person>John Smith</ajr:person> <ajr:person name=&quot;John Smith&quot;/>
  • 51.
    Creating Elements Use' Element ' tag to create new elements Most powerful with use of variables <xsl:template match= &quot; name &quot; > <xsl:element name= &quot; {.}&quot;> Nice person! </xsl:element> </xsl:template> <name>Gosho</name Input <Gosho> Nice person! </Gosho> Result
  • 52.
  • 53.
  • 54.
    Exercises What doesthe XML language represents ? What is it used for? When do we use it ? Create an XML document students.xml , which contains structured description of students. For each student you should enter information for his name, sex, birth date, phone, course, specialty, faculty number and a list of taken exams (exam name, tutor, grade). What does the namespaces represent in the XML documents? What are they used for? When do we used them?
  • 55.
    Exercises (2) Adddefault namespace for the students &quot; urn:students &quot; . Write an XML file containing orders. Each order is described by date, customer name and a list of order items. Each order item consists of product name, amount and price. Write an XSL stylesheet to transform the XML file to a human readable XHTML document. Sort the products in alphabetical order. Test the produced XHTML file in your Web browser.
  • 56.
    Exercises (3) Writeyour CV in XML format. It should have the following structure: Personal information (name, DOB, ...) Education Skills Work experience ... Write a XSL stylesheet for transforming the CV to HTML and XML with other structure.

Editor's Notes

  • #2 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #3 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #4 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #14 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #18 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #25 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #28 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #29 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #30 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #31 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## XSLT is the most important part of XSL. 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. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more. A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree . In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.
  • #32 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #33 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #34 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #35 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #36 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #37 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ## Output Format Control &lt;xsl:output&gt; has a large number of attributes that control the way the result document is produced, including generation of true HTML rather than XHTML output. For an XML document, this feature allows authors fairly explicit control over the contents of the result document XML declaration, the DOCTYPE declaration, use of CDATA sections, and so on. Specifying html for the method causes the XSLT processor to produce conventional HTML as output text instead of XHTML. Specifying text causes the output to consist only of element text content, concatenated together.
  • #38 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #39 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #40 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #41 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #42 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #43 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #44 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #45 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #46 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #47 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #48 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #49 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #50 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #51 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #52 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • #53 (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##