Introduction to XML Svetlin Nakov Bulgarian Association of Software Developers www.devbg.org
Contents <ul><li>What is XML? </li></ul><ul><li>XML and HTML </li></ul><ul><li>When to use XML? </li></ul><ul><li>Namespac...
What is XML
What is XML? <ul><li>Universal language (notation) for describing structured data </li></ul><ul><li>The data is stored tog...
What is XML? (2) <ul><li>Worldwide-affirmative   standard, supported by the W3C (www.w3c.org) </li></ul><ul><li>Platform, ...
XML - Example <?xml version=&quot;1.0&quot;?> <library name=&quot;.NET Developer's Library&quot;> <book> <title>Programmin...
XML  –  Example <?xml version=&quot;1.0&quot;?> <library  name=&quot;.NET Developer's Library&quot; > <book> <title>Progra...
XML   and HTML <ul><li>Likenesses between   XML   and   HTML : </li></ul><ul><ul><li>Both are text based </li></ul></ul><u...
Well-Formed Documents <ul><li>XML requires the documents to be well-formed </li></ul><ul><ul><li>Tags should always be clo...
Well-Formed Documents (2) <ul><li>Example of bad-defined XML document </li></ul>< xml> <button bug! value=&quot;OK name=&q...
When to Use   XML? <ul><li>Use XML : </li></ul><ul><ul><li>For information exchange between different systems </li></ul></...
XML Disadvantages <ul><li>XML disadvantages: </li></ul><ul><ul><li>Data is more massive (take more space) </li></ul></ul><...
Namespaces
Namespaces <ul><li>Namespaces in XML documents add the possibility to use different tags with the same name : </li></ul><?...
Namespaces - Example <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <country:towns  xmlns:country=&quot;urn:na...
Namespaces (2) <ul><li>It is possible to use default namespaces </li></ul><?xml version=&quot;1.0&quot; encoding=&quot;win...
XML Schemes and Validation
Schemes and Validation <ul><li>The XML documents structure is defined  by schemes </li></ul><ul><li>Schemes describes </li...
The   DTD Language <ul><li>DTD  ( Document Type Definition )  is : </li></ul><ul><ul><li>Formal language for describing XM...
The   DTD Language (2) <ul><li>DTD Example: </li></ul><!ELEMENT library (book+)> <!ATTLIST library name CDATA #REQUIRED > ...
XSD Schemes <ul><li>XSD (XML Scheme Definition Language) is: </li></ul><ul><ul><li>Powerful XML-based language for describ...
XSD Scheme Example <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <xs:schema   xmlns:xs=&quot;http://www.w3.or...
XSD Scheme Example (2) <xs:element name=&quot;book&quot;> <xs:complexType> <xs:sequence> <xs:element ref=&quot;title&quot;...
XPath
XPath <ul><li>XPath (XML Path Language) is a language for addressing parts of XML documents </li></ul><ul><li>XPath expres...
XPath   Expressions <ul><li>/  – addresses the root element </li></ul><ul><li>/someNode  – addresses all nodes with name &...
XSL
XSL for Cross - Media Publishing PDF as  digital preprint HTML a s  standard  I nternet  F ormat For Online  P ublishing A...
XSL Transformation Language (XSLT)
What is XSLT? <ul><li>XSLT stands for XSL Transformations </li></ul><ul><li>XSLT transforms an XML document into another  ...
XSL/XSLT Capabilities  <ul><li>Add prefix or suffix text to content </li></ul><ul><li>Remove, create, re-order and sort el...
Browsers Support <ul><li>Note:  Not all browsers  have full support for XML and XSLT  </li></ul><ul><li>Internet Explorer ...
Simple XML <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <catalog> <cd> <title> Evergreens </title> <art...
Create an XSL Style Sheet <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <xsl:stylesheet version=&quot;1....
Link the XSL Style Sheet to the XML Document <ul><li>A XML document can be linked explicitly to a XSL transformation docum...
Output Format Control <xsl:stylesheet xmlns:xsl= . . .> <xsl:output method= &quot; html &quot;   encoding= &quot; UTF-8 &q...
XSLT Specification <ul><li>Available from  http://www.w3.org/XSL/ </li></ul><ul><ul><li>Defines 34 elements and their attr...
Template Element <ul><li>Template element specifies transformation rule </li></ul><ul><li>The match attribute takes XPath ...
Import Element <ul><li>Use import element to include multiple XSLT files </li></ul><stylesheet … >   < import  href=&quot;...
The <xsl:value-of> Element <ul><li>U sed to extract the value of an XML element  </li></ul>Note: The value of the  select ...
The <xsl:for-each> Element <ul><li>U sed to select every XML element of a specified node-set  (in a loop) </li></ul>Note: ...
The <xsl:for-each> Element  Filter output <ul><li>We can also filter the output from the XML </li></ul><xsl:for-each selec...
XSLT <xsl:sort> Element <ul><li>U sed to sort the output </li></ul><ul><ul><li>Add this element  inside the  <xsl:for-each...
Sort Element Properties <ul><li>The ' order ' attribute </li></ul><ul><ul><li>' ascending ' or ' descending ' </li></ul></...
The <xsl:if> Element <ul><li>U sed to put a conditional test against  the content of the XML file </li></ul>Note:  The val...
The <xsl:choose> Element <ul><li>U sed in conjunction with  </li></ul><ul><ul><li><xsl:when>  and  <xsl:otherwise> </li></...
The  <xsl:apply-templates>   Element <ul><li>A pplies a template to the current element or to the current  element's  chil...
Variable Element <ul><li>Variables may be declared and used in XSLT </li></ul><ul><li>Variable is referenced with  $  nota...
Reusing Templates <ul><li>If same formatting is required many times </li></ul>//   A named template called by call-templat...
Attribute Values <ul><li>Use ' value-of ' element to access attributes </li></ul><ul><li>Attributes identified by ' @ ' pr...
Creating Elements <ul><li>Use ' Element ' tag to create new elements </li></ul><ul><li>Most powerful with use of variables...
XSLT Live Demo
Introduction to XML <ul><li>Questions? </li></ul>
Exercises <ul><li>What does the XML language represents ?  What is it used for?   When do we use it ? </li></ul><ul><li>Cr...
Exercises (2) <ul><li>Add default namespace for the students  &quot; urn:students &quot; . </li></ul><ul><li>Write an XML ...
Exercises (3) <ul><li>Write your CV in XML format. It should have the following structure: </li></ul><ul><ul><li>Personal ...
Upcoming SlideShare
Loading in...5
×

Introduction to XML

3,388

Published on

Introduction to XML - http://javaeecourse.devg.org

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,388
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
223
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (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.
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (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.
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • (c) 2007 National Academy for Software Development - http://academy.devbg.org All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.* ##
  • Introduction to XML

    1. 1. Introduction to XML Svetlin Nakov Bulgarian Association of Software Developers www.devbg.org
    2. 2. Contents <ul><li>What is XML? </li></ul><ul><li>XML and HTML </li></ul><ul><li>When to use XML? </li></ul><ul><li>Namespaces </li></ul><ul><li>Schemes and validation – DTD and XSD schemes </li></ul><ul><li>XPath Language </li></ul><ul><li>XSL </li></ul><ul><li>XSL transformations </li></ul>
    3. 3. What is XML
    4. 4. What is XML? <ul><li>Universal language (notation) for describing structured data </li></ul><ul><li>The data is stored together with the meta-information about it </li></ul><ul><li>Looks like HTML – text based, uses tags and attributes </li></ul><ul><li>It is used to describe other languages (formats) for data representation </li></ul>
    5. 5. What is XML? (2) <ul><li>Worldwide-affirmative standard, supported by the W3C (www.w3c.org) </li></ul><ul><li>Platform, programming languages and OS independent </li></ul>
    6. 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. 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. 8. XML and HTML <ul><li>Likenesses between XML and HTML : </li></ul><ul><ul><li>Both are text based </li></ul></ul><ul><ul><li>Both use tags and attributes </li></ul></ul><ul><li>Differences between XML and HTML: </li></ul><ul><ul><li>HTML is a language, and XML is a syntax for describing other languages </li></ul></ul><ul><ul><li>HTML describes the formatting of information, and XML describes structured information </li></ul></ul><ul><ul><li>XML requires the documents to be well-formed </li></ul></ul>
    9. 9. Well-Formed Documents <ul><li>XML requires the documents to be well-formed </li></ul><ul><ul><li>Tags should always be closed in the right order </li></ul></ul><ul><ul><li>Attributes should always be closed </li></ul></ul><ul><ul><li>The document should contain only one root element </li></ul></ul><ul><ul><li>There are restrictions of the tag and attribute names </li></ul></ul>
    10. 10. Well-Formed Documents (2) <ul><li>Example of bad-defined XML document </li></ul>< xml> <button bug! value=&quot;OK name=&quot;b1&quot;> <animation source=&quot;demo1.avi&quot;> 1 < 2 < 3 </click-button> < / xml >
    11. 11. When to Use XML? <ul><li>Use XML : </li></ul><ul><ul><li>For information exchange between different systems </li></ul></ul><ul><ul><li>For storing structured data </li></ul></ul><ul><ul><li>To create custom-defined languages for describing information </li></ul></ul><ul><ul><li>Configuration data </li></ul></ul>
    12. 12. XML Disadvantages <ul><li>XML disadvantages: </li></ul><ul><ul><li>Data is more massive (take more space) </li></ul></ul><ul><ul><li>Decreased performance </li></ul></ul><ul><ul><li>Frequently there is a need of more memory </li></ul></ul><ul><ul><li>Increased network traffic </li></ul></ul>
    13. 13. Namespaces
    14. 14. Namespaces <ul><li>Namespaces in XML documents add the possibility to use different tags with the same name : </li></ul><?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. 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. 16. Namespaces (2) <ul><li>It is possible to use default namespaces </li></ul><?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. 17. XML Schemes and Validation
    18. 18. Schemes and Validation <ul><li>The XML documents structure is defined by schemes </li></ul><ul><li>Schemes describes </li></ul><ul><ul><li>Possible tags </li></ul></ul><ul><ul><li>Possible attributes </li></ul></ul><ul><ul><li>Possible values for the attributes and elements </li></ul></ul><ul><ul><li>Order of the tags </li></ul></ul><ul><ul><li>Default values </li></ul></ul>
    19. 19. The DTD Language <ul><li>DTD ( Document Type Definition ) is : </li></ul><ul><ul><li>Formal language for describing XML document structures </li></ul></ul><ul><ul><li>Contains sum of rules for the tags and their attributes in a document </li></ul></ul><ul><ul><li>Text-based language, but not XML based </li></ul></ul><ul><ul><li>Rarely used because it is substituted by XSD </li></ul></ul>
    20. 20. The DTD Language (2) <ul><li>DTD Example: </li></ul><!ELEMENT library (book+)> <!ATTLIST library name CDATA #REQUIRED > <!ELEMENT book (title, author, isbn)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT isbn (#PCDATA)>
    21. 21. XSD Schemes <ul><li>XSD (XML Scheme Definition Language) is: </li></ul><ul><ul><li>Powerful XML-based language for describing the structure of XML documents </li></ul></ul><ul><ul><li>Contains sum of rules for the tags and their attributes is a document </li></ul></ul><ul><li>XSD schemes have bigger descriptive power than the DTD </li></ul><ul><li>XSD consequently replaces DTD </li></ul>
    22. 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. 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. 24. XPath
    25. 25. XPath <ul><li>XPath (XML Path Language) is a language for addressing parts of XML documents </li></ul><ul><li>XPath expressions contains description of paths to nodes and criteria, which the nodes should pass </li></ul><ul><li>XPath is frequently used with XSLT and XPointer </li></ul><ul><li>Example for XPath expressions: </li></ul>/library/book[isbn= ' 1-930110-19-7 ' ] /catalog/cd[ @ price < 10.80]
    26. 26. XPath Expressions <ul><li>/ – addresses the root element </li></ul><ul><li>/someNode – addresses all nodes with name &quot; someNode &quot;, direct inheritors to the root </li></ul><ul><li>/books/book – addresses all nodes &quot; book &quot; , direct inheritors to the node &quot; books &quot; </li></ul><ul><li>/books/book[price<&quot;10&quot;]/author – addresses all authors ( /books/book/ author ), whose books have price < &quot; 10 &quot; </li></ul><ul><li>/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 </li></ul>
    27. 27. XSL
    28. 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. 29. XSL Transformation Language (XSLT)
    30. 30. What is XSLT? <ul><li>XSLT stands for XSL Transformations </li></ul><ul><li>XSLT transforms an XML document into another text format document </li></ul><ul><ul><li>U ses XPath to navigate in the XML document </li></ul></ul><ul><li>XSLT is a W3C Recommendation </li></ul>
    31. 31. XSL/XSLT Capabilities <ul><li>Add prefix or suffix text to content </li></ul><ul><li>Remove, create, re-order and sort elements </li></ul><ul><li>Re-use elements elsewhere in the document </li></ul><ul><li>Transform data between XML formats </li></ul><ul><li>Specify the XSL formatting objects to be applied to each element class </li></ul><ul><li>Uses a recursive mechanism to go through document </li></ul>
    32. 32. Browsers Support <ul><li>Note: Not all browsers have full support for XML and XSLT </li></ul><ul><li>Internet Explorer 6 – full support </li></ul><ul><li>Mozilla 1.7.8 - is available with an XSLT implementation </li></ul><ul><li>Netscape 8 - based on the Mozilla engine. Same XML / XSLT support as Mozilla </li></ul><ul><li>Opera 8 - supports XML + CSS </li></ul>
    33. 33. 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
    34. 34. 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 )
    35. 35. Link the XSL Style Sheet to the XML Document <ul><li>A XML document can be linked explicitly to a XSL transformation document </li></ul><ul><li>Use processing instruction <?xml-stylesheet ?> </li></ul><?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. 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> <ul><li>Define output as XML, HTML, or text </li></ul><ul><li>Can specify the character encoding </li></ul><ul><li>Control the XML declaration and DOCTYPE </li></ul>
    37. 37. XSLT Specification <ul><li>Available from http://www.w3.org/XSL/ </li></ul><ul><ul><li>Defines 34 elements and their attributes </li></ul></ul><ul><ul><li>Can build useful style sheets using </li></ul></ul><ul><ul><ul><li>stylesheet </li></ul></ul></ul><ul><ul><ul><li>template </li></ul></ul></ul><ul><ul><ul><li>apply-templates </li></ul></ul></ul><ul><ul><li>Depends on the XPath specification </li></ul></ul>
    38. 38. Template Element <ul><li>Template element specifies transformation rule </li></ul><ul><li>The match attribute takes XPath expressions </li></ul><ul><li>If more than one XPath matches – then use priority rules to determine which is used </li></ul><xsl:template match=&quot;/&quot;> … </xsl:template>
    39. 39. Import Element <ul><li>Use import element to include multiple XSLT files </li></ul><stylesheet … > < import href=&quot;tables.xsl&quot;/> < import href=&quot;features.xsl&quot;> <template … > … </template> … </stylesheet>
    40. 40. The <xsl:value-of> Element <ul><li>U sed to extract the value of an XML element </li></ul>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. 41. The <xsl:for-each> Element <ul><li>U sed to select every XML element of a specified node-set (in a loop) </li></ul>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. 42. The <xsl:for-each> Element Filter output <ul><li>We can also filter the output from the XML </li></ul><xsl:for-each select=&quot;catalog/cd[artist='Bob Dylan ']&quot;> <ul><li>We use expressions in square brackets [ ... ] for setting conditions </li></ul><ul><li>Legal operator are: </li></ul><ul><ul><li>=  (equal) </li></ul></ul><ul><ul><li>!= (not equal) </li></ul></ul><ul><ul><li>&lt; less than </li></ul></ul><ul><ul><li>&gt; greater than </li></ul></ul>
    43. 43. XSLT <xsl:sort> Element <ul><li>U sed to sort the output </li></ul><ul><ul><li>Add this element inside the <xsl:for-each> element </li></ul></ul>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. 44. Sort Element Properties <ul><li>The ' order ' attribute </li></ul><ul><ul><li>' ascending ' or ' descending ' </li></ul></ul><ul><li>The ' data-type ' attribute </li></ul><ul><ul><li>' text ' (default) or ' number ' </li></ul></ul><ul><li>The ' case-order ' attribute </li></ul><ul><ul><li>' lower-first ' or ' upper-first ' </li></ul></ul><xsl:sort select=&quot; product/price &quot; data-type=&quot;number&quot; order=&quot;descending&quot; />
    45. 45. The <xsl:if> Element <ul><li>U sed to put a conditional test against the content of the XML file </li></ul>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. 46. The <xsl:choose> Element <ul><li>U sed in conjunction with </li></ul><ul><ul><li><xsl:when> and <xsl:otherwise> </li></ul></ul><ul><ul><li>t o express multiple conditional tests </li></ul></ul><xsl:choose> <xsl:when test=&quot;XPath expression &quot;> ... some output ... </xsl:when> <xsl:otherwise> ... some output .... </xsl:otherwise> </xsl:choose>
    47. 47. The <xsl:apply-templates> Element <ul><li>A pplies a template to the current element or to the current element's child nodes </li></ul><ul><ul><li>S elect attribute – will process only the child element that matches the value of the attribute </li></ul></ul>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. 48. Variable Element <ul><li>Variables may be declared and used in XSLT </li></ul><ul><li>Variable is referenced with $ notation </li></ul><ul><ul><li><xsl:variable name=&quot;colour&quot;>red</xsl:variable> </li></ul></ul><ul><ul><li><xsl:value-of select=&quot;$colour&quot;/> </li></ul></ul>
    49. 49. Reusing Templates <ul><li>If same formatting is required many times </li></ul>// 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. 50. Attribute Values <ul><li>Use ' value-of ' element to access attributes </li></ul><ul><li>Attributes identified by ' @ ' prefix </li></ul><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. 51. Creating Elements <ul><li>Use ' Element ' tag to create new elements </li></ul><ul><li>Most powerful with use of variables </li></ul><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. 52. XSLT Live Demo
    53. 53. Introduction to XML <ul><li>Questions? </li></ul>
    54. 54. Exercises <ul><li>What does the XML language represents ? What is it used for? When do we use it ? </li></ul><ul><li>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). </li></ul><ul><li>What does the namespaces represent in the XML documents? What are they used for? When do we used them? </li></ul>
    55. 55. Exercises (2) <ul><li>Add default namespace for the students &quot; urn:students &quot; . </li></ul><ul><li>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. </li></ul><ul><li>Write an XSL stylesheet to transform the XML file to a human readable XHTML document. Sort the products in alphabetical order. </li></ul><ul><li>Test the produced XHTML file in your Web browser. </li></ul>
    56. 56. Exercises (3) <ul><li>Write your CV in XML format. It should have the following structure: </li></ul><ul><ul><li>Personal information (name, DOB, ...) </li></ul></ul><ul><ul><li>Education </li></ul></ul><ul><ul><li>Skills </li></ul></ul><ul><ul><li>Work experience </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><li>Write a XSL stylesheet for transforming the CV to HTML and XML with other structure. </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×