Your SlideShare is downloading. ×
0
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

5 xsl (formatting xml documents)

1,558

Published on

XSL - Formatting XML Documents

XSL - Formatting XML Documents

Published in: Education, Technology
1 Comment
0 Likes
Statistics
Notes
  • Good information about style sheets..
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total Views
1,558
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
82
Comments
1
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Extensible Stylesheet Language (XSL) Atul Kahate [email_address]
  • 2. Agenda <ul><li>Stylesheets </li></ul><ul><li>Cascading Style Sheets (CSS) </li></ul><ul><li>Extensible Stylesheet Language (XSL) </li></ul><ul><li>XSL Transformations (XSLT) </li></ul>
  • 3. Style Sheets Basics <ul><li>XML concentrates on the structure of information </li></ul><ul><li>Normally, the appearance of information is secondary </li></ul><ul><li>Sometimes, it is necessary to format XML information so as to view it in a particular manner </li></ul><ul><li>This is called as styling an XML document </li></ul>
  • 4. Style Sheets <ul><li>Formatting instructions for XML documents are organized/grouped </li></ul><ul><li>These organized instructions are called as style sheets </li></ul><ul><li>A style sheet can be applied to view a particular XML document in a specific format </li></ul>
  • 5. Standards For XML Styling <ul><li>Two main standards </li></ul><ul><li>Developed by W3C </li></ul><ul><ul><li>CSS (Cascading Style Sheet) </li></ul></ul><ul><ul><li>XSL (XML Stylesheet Language) </li></ul></ul>
  • 6. CSS versus XSL <ul><li>CSS Approach </li></ul><ul><li>XSL Approach </li></ul>XML Document CSS Style Sheet XML Document XSL Style Sheet HTML Document
  • 7. CSS (Cascading Style Sheet) <ul><li>Originally developed for HTML </li></ul><ul><li>Browsers that support XML also work well with CSS </li></ul><ul><li>CSS </li></ul><ul><ul><li>Set of rules that tells the browser about display information </li></ul></ul><ul><ul><li>e.g. which fonts to use, what size, style, margins, etc </li></ul></ul>
  • 8. Need for CSS <ul><li>HTML is a display-oriented language </li></ul><ul><li>CSS was still developed in addition to HTML -- Why? </li></ul><ul><li>HTML became quite complex over the years </li></ul><ul><li>Adding more and more elements to HTML was not a viable option </li></ul><ul><li>CSS was developed to separate HTML elements from precisely how they should be styled </li></ul>
  • 9. CSS Terminology <ul><li>Style sheets are used to define rules </li></ul><ul><li>A rule is composed of two parts </li></ul><ul><ul><li>Selector (Element to which the rule applies) </li></ul></ul><ul><ul><li>Declaration (Attributes and values) </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>PARA {FONT: 12pt “Times New Roman”} </li></ul></ul><ul><ul><li>Here, PARA is the selector, the rest is the declaration </li></ul></ul>
  • 10. Example of CSS Syntax <ul><li>section.title </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times New Roman”, serif; </li></ul><ul><li>font-size: 10pt; </li></ul><ul><li>margin: 5px; </li></ul><ul><li>display: block; </li></ul><ul><li>font-style: italic </li></ul><ul><li>} </li></ul>
  • 11. DTD and Stylesheet Example - 1 <ul><li>Consider a MEMO DTD </li></ul><ul><li>… </li></ul><ul><li>&lt;!DOCTYPE MEMODTD [ </li></ul><ul><li>&lt;!ELEMENT MEMO (TO, FROM, SUBJECT, BODY)&gt; </li></ul><ul><li>&lt;!ELEMENT TO (#PCDATA)&gt; </li></ul><ul><li>&lt;!ELEMENT FROM (#PCDATA)&gt; </li></ul><ul><li>&lt;!ELEMENT SUBJECT (#PCDATA)&gt; </li></ul><ul><li>&lt;!ELEMENT BODY (#PCDATA)&gt; </li></ul><ul><li>]&gt; </li></ul>
  • 12. DTD and Stylesheet Example - 2 <ul><li>Suppose we want to do the following </li></ul><ul><ul><li>Display the elements TO, FROM, and SUBJECT in bold along with their contents </li></ul></ul><ul><ul><li>Display the entire memo in Times New Roman font with size as 12 points, and margin of 1 line </li></ul></ul><ul><li>Use the stylesheet as follows </li></ul>
  • 13. DTD and Stylesheet Example - 3 <ul><li>CSS declaration follows </li></ul><ul><li>… </li></ul><ul><li>MEMO {font-family: “Times New Roman”, “Book Antigua”, serif; </li></ul><ul><li>font-size: 12pt; </li></ul><ul><li>margin: lin </li></ul><ul><li>} </li></ul><ul><li>TO, FROM, SUBJECT {font-weight: bold} </li></ul>
  • 14. Sample XML Document <ul><li>&lt;?xml version=“1.0”?&gt; </li></ul><ul><li>&lt;?xml-stylesheet href=“article.css” type=“text/css”?&gt; </li></ul><ul><li>&lt;article&gt; </li></ul><ul><li>&lt;title&gt; Style Sheets Example &lt;/title&gt; </li></ul><ul><li>&lt;section&gt; </li></ul><ul><li>&lt;p&gt; This example illustrates how style sheets can be applied to XML documents. &lt;/p&gt; </li></ul><ul><li>&lt;/section&gt; </li></ul><ul><li>&lt;section&gt; </li></ul><ul><li>&lt;title&gt; Styling &lt;/title&gt; </li></ul><ul><li>&lt;p&gt; Style sheets format XML documents in a desired manner. &lt;/p&gt; </li></ul><ul><li>&lt;/section&gt; </li></ul><ul><li>&lt;/article&gt; </li></ul>
  • 15. Corresponding CSS Document <ul><li>/* a simple style sheet */ </li></ul><ul><li>article </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times New Roman”, serif; </li></ul><ul><li>font-size: 18pt; </li></ul><ul><li>margin: 5px </li></ul><ul><li>} </li></ul><ul><li>article, p, title </li></ul><ul><li>{ </li></ul><ul><li>display: block; </li></ul><ul><li>margin-bottom: 10px </li></ul><ul><li>} </li></ul><ul><li>article title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 24pt; </li></ul><ul><li>font-weight: bold </li></ul><ul><li>} </li></ul><ul><li>section title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 20pt; </li></ul><ul><li>font-style: italic </li></ul><ul><li>} </li></ul>
  • 16. Output in Web Browser
  • 17. Understanding CSS Example - 1 <ul><li>/* a simple style sheet */ </li></ul><ul><li>article </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times New Roman”, serif; </li></ul><ul><li>font-size: 18pt; </li></ul><ul><li>margin: 5px; </li></ul><ul><li>} </li></ul><ul><li>Comments are enclosed between /* and */ </li></ul><ul><li>The above declaration specifies the formatting of the entire contents of the article element </li></ul>
  • 18. Understanding CSS Example - 2 <ul><li>article, p, title </li></ul><ul><li>{ </li></ul><ul><li>display: block; </li></ul><ul><li>margin-bottom: 10px; </li></ul><ul><li>} </li></ul><ul><li>In addition to the basic definition applied to the article element, this defines more styles for article , p , and title elements </li></ul>
  • 19. Understanding CSS Example - 3 <ul><li>article title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 24pt; </li></ul><ul><li>font-weight: bold; </li></ul><ul><li>} </li></ul><ul><li>section title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 20pt; </li></ul><ul><li>font-style: italic; </li></ul><ul><li>} </li></ul><ul><li>Specify attributes for the title element within article and within section separately </li></ul>
  • 20. Exercise <ul><li>Consider employee information to be stored in the following format in an XML file: </li></ul><ul><ul><li>Employee ID </li></ul></ul><ul><ul><li>Employee Name </li></ul></ul><ul><ul><li>Salary </li></ul></ul><ul><ul><li>Department </li></ul></ul><ul><li>Create CSS to use different fonts, font sizes, font colors as per your choice </li></ul>
  • 21. Introduction to XSLT Concepts
  • 22. Programming Language Classification <ul><li>Imperative Languages </li></ul><ul><ul><li>Set some variables, call methods, use operators that change value, etc </li></ul></ul><ul><ul><li>C, C++, Java, C# </li></ul></ul><ul><li>Declarative or Functional Languages </li></ul><ul><ul><li>Perform logical operations using declarations, rather than writing code </li></ul></ul><ul><ul><li>Prolog, XSLT </li></ul></ul>
  • 23. Recursion <ul><li>Using recursion, imperative languages such as Java can behave like declarative languages such as XSLT </li></ul><ul><li>Example </li></ul><ul><ul><li>public int factorial(int number) { </li></ul></ul><ul><ul><li>if (number &lt;= 1) return 1; </li></ul></ul><ul><ul><li>return number * factorial(number-1); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  • 24. Recursion Illustrated
  • 25. Recursion and XSL: When? <ul><li>When we have a set of repeating values in the source XML and we want the transformation result to reflect something about all of those values. For example, if you have a catalog of items in XML and want to present those items along with the price for all of the items, you would have to find that total price using a recursive template. </li></ul><ul><li>When the source XML contains a number x in a tag, for example &lt;countTo number=&amp;quot;5&amp;quot;/&gt;, and you want to present some information that same x number of times in the transformation output. </li></ul>
  • 26. XSL Terminologies XSL, XSLT, XSLFO, XPath, XPointer, Stylesheet, Template, …
  • 27. XSL (XML Stylesheet Language) <ul><li>Two parts </li></ul><ul><ul><li>XSLT (XSL Transformation) </li></ul></ul><ul><ul><li>XSL-FO (XSL Formatting Objects) </li></ul></ul><ul><li>XSLFO is similar to CSS, quite complex </li></ul><ul><li>We will discuss XSLT in detail, XSL-FO in brief </li></ul>
  • 28. XPath <ul><li>Allows searching and navigation of XML documents </li></ul><ul><li>Can specify which parts of an XML document we want to transform </li></ul><ul><li>Used heavily in XSLT for searching of information </li></ul>
  • 29. XSLT Usage <ul><li>Styling </li></ul><ul><li>Add elements specific to viewing (e.g. logo) </li></ul><ul><li>Create new content from existing one (e.g. TOC) </li></ul><ul><li>Present information with the right level of details (e.g. overview for managers, details for staff) </li></ul><ul><li>Convert between different DTDs/schemas or different versions of a DTD/schema </li></ul><ul><li>Transform XML documents into HTML for compatibility with older browsers </li></ul>
  • 30. XSLT Stylesheets <ul><li>An XSLT stylesheet consists of a series of templates , together with instructions based on XPath </li></ul><ul><li>Tell an XSLT processor how to match the template against the nodes in an XML input document </li></ul><ul><li>For each template, the processor reads the input document for all matching patterns and produces an output document </li></ul><ul><li>See next slide </li></ul>
  • 31. XSLT Processing Concept Input XML document XSLT Processor Output XML document XSLT Template XSLT Stylesheet
  • 32. XSLT Basics
  • 33. Simple XSLT Example <ul><li>XML (test.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;title&gt;XSL&lt;/title&gt; </li></ul></ul><ul><ul><li>&lt;author&gt;John Smith&lt;/author&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSLT (test.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;H1&gt;&lt;xsl:value-of select=&amp;quot;//title&amp;quot;/&gt;&lt;/H1&gt; </li></ul></ul><ul><ul><li>&lt;H2&gt;&lt;xsl:value-of select=&amp;quot;//author&amp;quot;/&gt;&lt;/H2&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 34. Change to the XML <ul><li>The XML document has an &lt; xml-stylesheet&gt; tag, which informs the parser that we want to use an XSLT stylesheet to process this XML file before displaying its contents </li></ul>
  • 35. Now look at the XSL <ul><li>The XSLT stylesheet is also a well-formed XML document </li></ul><ul><li>The &lt;xsl:stylesheet&gt; element has two attributes </li></ul><ul><ul><li>Version specifies the XSLT specifications version </li></ul></ul><ul><ul><li>Declares the namespace </li></ul></ul>
  • 36. Question <ul><li>What if our XML document has multiple occurrences of the title and author tags? </li></ul><ul><ul><li>We would still see only the first occurrence, since we have not yet seen the recursion part of XSLT </li></ul></ul>
  • 37. Modified XSL <ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; &gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;H2&gt;&lt;xsl:value-of select=&amp;quot;//author&amp;quot;/&gt;&lt;/H2&gt; </li></ul><ul><li>&lt;H1&gt;&lt;xsl:value-of select=&amp;quot;//title&amp;quot;/&gt;&lt;/H1&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 38. Interesting Tricks – 1 <ul><li>XML (trick-1.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=“trick-1.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;title&gt;XSL&lt;/title&gt; </li></ul></ul><ul><ul><li>&lt;author&gt;John Smith&lt;/author&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL (trick-1.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul><ul><li>What would be the output? </li></ul>
  • 39. Answer <ul><li>The full XML contents </li></ul><ul><li>Why? </li></ul><ul><ul><li>If no template is specified, XSLT produces the complete XML as output! </li></ul></ul>
  • 40. Interesting Tricks – 2 <ul><li>XML (trick-2.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=“trick-2.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;title&gt;XSL&lt;/title&gt; </li></ul></ul><ul><ul><li>&lt;author&gt;John Smith&lt;/author&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL (trick-2.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=“/”&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul><ul><li>What would be the output? </li></ul>
  • 41. Answer <ul><li>Now we have said, match root, but once root is matched, we say do nothing (since there is nothing between &lt;xsl:template match = “/” and &lt;/xsl:template&gt; tags </li></ul><ul><li>Hence, output is empty </li></ul>
  • 42. Interesting Tricks – 3 <ul><li>XML (trick-3.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=“trick-3.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;title&gt;XSL&lt;/title&gt; </li></ul></ul><ul><ul><li>&lt;author&gt;John Smith&lt;/author&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL (trick-3.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=“/”&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select = “title” /&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul><ul><li>What would be the output? </li></ul>
  • 43. Answer <ul><li>Now, we try to find a match on the title element inside /. </li></ul><ul><li>But the path for title should be /xslTutorial/title </li></ul><ul><li>Hence, output would be empty </li></ul>
  • 44. Interesting Tricks – 4 <ul><li>XML (trick-4.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=“trick-4.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;title&gt;XSL&lt;/title&gt; </li></ul></ul><ul><ul><li>&lt;author&gt;John Smith&lt;/author&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL (trick-4.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=“/”&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select = “/xslTutorial/title” /&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul><ul><li>What would be the output? </li></ul>
  • 45. Answer <ul><li>It would produce the contents of the title element, as expected, now </li></ul>
  • 46. Template Basics
  • 47. Usage of Templates <ul><li>&lt;xsl:template match = “…”&gt; </li></ul><ul><ul><li>We know that this clause is used to match a particular tag from an XML file and to do processing, accordingly </li></ul></ul><ul><li>&lt;xsl:template name = “…”&gt; </li></ul><ul><ul><li>Allows us to define a template </li></ul></ul><ul><ul><li>Once such a template is defined, we can use &lt;xsl:call-template name = “…”&gt; to call that defined template </li></ul></ul>
  • 48. Understanding &lt;apply-templates&gt; <ul><li>class.xml </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class.xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;class&gt; </li></ul></ul><ul><ul><li>&lt;student&gt;Jack&lt;/student&gt; </li></ul></ul><ul><ul><li>&lt;student&gt;Harry&lt;/student&gt; </li></ul></ul><ul><ul><li>&lt;student&gt;Rebecca&lt;/student&gt; </li></ul></ul><ul><ul><li>&lt;teacher&gt;Mr. Bean&lt;/teacher&gt; </li></ul></ul><ul><ul><li>&lt;/class&gt; </li></ul></ul><ul><li>Class.xsl </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=“student&amp;quot;&gt; Found a learner! </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul><ul><li>Output </li></ul><ul><ul><li>Found a learner! Found a learner! Found a learner! Mr. Bean </li></ul></ul>
  • 49. How it Works? &lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; &lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class.xsl&amp;quot;?&gt; &lt;class&gt; &lt;student&gt;Jack&lt;/student&gt; &lt;student&gt;Harry&lt;/student&gt; &lt;student&gt;Rebecca&lt;/student&gt; &lt;teacher&gt;Mr. Bean&lt;/teacher&gt; &lt;/class&gt; &lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; &lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; &lt;xsl:template match=&amp;quot;student&amp;quot;&gt; Found a learner! &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; Step 1: For all matching student tags, display output Found a learner! Step 2: Come here and run the template recursively for all the student tags, displaying Found a learner! Step 3: For all tags other than student , there is no template in our XSL. Therefore, blindly output them, as they are!
  • 50. Plain English Version <ul><li>For all the elements in the given XML </li></ul><ul><ul><li>If the current element = “student” </li></ul></ul><ul><ul><ul><li>Display “Found a learner” </li></ul></ul></ul><ul><ul><li>Else </li></ul></ul><ul><ul><ul><li>Display the actual contents of the element </li></ul></ul></ul><ul><ul><li>End-if </li></ul></ul><ul><li>End-For </li></ul>
  • 51. Explanation <ul><li>The way this works is: </li></ul><ul><ul><li>Use a template if one defined </li></ul></ul><ul><ul><li>Blindly output the contents of the elements wherever there is no template defined </li></ul></ul><ul><li>For each &lt;template match = “student”&gt;, we display the text Found a learner! instead of the student name itself. </li></ul><ul><li>However, for the teacher tag, there is no &lt;template match&gt;. Therefore, its contents are displayed as they are. </li></ul>
  • 52. Notes on the Result – 1 <ul><li>Note that we would see display for all the student tags </li></ul><ul><li>However, in the first example, we would have seen the output only for the first instance of title and author tags </li></ul><ul><li>Why? </li></ul><ul><ul><li>In the earlier example, there was no template , i.e. no recursion – the syntax used was value-of select </li></ul></ul><ul><ul><li>Now, we use a template , which introduces recursion </li></ul></ul>
  • 53. Notes on the Result – 2 <ul><li>This style of coding is ambiguous! </li></ul><ul><li>It can lead to completely unexpected results </li></ul><ul><li>Let us modify our XML and XSL now </li></ul>
  • 54. Modified XML and XSL <ul><li>class1.xml </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; class &gt; </li></ul><ul><li>&lt; dept &gt; Bye &lt;/ dept &gt; </li></ul><ul><li>&lt; salary &gt; 10000 &lt;/ salary &gt; </li></ul><ul><li>&lt;/ class &gt; </li></ul><ul><li>class1.xsl </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; hello &amp;quot;&gt; Found a learner! &lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul><ul><li>What would be the output? </li></ul>
  • 55. Output <ul><li>Bye10000 </li></ul><ul><li>Why? </li></ul><ul><ul><li>The attempt is to find a match for the tag or element hello in our XML document, which is not found </li></ul></ul><ul><ul><li>For other tags in the XML document (i.e. dept and salary), there is no template defined; so no special processing is needed for them, except blindly outputting their contents, as before! </li></ul></ul>
  • 56. Further Modifications <ul><li>Modified XML </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; class &gt; </li></ul><ul><li>&lt; dept &gt; Bye &lt;/ dept &gt; </li></ul><ul><li>&lt; salary &gt; 10000 &lt;/ salary &gt; </li></ul><ul><li>&lt; name &gt; </li></ul><ul><li>&lt; first &gt; test &lt;/ first &gt; </li></ul><ul><li>&lt; last &gt; test &lt;/ last &gt; </li></ul><ul><li>&lt;/ name &gt; </li></ul><ul><li>&lt;/ class &gt; </li></ul><ul><li>Resulting Output </li></ul><ul><li>Bye10000testtest </li></ul><ul><li>This is based on same logic as earlier </li></ul>
  • 57. Still More Changes <ul><li>class3.xml </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; class &gt; </li></ul><ul><li>&lt; dept &gt; Bye &lt;/ dept &gt; </li></ul><ul><li>&lt; salary &gt; 10000 &lt;/ salary &gt;&lt;/ class &gt; </li></ul><ul><li>class3.xsl </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; dept &amp;quot;&gt; </li></ul><ul><li>Found a learner! </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; salary &amp;quot;&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 58. Output <ul><li>Found a learner! </li></ul><ul><li>This is because we have suppressed the output for the salary tag now </li></ul>
  • 59. Introducing apply-templates
  • 60. Use of &lt;apply-templates&gt; - Tricky <ul><li>class2.xml </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; </li></ul><ul><li>href=&amp;quot;class2.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;class&gt; </li></ul><ul><li>&lt;college&gt;test&lt;/college&gt; </li></ul><ul><li>&lt;dept&gt;one&lt;/dept&gt; </li></ul><ul><li>&lt;salary&gt;10000&lt;/salary&gt; </li></ul><ul><li>&lt;dept&gt;two&lt;/dept&gt; </li></ul><ul><li>&lt;salary&gt;20000&lt;/salary&gt; </li></ul><ul><li>&lt;dept&gt;three&lt;/dept&gt; </li></ul><ul><li>&lt;salary&gt;30000&lt;/salary&gt;&lt;/class&gt; </li></ul><ul><li>Class2.xsl </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =“ dept &amp;quot;&gt; &lt;xsl:apply-templates /&gt; &lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 61. Output <ul><li>Testone10000two20000three30000 </li></ul><ul><li>Reason: The logic works as: </li></ul><ul><ul><li>If there is at least one dept tag </li></ul></ul><ul><ul><ul><li>&lt;apply-templates&gt; (which means, display the default output as it is, which means everything) </li></ul></ul></ul>
  • 62. Suppressing Unwanted Output <ul><li>Modified XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; dept &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; salary &amp;quot;&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 63. Controlling the Output the Way We Want
  • 64. XSL Changed – 1 <ul><li>Now changes the XSL to this: </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;class&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;student&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;student&amp;quot;&gt; </li></ul><ul><li>Found a learner! </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul><ul><li>What would be the output? See next slide. </li></ul>
  • 65. Plain English Version <ul><li>For each element in the given XML </li></ul><ul><ul><li>If the current element is class </li></ul></ul><ul><ul><ul><li>If the child element of the current element is student </li></ul></ul></ul><ul><ul><ul><ul><li>Output Found a learner </li></ul></ul></ul></ul><ul><ul><ul><li>End-if </li></ul></ul></ul><ul><ul><li>End-if </li></ul></ul><ul><li>End-for </li></ul><ul><li>Note: There is no Else now! </li></ul>
  • 66. Output of the second XSL <ul><li>Found a learner! Found a learner! Found a learner! </li></ul><ul><li>Explanation </li></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;class&amp;quot;&gt; </li></ul></ul><ul><ul><ul><li>The XSLT processor begins at the root element when looking for template matches. It finds a match for the root element class . </li></ul></ul></ul><ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;student&amp;quot;/&gt; </li></ul></ul><ul><ul><ul><li>In our template that matched class we use xsl:apply-templates which will check for template matches on all the children of class . The children of class in our XML document are student and teacher . </li></ul></ul></ul><ul><ul><ul><li>To have the teacher element &amp;quot;Mr. Bean&amp;quot; ignored, we use the select attribute of xsl:apply-templates to specify only student children. </li></ul></ul></ul><ul><ul><ul><li>The XSLT processor then goes searching templates that only match student elements. </li></ul></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;student“&gt; </li></ul></ul><ul><ul><ul><li>The processor finds the only other template in our XSLT, which prints out &amp;quot;Found a learner!&amp;quot; for each student element in the XML document. XSLT finds three students, so &amp;quot;Found a learner!&amp;quot; is displayed three times. </li></ul></ul></ul><ul><ul><ul><li>XSLT then finishes its processing and we are left with XSLT output that has eliminated the unwanted text, &amp;quot;Mr. Bean!&amp;quot; </li></ul></ul></ul>
  • 67. Beware of this Problem! <ul><li>Suppose the XSL was like this: </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;class&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;student&amp;quot;&gt; </li></ul><ul><li>Found a learner! </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul><ul><li>What would be the output? See next slide. </li></ul>
  • 68. Analysis <ul><li>The only change we have made is to remove the select attribute from apply-templates </li></ul><ul><li>This would translate to something different! </li></ul><ul><li>See next slide </li></ul>
  • 69. Plain English Version <ul><li>For each element in the given XML </li></ul><ul><ul><li>If the current element is class </li></ul></ul><ul><ul><ul><li>If the child element of the current element is student </li></ul></ul></ul><ul><ul><ul><ul><li>Output Found a learner </li></ul></ul></ul></ul><ul><ul><ul><li>Else </li></ul></ul></ul><ul><ul><ul><ul><li>Output the contents of the current element as they are </li></ul></ul></ul></ul><ul><ul><ul><li>End-if </li></ul></ul></ul><ul><ul><li>End-if </li></ul></ul><ul><li>End-for </li></ul><ul><li>Note: There is an Else now! </li></ul>
  • 70. What about this? <ul><li>XML </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; college &gt; </li></ul><ul><li>&lt; class &gt; </li></ul><ul><li>&lt; institution &gt; SICSR &lt;/ institution &gt; </li></ul><ul><li>&lt; student &gt; Jack &lt;/ student &gt; </li></ul><ul><li>&lt; student &gt; Harry &lt;/ student &gt; </li></ul><ul><li>&lt; student &gt; Rebecca &lt;/ student &gt; </li></ul><ul><li>&lt; teacher &gt; Mr. Bean &lt;/ teacher &gt; </li></ul><ul><li>&lt;/ class &gt; </li></ul><ul><li>&lt;/ college &gt; </li></ul><ul><li>XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; class &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; student &amp;quot;&gt; </li></ul><ul><li>Found a learner! </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 71. Output <ul><li>SICSR Found a learner! Found a learner! Found a learner! Mr. Bean </li></ul><ul><li>Explanation </li></ul><ul><ul><li>The same logic as earlier applies </li></ul></ul><ul><ul><li>If a match is found, do something; else display contents of current element blindly </li></ul></ul><ul><ul><li>Remember, we do not have a select attribute in the template </li></ul></ul><ul><ul><li>If we have it, what would happen? </li></ul></ul>
  • 72. Another Variation <ul><li>XML </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;class.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; college &gt; </li></ul><ul><li>&lt; class &gt; </li></ul><ul><li>&lt; institution &gt; SICSR &lt;/ institution &gt; </li></ul><ul><li>&lt; student &gt; Jack &lt;/ student &gt; </li></ul><ul><li>&lt; student &gt; Harry &lt;/ student &gt; </li></ul><ul><li>&lt; student &gt; Rebecca &lt;/ student &gt; </li></ul><ul><li>&lt; teacher &gt; Mr. Bean &lt;/ teacher &gt; </li></ul><ul><li>&lt;/ class &gt; </li></ul><ul><li>&lt;/ college &gt; </li></ul><ul><li>XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; class &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; student &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; student &amp;quot;&gt; </li></ul><ul><li>Found a learner! </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul><ul><li>Output </li></ul><ul><li>Found a learner! Found a learner! Found a learner! </li></ul>
  • 73. Summary <ul><li>Do not leave our code in an ambiguous state </li></ul><ul><li>This happens if we specify &lt;apply-templates&gt; without any specific select attribute </li></ul><ul><li>It can also happen if we do not specify exact selection criteria inside &lt;template match&gt; </li></ul><ul><li>Output may not be as expected! </li></ul>
  • 74. Another XSLT Example – 1 <ul><li>Consider the following XML document </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;pune.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;content&gt; </li></ul><ul><li>&lt;about&gt; PUNE &lt;/about&gt; </li></ul><ul><li>&lt;city&gt; </li></ul><ul><li>&lt;line1&gt; Pune is a lovely city &lt;/line1&gt; </li></ul><ul><li>&lt;line2&gt; The education facilities are as best as you can get &lt;/line2&gt; </li></ul><ul><li>&lt;line3&gt; And the weather is great, too. &lt;/line3&gt; </li></ul><ul><li>&lt;/city&gt; </li></ul><ul><li>&lt;/content&gt; </li></ul>
  • 75. Another XSLT Example – 2 <ul><li>Here is the corresponding XSLT document </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;content&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt;&lt;title&gt;Welcome to Pune!&lt;/title&gt;&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;h1&gt;&lt;xsl:value-of select=&amp;quot;about&amp;quot;/&gt;&lt;/h1&gt; </li></ul><ul><li>&lt;h2&gt;&lt;xsl:value-of select=&amp;quot;city/line1&amp;quot;/&gt;&lt;/h2&gt; </li></ul><ul><li>&lt;h3&gt;&lt;xsl:value-of select=&amp;quot;city/line2&amp;quot;/&gt;&lt;/h3&gt; </li></ul><ul><li>&lt;h4&gt;&lt;xsl:value-of select=&amp;quot;city/line3&amp;quot;/&gt;&lt;/h4&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 76. Using Parameters in Templates
  • 77. Using Parameters in Templates <ul><li>&lt;xsl:template name = &amp;quot;print&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:param name = &amp;quot;A&amp;quot; /&gt; </li></ul><ul><li>&lt;xsl:param name = &amp;quot;B&amp;quot; &gt;111&lt;/xsl:param&gt; </li></ul><ul><li>&lt;xsl:value-of select = &amp;quot;$A&amp;quot; /&gt; </li></ul><ul><li>&lt;xsl:text &gt; + &lt;/xsl:text&gt; </li></ul><ul><li>&lt;xsl:value-of select = &amp;quot;$B&amp;quot; /&gt; </li></ul><ul><li>&lt;xsl:text &gt; = &lt;/xsl:text&gt; </li></ul><ul><li>&lt;xsl:value-of select = &amp;quot;$A+$B&amp;quot; /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>This defines a template (like a method) called as print </li></ul><ul><li>This template can receive two parameters, named A and B. B has a default value of 111. </li></ul><ul><li>The template prints the following as the output: </li></ul><ul><li>Value of A + Value of B = Sum of A and B </li></ul><ul><li>For example, if a caller calls this template with A = 10 and B = 20, the output will be: </li></ul><ul><li>10 + 20 = 30 </li></ul>
  • 78. Calling Templates <ul><li>&lt;xsl:template match = &amp;quot;/&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:call-template name = &amp;quot;print&amp;quot; &gt; </li></ul><ul><li> &lt;xsl:with-param name = &amp;quot;A&amp;quot; &gt;11&lt;/xsl:with-param&gt; </li></ul><ul><li> &lt;xsl:with-param name = &amp;quot;B&amp;quot; &gt;33&lt;/xsl:with-param&gt; </li></ul><ul><li>&lt;/xsl:call-template&gt; </li></ul><ul><li>&lt;xsl:call-template name = &amp;quot;print&amp;quot; &gt; </li></ul><ul><li> &lt;xsl:with-param name = &amp;quot;A&amp;quot; &gt;55&lt;/xsl:with-param&gt; </li></ul><ul><li>&lt;/xsl:call-template&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>Now, we are calling the template defined earlier ( print ) twice in succession: first time passing A = 11 and B = 33; and second time passing A = 55 and not passing any value for B </li></ul><ul><li>Output (Full code on next slide): </li></ul><ul><li>11 + 33 = 44 </li></ul><ul><li>55 + 111 = 166 </li></ul><ul><li>Note : 111 was B’s default value in the print template on the previous slide </li></ul>
  • 79. Code <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;1.xsl&amp;quot; ?&gt; </li></ul></ul><ul><ul><li>&lt;AAA &gt; </li></ul></ul><ul><ul><li>&lt;BBB&gt;bbb &lt;/BBB&gt; </li></ul></ul><ul><ul><li>&lt;CCC&gt;ccc &lt;/CCC&gt; </li></ul></ul><ul><ul><li>&lt;/AAA&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl = &amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version = &amp;quot;1.0&amp;quot; &gt; </li></ul></ul><ul><ul><li>&lt;xsl:output method = &amp;quot;text&amp;quot; /&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match = &amp;quot;/&amp;quot; &gt; </li></ul></ul><ul><ul><li>&lt;xsl:call-template name = &amp;quot;print&amp;quot; &gt; </li></ul></ul><ul><ul><li>&lt;xsl:with-param name = &amp;quot;A&amp;quot; &gt;11&lt;/xsl:with-param&gt; </li></ul></ul><ul><ul><li>&lt;xsl:with-param name = &amp;quot;B&amp;quot; &gt;33&lt;/xsl:with-param&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:call-template&gt; </li></ul></ul><ul><ul><li>&lt;xsl:call-template name = &amp;quot;print&amp;quot; &gt; </li></ul></ul><ul><ul><li>&lt;xsl:with-param name = &amp;quot;A&amp;quot; &gt;55&lt;/xsl:with-param&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:call-template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template name = &amp;quot;print&amp;quot; &gt; </li></ul></ul><ul><ul><li>&lt;xsl:param name = &amp;quot;A&amp;quot; /&gt; </li></ul></ul><ul><ul><li>&lt;xsl:param name = &amp;quot;B&amp;quot; &gt;111&lt;/xsl:param&gt; </li></ul></ul><ul><ul><li>&lt;xsl:text &gt; </li></ul></ul><ul><ul><li>&lt;/xsl:text&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select = &amp;quot;$A&amp;quot; /&gt; </li></ul></ul><ul><ul><li>&lt;xsl:text &gt; + &lt;/xsl:text&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select = &amp;quot;$B&amp;quot; /&gt; </li></ul></ul><ul><ul><li>&lt;xsl:text &gt; = &lt;/xsl:text&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select = &amp;quot;$A+$B&amp;quot; /&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 80. Attributes
  • 81. Dealing with Attributes <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;dog name=&apos;Joe&apos;&gt; </li></ul></ul><ul><ul><li>&lt;data weight=&apos;18 kg&apos; color=&amp;quot;black&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;/dog&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;dog&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;P&gt;&lt;B&gt;&lt;xsl:text&gt; Dog: &lt;/xsl:text&gt; &lt;/B&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select=&amp;quot;@name&amp;quot;/&gt;&lt;/P&gt; </li></ul></ul><ul><ul><li>&lt;P&gt;&lt;B&gt;&lt;xsl:text&gt; Color: &lt;/xsl:text&gt; &lt;/B&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select=&amp;quot;data/@color&amp;quot;/&gt;&lt;/P&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 82. Another Example <ul><li>XML </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;products.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;card type=&amp;quot;simple&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;John Doe&lt;/name&gt; </li></ul><ul><li>&lt;title&gt;CEO, Widget Inc.&lt;/title&gt; </li></ul><ul><li>&lt;email&gt;john.doe@widget.com&lt;/email&gt; </li></ul><ul><li>&lt;phone&gt;(202) 456-1414&lt;/phone&gt; </li></ul><ul><li>&lt;/card&gt; </li></ul><ul><li>XSL </li></ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;card[@type=&apos;simple&apos;]&amp;quot;&gt; </li></ul><ul><li>&lt;html xmlns=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot;&gt; </li></ul><ul><li>&lt;title&gt;business card&lt;/title&gt;&lt;body&gt; </li></ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;name&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;title&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;email&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;phone&amp;quot;/&gt; </li></ul><ul><li>&lt;/body&gt;&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;card/name&amp;quot;&gt; </li></ul><ul><li>&lt;h1&gt;&lt;xsl:value-of select=&amp;quot;text()&amp;quot;/&gt;&lt;/h1&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;email&amp;quot;&gt; </li></ul><ul><li>&lt;p&gt;email: &lt;a href=&amp;quot;mailto:{text()}&amp;quot;&gt;&lt;tt&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;text()&amp;quot;/&gt; </li></ul><ul><li>&lt;/tt&gt;&lt;/a&gt;&lt;/p&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 83. A Detailed Example
  • 84. discussionForumHome.xml <ul><li>&lt;? xml version =&amp;quot;1.0&amp;quot; encoding =&amp;quot;utf-8&amp;quot;?&gt; </li></ul><ul><li>&lt;? xml-stylesheet href=&amp;quot;discussionForumHome.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot; ?&gt; </li></ul><ul><li>&lt; disussionForumHome &gt; </li></ul><ul><li>&lt; messageBoard id =&amp;quot;1&amp;quot; name =&amp;quot;Java Programming&amp;quot;/&gt; </li></ul><ul><li>&lt; messageBoard id =&amp;quot;2&amp;quot; name =&amp;quot;XML Programming&amp;quot;/&gt; </li></ul><ul><li>&lt; messageBoard id =&amp;quot;3&amp;quot; name =&amp;quot;XSLT Programming&amp;quot;/&gt; </li></ul><ul><li>&lt;/ disussionForumHome &gt; </li></ul>
  • 85. discussionForumHome.xsl <ul><li>&lt;? xml version =&amp;quot;1.0&amp;quot; encoding =&amp;quot;utf-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl =&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt; html &gt; </li></ul><ul><li>&lt; head &gt; </li></ul><ul><li>&lt; title &gt;Discussion Forum Home Page&lt;/ title &gt; </li></ul><ul><li>&lt;/ head &gt; </li></ul><ul><li>&lt; body &gt; </li></ul><ul><li>&lt; h1 &gt;Discussion Forum Home Page&lt;/ h1 &gt; </li></ul><ul><li>&lt; h3 &gt;Please select a message board to view:&lt;/ h3 &gt; </li></ul><ul><li>&lt; ul &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot;disussionForumHome/messageBoard&amp;quot;/&gt; </li></ul><ul><li>&lt;/ ul &gt; </li></ul><ul><li>&lt;/ body &gt; </li></ul><ul><li>&lt;/ html &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot;messageBoard&amp;quot;&gt; </li></ul><ul><li>&lt; li &gt; </li></ul><ul><li>&lt; a href =&amp;quot;viewForum?id={@id}&amp;quot;&gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot;@name&amp;quot;/&gt; </li></ul><ul><li>&lt;/ a &gt; </li></ul><ul><li>&lt;/ li &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 86. &lt;xsl:template match = “/”&gt; <ul><li>Read this as: Start processing the XML document at the root of the document </li></ul><ul><li>/ indicates root </li></ul><ul><li>There are some details underneath, as explained next </li></ul>
  • 87. Understanding &lt;xsl:template-match&gt; - 1 <ul><li>&lt; xsl:template match =&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt; html &gt; </li></ul><ul><li>&lt; head &gt; </li></ul><ul><li>&lt; title &gt;Discussion Forum Home Page&lt;/ title &gt; </li></ul><ul><li>&lt;/ head &gt; </li></ul><ul><li>&lt; body &gt; </li></ul><ul><li>&lt; h1 &gt;Discussion Forum Home Page&lt;/ h1 &gt; </li></ul><ul><li>&lt; h3 &gt;Please select a message board to view:&lt;/ h3 &gt; </li></ul><ul><li>&lt; ul &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot;disussionForumHome/messageBoard&amp;quot;/&gt; </li></ul><ul><li>&lt;/ ul &gt; </li></ul><ul><li>&lt;/ body &gt; </li></ul><ul><li>&lt;/ html &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>Once root is located, start outputting HTML tags as shown, until the &lt; xsl:apply-templates select =&amp;quot;disussionForumHome/messageBoard&amp;quot;/&gt; is encountered </li></ul>
  • 88. Understanding &lt;xsl:template-match&gt; - 2 <ul><li>&lt;xsl:template match&gt; </li></ul><ul><ul><li>Has four optional attributes </li></ul></ul><ul><ul><ul><li>match – Almost always necessary, takes an Xpath expression as an argument. When the current node matches the node set defined in this expression, the template is executed. </li></ul></ul></ul><ul><ul><ul><li>name – Allows us to refer to the template from elsewhere in the document </li></ul></ul></ul><ul><ul><ul><li>priority – NA </li></ul></ul></ul><ul><ul><ul><li>mode – NA </li></ul></ul></ul>
  • 89. Understanding &lt;xsl:template-match&gt; - 3 <ul><li>But the XSLT also has one more &lt;xsl:template&gt; element as follows: </li></ul><ul><ul><li>&lt; xsl:template match =&amp;quot;messageBoard&amp;quot;&gt; </li></ul></ul><ul><li>Why does this not get the processor’s attention? </li></ul><ul><li>This is because by default, only the root is loaded; all other templates must be explicitly called by other parts of the XSLT code, as we shall discuss shortly </li></ul>
  • 90. &lt;xsl:apply-templates&gt; - 1 <ul><li>Tells the XSLT processor to </li></ul><ul><ul><li>Begin a new search for elements in the source XML document that match the search pattern </li></ul></ul><ul><ul><li>And to look for a matching &lt;xsl:template&gt; element in the XSLT for the same element </li></ul></ul><ul><li>Here, we have: </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot;disussionForumHome/messageBoard&amp;quot;/&gt; </li></ul><ul><li>Hence, the XSLT processor searches for </li></ul><ul><ul><li>A pattern disussionForumHome/messageBoard in the XML document </li></ul></ul><ul><ul><li>A &lt;template-match&gt; element for the same in the XSLT document </li></ul></ul>
  • 91. &lt;xsl:apply-templates&gt; - 2 <ul><li>The &lt;xsl:apply-templates&gt; element works recursively </li></ul><ul><li>Here, it tells the XSLT processor to first select the &lt;discussionForumHome&gt; elements of the current node </li></ul><ul><li>Current node here is the root element of the XML document </li></ul><ul><li>Hence, it selects all the &lt;discussionForumHome&gt; elements at the root level, which means just one element </li></ul><ul><li>However, if we had more elements with the same name deeper in the hierarchy, they would have been ignored </li></ul>
  • 92. &lt;xsl:apply-templates&gt; - 3 <ul><li>Assuming that the XSLT processor locates the &lt;discussionForumHome&gt; element, it searches for all of its &lt;messageBoard&gt; children </li></ul><ul><li>For each &lt;messageBoard&gt; child, the processor looks for the template in our stylesheet that provides the best match </li></ul><ul><li>Since our stylesheet contains a template that exactly matches the &lt;messageBoard&gt; pattern, it is instantiated for each of the &lt;messageBoard&gt; elements in the source XML </li></ul>
  • 93. &lt;xsl:template match = “messageBoard”&gt; <ul><li>This template gets invoked for each instance of the &lt;messageBoard&gt; element </li></ul><ul><li>&lt; xsl:template match =&amp;quot;messageBoard&amp;quot;&gt; </li></ul><ul><li>&lt; li &gt; </li></ul><ul><li>&lt; a href =&amp;quot;viewForum?id={@id}&amp;quot;&gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot;@name&amp;quot;/&gt; </li></ul><ul><li>&lt;/ a &gt; </li></ul><ul><li>&lt;/ li &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>In each case, it produces a line item, consisting of a hyperlink, displaying the name attribute of the &lt;messageBoard&gt; element, and embedding the id attribute in the URL </li></ul>
  • 94. Summary <ul><li>Most transformation in XSLT is driven by two elements, &lt;xsl:template&gt; and &lt;xsl:apply-templates&gt;. </li></ul><ul><li>Processing begins at root, and then: </li></ul><ul><ul><li>For each X in the current node, processor searches for all &lt;xsl:template match = “pattern”&gt; elements in the stylesheet that potentially match the node. </li></ul></ul><ul><ul><li>The selected &lt;xsl:template match = “pattern”&gt; is instantiated using node X as its current node. This templates typically copies data from the source XML, or produces brand new content in combination with the source data. </li></ul></ul><ul><ul><li>If the template contains &lt;xsl:apply-templates select = “newPattern” /&gt;, a new current node is created and the process repeats recursively. </li></ul></ul>
  • 95. Comparing &lt;xsl:template&gt; to &lt;xsl:apply-template&gt; <ul><li>Think about the former as similar to a Java method definition, and the later as invoking that method </li></ul>
  • 96. Another Example <ul><li>college.xml </li></ul><ul><li>&lt;? xml version =&amp;quot;1.0&amp;quot; encoding =&amp;quot;utf-8&amp;quot;?&gt; </li></ul><ul><li>&lt;? xml-stylesheet href=&amp;quot;college.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot; ?&gt; </li></ul><ul><li>&lt; college &gt; </li></ul><ul><li>&lt; name &gt;SICSR&lt;/ name &gt; </li></ul><ul><li>&lt; city &gt;Pune&lt;/ city &gt; </li></ul><ul><li>&lt; state &gt;Maharashtra&lt;/ state &gt; </li></ul><ul><li>&lt;/ college &gt; </li></ul><ul><li>college.xsl </li></ul><ul><li>&lt;? xml version =&amp;quot;1.0&amp;quot; encoding =&amp;quot;utf-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl =&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot;college&amp;quot;&gt; </li></ul><ul><li>&lt; b &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot;name&amp;quot;/&gt; is located in &lt; xsl:value-of select =&amp;quot;city&amp;quot;/&gt;, &lt; xsl:value-of select =&amp;quot;state&amp;quot;/&gt;. </li></ul><ul><li>&lt;/ b &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 97. Explanation <ul><li>Elements that do not start with &lt;xsl:&gt; are simply copied into the result tree </li></ul><ul><li>&lt;xsl:value of&gt; copies the value of something in the XML source tree to the result tree </li></ul><ul><li>Here, current node is &lt;college&gt;, so &lt;xsl:value-of select = “name”/&gt; selects the text content of the &lt;name&gt; element inside &lt;school&gt; </li></ul>
  • 98. An Example for Conceptual Clarity <ul><li>&lt;customer&gt; </li></ul><ul><ul><li>&lt;customer&gt; (Many instance) </li></ul></ul><ul><ul><ul><li>&lt;name&gt; (Many instances) </li></ul></ul></ul>&lt; xsl:template match = &amp;quot; customers &amp;quot; &gt; &lt; ul &gt; &lt; xsl:apply-templates select = &amp;quot; customer &amp;quot; /&gt; &lt;/ ul &gt; &lt;/ xsl:template &gt; &lt; xsl:template match = &amp;quot; customer &amp;quot; &gt; &lt;!– Loops over all customer instances  &lt; li &gt; &lt; xsl:value-of select = &amp;quot; name &amp;quot; /&gt; &lt;/ li &gt; &lt;/ xsl:template &gt; &lt;/ xsl:stylesheet &gt;
  • 99. Value-of select = ‘.’ <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;bold&gt;Hello, world.&lt;/bold&gt; </li></ul></ul><ul><ul><li>&lt;red&gt;I am &lt;/red&gt; </li></ul></ul><ul><ul><li>&lt;italic&gt;fine.&lt;/italic&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;bold&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;P&gt;&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt;&lt;/P&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;red&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;P style=&amp;quot;color:red&amp;quot;&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/P&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;italic&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;P&gt;&lt;i&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/i&gt;&lt;/P&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 100. Another Example – Can be Confusing! <ul><li>What would be the output? </li></ul><ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;employee&gt; </li></ul></ul><ul><ul><li>&lt;firstName&gt;Joe&lt;/firstName&gt; </li></ul></ul><ul><ul><li>&lt;surName&gt;Smith&lt;/surName&gt; </li></ul></ul><ul><ul><li>&lt;/employee&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;employee&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;surName&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;i&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/i&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 101. Explanation <ul><li>XSLT tries to process the first match (at the outermost possible level), i.e. on xslTutorial – There is none </li></ul><ul><li>So, it tries to find a match on the next level, which is employee; and executes code inside </li></ul><ul><li>Now, anything inside the employee tag has to be processed while inside the employee template itself </li></ul><ul><li>But surName is outside of employee, and hence will be ignored </li></ul>
  • 102. Modified Example <ul><li>Is this correct now? </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;employee.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;employee&gt; </li></ul></ul><ul><ul><li>&lt;firstName&gt;Joe&lt;/firstName&gt; </li></ul></ul><ul><ul><li>&lt;middleName&gt;Angus&lt;/middleName&gt; </li></ul></ul><ul><ul><li>&lt;surName&gt;Smith&lt;/surName&gt; </li></ul></ul><ul><ul><li>&lt;/employee&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee&amp;quot;&gt; </li></ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;surName&amp;quot;&gt; </li></ul><ul><li>&lt;i&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/i&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/middleName&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;middleName&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 103. Analysis <ul><li>No! </li></ul><ul><li>Inside an &lt;xsl:template-match&gt;, another &lt;xsl:template match&gt; cannot exist </li></ul><ul><li>How to print the last name, then? </li></ul>
  • 104. Modified Example <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;employee.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;employee&gt; </li></ul></ul><ul><ul><li>&lt;firstName&gt;Joe&lt;/firstName&gt; </li></ul></ul><ul><ul><li>&lt;middleName&gt;Angus&lt;/middleName&gt; </li></ul></ul><ul><ul><li>&lt;surName&gt;Smith&lt;/surName&gt; </li></ul></ul><ul><ul><li>&lt;/employee&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee&amp;quot;&gt; </li></ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul><ul><li>&lt;xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/ sur Name&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=“ . &amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 105. Another Version <ul><li>XSL </li></ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee&amp;quot;&gt; </li></ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul><ul><li>&lt;xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/firstName&amp;quot;&gt; </li></ul><ul><li>&lt;h1&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h1&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/middleName&amp;quot;&gt; </li></ul><ul><li>&lt;i&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/i&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/surName&amp;quot;&gt; </li></ul><ul><li>&lt;h2&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h2&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 106. Modified Further <ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee&amp;quot;&gt; </li></ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul><ul><li>&lt;xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/firstName&amp;quot;&gt; </li></ul><ul><li>&lt;h1&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h1&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;!-- &lt;xsl:template match=&amp;quot;/xslTutorial/employee/middleName&amp;quot;&gt; </li></ul><ul><li>&lt;i&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/i&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>--&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/surName&amp;quot;&gt; </li></ul><ul><li>&lt;h2&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h2&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 107. Modified Further <ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee&amp;quot;&gt; </li></ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul><ul><li>&lt;xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;!-- </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/firstName&amp;quot;&gt; </li></ul><ul><li>&lt;h1&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h1&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/middleName&amp;quot;&gt; </li></ul><ul><li>&lt;i&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/i&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/surName&amp;quot;&gt; </li></ul><ul><li>&lt;h2&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h2&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>--&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 108. Yet Another Version – Only middle name would get displayed after the complete name <ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee&amp;quot;&gt; </li></ul><ul><li>&lt;B&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/B&gt; </li></ul><ul><li>&lt;xsl:apply-templates select=“middleName” /&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/middleName&amp;quot;&gt; </li></ul><ul><li>&lt;i&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/i&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/xslTutorial/employee/surName&amp;quot;&gt; </li></ul><ul><li>&lt;h2&gt; &lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; &lt;/h2&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 109. Controlling the Output Method <ul><li>Try adding </li></ul><ul><ul><li>&lt;xsl:output method=“html” /&gt; </li></ul></ul><ul><ul><li>&lt;xsl:output method=“xml” /&gt; </li></ul></ul><ul><ul><li>&lt;xsl:output method=“text” /&gt; </li></ul></ul><ul><li>one after the other </li></ul><ul><li>Run the example from the command prompt as: </li></ul><ul><ul><li>set CLASSPATH=c:xalanbinxalan.jar </li></ul></ul><ul><ul><li>java org.apache.xalan.xslt.Process -INDENT 3 -IN employee.xml -XSL employee.xsl -OUT output.html </li></ul></ul>
  • 110. More XSLT Examples
  • 111. First XSL Example: Hello World! <ul><li>XML Document (HelloWorld.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;HelloWorld.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;msg&gt;Hello World! </li></ul></ul><ul><ul><li>&lt;/msg&gt; </li></ul></ul><ul><li>XSL Document (HelloWorld.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:output method=&amp;quot;text&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;msg&amp;quot;&gt;Found it!&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 112. Second XSL example <ul><li>XML Document (second.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;second.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;message&gt;We can easily output XML using XSLT! </li></ul></ul><ul><ul><li>&lt;/message&gt; </li></ul></ul><ul><li>XSL Document (second.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:output method=&amp;quot;text&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt;Message in XML document is: &lt;xsl:apply-templates/&gt;!&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 113. Third XSL Example <ul><li>XML document (third.xml) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;third.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;name&gt; </li></ul></ul><ul><ul><li>&lt;first&gt;Sachin&lt;/first&gt; </li></ul></ul><ul><ul><li>&lt;last&gt; Tendukar&lt;/last&gt; </li></ul></ul><ul><ul><li>&lt;/name&gt; </li></ul></ul><ul><li>XSL document (third.xsl) </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;name&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;html&gt; </li></ul></ul><ul><ul><li>&lt;head&gt; </li></ul></ul><ul><ul><li>&lt;title&gt;XSL Output Example&lt;/title&gt; </li></ul></ul><ul><ul><li>&lt;/head&gt; </li></ul></ul><ul><ul><li>&lt;body&gt; </li></ul></ul><ul><ul><li>&lt;p&gt; </li></ul></ul><ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;first&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;/p&gt; </li></ul></ul><ul><ul><li>&lt;p&gt; </li></ul></ul><ul><ul><li>&lt;xsl:apply-templates select=&amp;quot;last&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;/p&gt; </li></ul></ul><ul><ul><li>&lt;/body&gt; </li></ul></ul><ul><ul><li>&lt;/html&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 114. XPath Basics
  • 115. Nodes <ul><li>XPath views a document as a tree </li></ul><ul><li>This tree consists of nodes </li></ul><ul><li>One node can contain other child nodes </li></ul><ul><li>There are 7 types of nodes </li></ul><ul><ul><li>root, element, attribute, text, comment, processing instruction, and namespace </li></ul></ul>
  • 116. Sample XML Document <ul><li>&lt;?xml version = “1.0”?&gt; </li></ul><ul><li>&lt;!-- This is a comment --&gt; </li></ul><ul><li>&lt;BOOK author = “Andrew Tanenbaum” edition = “3”&gt; </li></ul><ul><li>&lt;REVIEW&gt; This is a wonderful book!&lt;/REVIEW&gt; </li></ul><ul><li>Computer Networks </li></ul><ul><li>&lt;/BOOK&gt; </li></ul>
  • 117. XPath View Root Comment This is a comment Element BOOK Attribute author Andrew Tanenbaum Attribute edition 3 Element REVIEW Text This is a wonderful book! Text Computer Networks
  • 118. XPath Basics <ul><li>XPath allows selection of nodes/attributes based on certain conditions </li></ul><ul><li>XSLT processes these nodes </li></ul><ul><li>XPath expressions </li></ul><ul><ul><li>Criteria for selection of nodes/attributes </li></ul></ul><ul><li>Location path </li></ul><ul><ul><li>Similar to street directions: Used for locating information in an XML document </li></ul></ul>
  • 119. XPath Example <ul><li>child::chapter[child::title] </li></ul><ul><ul><li>Look for child nodes of the current node (called as the context node ) representing the &lt;chapter&gt; element </li></ul></ul><ul><ul><li>Within that, look only for child nodes that represent the &lt;title&gt; element </li></ul></ul><ul><ul><li>&lt;chapter&gt; </li></ul></ul><ul><ul><ul><li>&lt;title&gt; </li></ul></ul></ul>
  • 120. XPath Examples – 1 If context node is the &lt;book&gt; element, an empty node-set would be returned. If the context node is &lt;paragraph&gt; number 1 in &lt;section&gt; 2 of &lt;chapter&gt; 2, the returned node-set would be the text node with value First paragraph . Select all text node children of the context node. child::text() If context node is the &lt;book&gt; element, the node-set containing &lt;title&gt;, &lt;chapters&gt; and &lt;appendices&gt; nodes would be returned. Select all element node children of the context node. Children other than the element node are not selected. child::* If context is book, empty node-set would be returned. If context node is &lt;section&gt; element in chapter 2 with attribute number =“1”, a node-set containing four paragraph nodes would be returned. Select the &lt;paragraph&gt; child element nodes of the context node. child::paragraph Example Meaning Syntax
  • 121. XPath Examples – 2 If context node is the &lt;chapter&gt; 1 element, a node-set containing two attribute nodes would be returned, representing the number and title attributes. Select all attribute nodes of the context node. attribute::* NA Select all attribute nodes with name number under the context node. attribute::number NA Select all the child nodes of the context node. Note that this will return non-elements also. child::node() Example Meaning Syntax
  • 122. XPath Examples – 3 NA Select the context node if it is the &lt;paragraph&gt; element. self:paragraph If the context node is &lt;chapters&gt;, an empty node-set would be returned. If the context node is a &lt;section&gt;, the result would be a node-set containing one &lt;chapter&gt; element which contains this &lt;section&gt; element. Select the &lt;chapter&gt; element nodes that are ancestors of the context node. If the context node is a &lt;chapter&gt; element, return that also. ancestor-or-self::chapter If the context node is &lt;book&gt; or &lt;chapters&gt; element, we would get a node-set consisting of six &lt;paragraph&gt; nodes. If the context node is &lt;section&gt; 1 element for &lt;chapter&gt; 2, we would get a node-set consisting of four &lt;paragraph&gt; nodes. Select all &lt;paragraph&gt; elements that are descendants of the context node. descendant::paragraph Example Meaning Syntax
  • 123. Unabbreviated relative location path Abbreviated relative location path Selection criteria child::BOOK BOOK Element nodes that are children of the context node. child::* * All element nodes that are children of the context node. child::text () text () All text node children of the context node. self::node () . Context node itself. parent::node () .. Parent of the context node. attribute::reviewer @reviewer Attribute reviewer of the context node. attribute::* @* All attributes of the context node. parent::node ()/attribute::reviewer [email_address] Attribute reviewer of the parent of the context node. child::AUTHOR [position () = 1] AUTHOR [1] First AUTHOR child node of the context node. child::AUTHOR [position () = last ()] AUTHOR [last ()] Last AUTHOR child node of the context node. child::REVIEW [attribute::reviewer = “Jui”] REVIEW [@reviewer = “Jui”] All REVIEW element children of the context node having a reviewer attribute with a value of Jui . child::REVIEW [attribute::reviewer = “Jui’] [2] REVIEW [@reviewer = “Jui”] [2] Second REVIEW child of the context node having a reviewer attribute with a value of Jui . child::REVIEW [position () = 2] [attribute::reviewer = “Jui’] REVIEW [2] [@reviewer = “Jui”] Second REVIEW child of the context node if it has a reviewer attribute with a value of Jui . child::*/child::AUTHOR */AUTHOR All grand-children element nodes of the context node that are also AUTHOR elements. child::BOOK[child::TITLE= “TCP/IP”] BOOK[TITLE = “TCP/IP”] BOOK element children of the context node having one or more TITLE element children with string-value as TCP/IP . child::BOOK/child::REVIEW/child::COMMENT [position () = 2] BOOK/REVIEW/COMMENT [2] Second child COMMENT element node of the REVIEW element node, which itself is the child of the BOOK element node. self::node()/descendant-or-self::node()/child:COMMENT .//COMMENT All COMMENT element nodes that are descendants of the context node. Note that the // notation is a short-hand for the descendant:: syntax.
  • 124. Template Rules <ul><li>Describes how an XML element node is converted into an XSL element node for displaying </li></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li>&lt;xsl: template match=“/article/title”&gt; </li></ul></ul><ul><ul><li>&lt;xsl: template match=“line”&gt; </li></ul></ul><ul><ul><li>&lt;xsl: template match=“city/line”&gt; </li></ul></ul><ul><ul><li>&lt;xsl: template match=“content//line”&gt; </li></ul></ul><ul><ul><li>&lt;xsl: template match=“title | line”&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=“/article/*” </li></ul></ul><ul><ul><li>&lt;xsl:template match=“/article/section[2]/title/text()” </li></ul></ul>
  • 125. &lt;xsl:value-of&gt; Element <ul><li>The &lt;xsl:value-of&gt; element can be used to select the value of an XML element and add it to the output stream of the transformation </li></ul><ul><li>The value of the required select attribute contains an XPath expression </li></ul><ul><li>It works like navigating a file system where a forward slash (/) selects subdirectories </li></ul>
  • 126. XSLT Example <ul><li>Consider the following XML document </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;history.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;subject&gt; </li></ul><ul><li>&lt;info&gt;History of India&lt;/info&gt; </li></ul><ul><li>&lt;/subject&gt; </li></ul><ul><li>We can write the following XSLT code to transform this into HTML </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;subject&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt;&lt;title&gt;Hello World&lt;/title&gt;&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;h2&gt; &lt;xsl:value-of select=&amp;quot;info&amp;quot;/&gt; &lt;/h2&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 127. Exercise <ul><li>Write an XSLT document for the following XML document to display it as HTML </li></ul><ul><li>&lt;?xml version = “1.0” ?&gt; </li></ul><ul><li>&lt;?xml:stylesheet type = “text/xsl” href = “one.xsl”?&gt; </li></ul><ul><li>&lt;myPerson&gt; </li></ul><ul><li>&lt;personName&gt;Sachin Tendulkar&lt;/personName&gt; </li></ul><ul><li>&lt;/myPerson&gt; </li></ul>
  • 128. Solution <ul><li>&lt;xsl:stylesheet version = “1.0” xmlns:xsl = “http://www/w3.org/1999/XSL/Transform”&gt; </li></ul><ul><li>&lt;xsl:template match = “myPerson”&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;b&gt; &lt;xsl:value-of select = “personName”/&gt; &lt;/b&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 129. Exercise <ul><li>Consider the following XML file: </li></ul><ul><li>&lt;?xml version=“1.0”?&gt; </li></ul><ul><li>&lt;BOOK&gt; </li></ul><ul><li>&lt;BOOK_TITLE&gt;Computer Networks&lt;/BOOK_TITLE&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Tanenbaum&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>Use XSL to display title and author as level 1 and level 2 headers, respectively </li></ul>
  • 130. Solution <ul><li>&lt;xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”&gt; </li></ul><ul><li>&lt;xsl:template match=“BOOK”&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt;&lt;title&gt; Book Information &lt;/title&gt;&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li> &lt;h1&gt;&lt;xsl:value-of select=“BOOK_TITLE”/&gt;&lt;/h1&gt; </li></ul><ul><li> &lt;h2&gt;by &lt;xsl:value-of select=“AUTHOR”/&gt;&lt;/h2&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 131. Exercise <ul><li>Consider this XML and write an XSL to display only the book title and price </li></ul><ul><li>&lt;?xml version = “1.0” ?&gt; </li></ul><ul><li>&lt;?xml version = &amp;quot;1.0&amp;quot; ?&gt; </li></ul><ul><li>&lt;?xml:stylesheet type = &amp;quot;text/xsl&amp;quot; href = &amp;quot;book2.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;CATALOG&gt; </li></ul><ul><li>&lt;BOOK&gt; </li></ul><ul><li>&lt;TITLE&gt;Computer Networks&lt;/TITLE&gt; </li></ul><ul><li>&lt;AUTHORS&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Andrew Tanenbaum&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/AUTHORS&gt; </li></ul><ul><li>&lt;PUBYEAR&gt;2003&lt;/PUBYEAR&gt; </li></ul><ul><li>&lt;PRICE&gt;250&lt;/PRICE&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>&lt;BOOK&gt; </li></ul><ul><li>&lt;TITLE&gt;Computer Fundamentals&lt;/TITLE &gt; </li></ul><ul><li>&lt;AUTHORS&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Rajaraman&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Ghosh&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/AUTHORS&gt; </li></ul><ul><li>&lt;PUBYEAR&gt;2002&lt;/PUBYEAR&gt; </li></ul><ul><li>&lt;PRICE&gt;250&lt;/PRICE&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>&lt;/CATALOG&gt; </li></ul>
  • 132. Solution <ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;BOOK&amp;quot;&gt; </li></ul><ul><li>Book Name: &lt;xsl:value-of select=&amp;quot;TITLE&amp;quot;/&gt; Price: &lt;xsl:value-of select=&amp;quot;PRICE&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 133. Exercise <ul><li>Consider the following XML document, titled emp.xml: </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;emp.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;EMP_INFO&gt; </li></ul><ul><li>&lt;EMPLOYEE&gt; </li></ul><ul><li>&lt;EMP_NAME empID=&amp;quot;9662&amp;quot;&gt; </li></ul><ul><li>&lt;FIRST&gt;Sachin&lt;/FIRST&gt; </li></ul><ul><li>&lt;LAST&gt;Tendulkar&lt;/LAST&gt; </li></ul><ul><li>&lt;/EMP_NAME&gt; </li></ul><ul><li>&lt;/EMPLOYEE&gt; </li></ul><ul><li>&lt;/EMP_INFO&gt; </li></ul><ul><li>Write emp.xsl file mentioned above, which would: </li></ul><ul><ul><li>Display a heading Emp Name: , followed by the employee’s name. </li></ul></ul><ul><ul><li>Display the employee id below this, in a smaller font. </li></ul></ul>
  • 134. Solution <ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;EMP_INFO&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt;&lt;title&gt;Emp Info!&lt;/title&gt;&lt;/head&gt; </li></ul><ul><li> &lt;body&gt; </li></ul><ul><li> &lt;h1&gt;Emp Name: &lt;xsl:value-of select=&amp;quot;EMPLOYEE/EMP_NAME/FIRST&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;EMPLOYEE/EMP_NAME/LAST&amp;quot;/&gt; &lt;/h1&gt; </li></ul><ul><li>&lt;h3&gt; &lt;xsl:value-of select=&amp;quot;EMPLOYEE/EMP_NAME/@empID&amp;quot;/&gt;&lt;/h3&gt; </li></ul><ul><li> &lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 135. Concept of apply-templates
  • 136. apply-templates <ul><li>We know that &lt;xsl:template match=“…”&gt; allows us to search for a location path in our XML document </li></ul><ul><li>Another XSLT syntax, &lt;xsl:apply-templates/&gt; allows us to list the contents of the elements with reference to the previous &lt;xsl:template match=“…”&gt; declaration </li></ul><ul><li>Examples follow </li></ul>
  • 137. Source XML (apply.xml) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;apply1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;book&gt; </li></ul><ul><li>&lt;title&gt;Profesional XSL&lt;/title&gt; </li></ul><ul><li>&lt;chapters&gt; </li></ul><ul><li>&lt;chapter&gt; </li></ul><ul><li>&lt;chapterNo&gt;2&lt;/chapterNo&gt; </li></ul><ul><li>&lt;chapterTopic&gt;XPath&lt;/chapterTopic&gt; </li></ul><ul><li>&lt;chapterAuthor&gt;Andrew Watt&lt;/chapterAuthor&gt; </li></ul><ul><li>&lt;chapterSections&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 1&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 2&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 3&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 4&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 5&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 6&lt;/chapterSection&gt; </li></ul><ul><li>&lt;/chapterSections&gt; </li></ul><ul><li>&lt;/chapter&gt; </li></ul><ul><li>&lt;chapter&gt; </li></ul><ul><li>&lt;chapterNo&gt;3&lt;/chapterNo&gt; </li></ul><ul><li>&lt;chapterTopic&gt;XSLT Basics&lt;/chapterTopic&gt; </li></ul><ul><li>&lt;chapterAuthor&gt;Paul Spencer&lt;/chapterAuthor&gt; </li></ul><ul><li>&lt;chapterSections&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 1&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 2&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 3&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 4&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 5&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 6&lt;/chapterSection&gt; </li></ul><ul><li>&lt;/chapterSections&gt; </li></ul><ul><li>&lt;/chapter&gt; </li></ul><ul><li>&lt;chapter&gt; </li></ul><ul><li>&lt;chapterNo&gt;4&lt;/chapterNo&gt; </li></ul><ul><li>&lt;chapterTopic&gt;Modular XSLT&lt;/chapterTopic&gt; </li></ul><ul><li>&lt;chapterAuthor&gt;Kurt Cagle&lt;/chapterAuthor&gt; </li></ul><ul><li>&lt;chapterSections&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 1&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 2&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 3&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 4&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 5&lt;/chapterSection&gt; </li></ul><ul><li>&lt;chapterSection&gt;Section 6&lt;/chapterSection&gt; </li></ul><ul><li>&lt;/chapterSections&gt; </li></ul><ul><li>&lt;/chapter&gt; </li></ul><ul><li>&lt;/chapters&gt; </li></ul><ul><li>&lt;/book&gt; </li></ul>
  • 138. Basic XSL Example (apply0.xml) <ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul><ul><li>What would be the output? </li></ul>
  • 139. Explanation <ul><li>We have not specified what action should be taken when root is applied </li></ul><ul><li>This causes the entire output to be suppressed! </li></ul>
  • 140. XSL Example: 1 (apply1.xsl) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul><ul><li>Output: ? </li></ul>
  • 141. Explanation <ul><li>Now there is an &lt;apply-templates&gt; inside the &lt;template match&gt; </li></ul><ul><li>This causes the default &lt;apply-templates&gt; to be applied, i.e. without any particular select inside &lt;apply-templates&gt; </li></ul><ul><li>In other words, &lt;apply-templates&gt; gets executed for all elements inside the root </li></ul>
  • 142. XSL Example: 2 (apply2.xsl) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;*&amp;quot;&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 143. Explanation <ul><li>Empty output, since &lt;apply-templates/&gt; will bring search from the root level to all non-root elements. </li></ul><ul><li>But at that level, there is no &lt;apply-templates/&gt; to display anything, unlike in the previous example. </li></ul>
  • 144. XSL Example: 3 (apply3.xsl) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;*&amp;quot;&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;book&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;chapters&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;chapter&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;chapterTopic&amp;quot;&gt; </li></ul><ul><li>&lt;!-- &lt;xsl:apply-templates/&gt; --&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul><ul><li>Display the list of chapter topics </li></ul>
  • 145. XSL Example: 4 (apply4.xsl) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;*&amp;quot;&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;book&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;chapters&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;chapter&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/book/chapters/chapter/chapterNo&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/book/chapters/chapter/chapterTopic&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/book/chapters/chapter/chapterAuthor&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul><ul><li>Display element values for all the selected elements </li></ul>
  • 146. XSL Example: 6 (apply6.xsl) - Tricky <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 147. XSL Example: 5 (apply5.xsl) – Tricky! <ul><li>What if we remove all search paths except the absolute ones? </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/book/chapters/chapter/chapterNo&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/book/chapters/chapter/chapterTopic&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/book/chapters/chapter/chapterAuthor&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 148. Explanation <ul><li>Here, there is no &lt;apply-templates&gt; calling any of the defined &lt;template match&gt; tags </li></ul><ul><li>Hence, our &lt;template match&gt; tags would get ignored completely </li></ul><ul><li>Hence, it would produce the full XML contents as the output </li></ul>
  • 149. Using the Mode Attribute
  • 150. Using Mode <ul><li>If more than one template matches an identical pattern, a conflict arises </li></ul><ul><li>This can be solved by using template priority, but at times, that is also ambiguous </li></ul><ul><li>In such situations, we can use the template mode </li></ul><ul><li>Useful when a template needs to visit the same node, but with different results </li></ul>
  • 151. Mode Example, also uses CSS <ul><li>XML (css-example-1) </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;css-example-1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; us &gt; </li></ul><ul><li>&lt; state name =&amp;quot; Hawaii &amp;quot;&gt; </li></ul><ul><li>&lt; county name =&amp;quot; Hawaii &amp;quot;&gt; </li></ul><ul><li>&lt; city class =&amp;quot; largest &amp;quot;&gt; Hilo &lt;/ city &gt; </li></ul><ul><li>&lt;/ county &gt; </li></ul><ul><li>&lt; county name =&amp;quot; Honolulu &amp;quot;&gt; </li></ul><ul><li>&lt; city class =&amp;quot; largest &amp;quot;&gt; Honolulu &lt;/ city &gt; </li></ul><ul><li>&lt;/ county &gt; </li></ul><ul><li>&lt; county name =&amp;quot; Kauai &amp;quot;&gt; </li></ul><ul><li>&lt; city class =&amp;quot; largest &amp;quot;&gt; Kapaa &lt;/ city &gt; </li></ul><ul><li>&lt;/ county &gt; </li></ul><ul><li>&lt; county name =&amp;quot; Maui &amp;quot;&gt; </li></ul><ul><li>&lt; city class =&amp;quot; largest &amp;quot;&gt; Kahului &lt;/ city &gt; </li></ul><ul><li>&lt;/ county &gt; </li></ul><ul><li>&lt;/ state &gt; </li></ul><ul><li>&lt;/ us &gt; </li></ul><ul><li>XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; html &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; us/state &amp;quot;&gt; </li></ul><ul><li>&lt; html &gt; </li></ul><ul><li>&lt; head &gt; </li></ul><ul><li>&lt; title &gt; State: &lt; xsl:value-of select =&amp;quot; @name &amp;quot;/&gt; </li></ul><ul><li>&lt;/ title &gt; </li></ul><ul><li>&lt; style type =&amp;quot; text/css &amp;quot;&gt; </li></ul><ul><li>h1, h2 {font-family: sans-serif, color: blue} </li></ul><ul><li>ul {font-size: 16pt} </li></ul><ul><li>&lt;/ style &gt; </li></ul><ul><li>&lt;/ head &gt; </li></ul><ul><li>&lt; body &gt; </li></ul><ul><li>&lt; h1 &gt; State: &lt; xsl:value-of select =&amp;quot; @name &amp;quot;/&gt; </li></ul><ul><li>&lt;/ h1 &gt; </li></ul><ul><li>&lt; h2 &gt; All Countries &lt;/ h2 &gt; </li></ul><ul><li>&lt; ul &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; county &amp;quot; mode =&amp;quot; county &amp;quot;/&gt; </li></ul><ul><li>&lt;/ ul &gt; </li></ul><ul><li>&lt; h2 &gt; Largest Cities (by County) &lt;/ h2 &gt; </li></ul><ul><li>&lt; ul &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; county &amp;quot; mode =&amp;quot; city &amp;quot;/&gt; </li></ul><ul><li>&lt;/ ul &gt; </li></ul><ul><li>&lt;/ body &gt; </li></ul><ul><li>&lt;/ html &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; county &amp;quot; mode =&amp;quot; county &amp;quot;&gt; </li></ul><ul><li>&lt; li &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; @name &amp;quot;/&gt; </li></ul><ul><li>&lt;/ li &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; county &amp;quot; mode =&amp;quot; city &amp;quot;&gt; </li></ul><ul><li>&lt; li &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; city &amp;quot;/&gt; ( &lt; xsl:value-of select =&amp;quot; @name &amp;quot;/&gt; ) </li></ul><ul><li>&lt;/ li &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 152. Creating New Elements and Attributes
  • 153. Creating Elements and Attributes <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;students&gt; </li></ul><ul><li>&lt;student first_name=&amp;quot;Raju&amp;quot;&gt; </li></ul><ul><li>&lt;id&gt;101&lt;/id&gt; </li></ul><ul><li>&lt;remarks&gt; A student who is not at all sincere!&lt;/remarks&gt; </li></ul><ul><li>&lt;/student&gt; </li></ul><ul><li>&lt;student first_name=&amp;quot;Aarati&amp;quot;&gt; </li></ul><ul><li>&lt;id&gt;102&lt;/id&gt; </li></ul><ul><li>&lt;remarks&gt; A student who is really quite dedicated to her studies!&lt;/remarks&gt; </li></ul><ul><li>&lt;/student&gt; </li></ul><ul><li>&lt;student first_name=&amp;quot;Rajani&amp;quot;&gt; </li></ul><ul><li>&lt;id&gt;103&lt;/id&gt; </li></ul><ul><li>&lt;remarks&gt; A student who is awesome!&lt;/remarks&gt; </li></ul><ul><li>&lt;/student&gt; </li></ul><ul><li>&lt;/students&gt; </li></ul>
  • 154. Corresponding XSL <ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;students&amp;quot;&gt; </li></ul><ul><li>&lt;students&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/students&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;student&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:element name=&amp;quot;{@first_name}&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:attribute name=&amp;quot;id&amp;quot;&gt;&lt;xsl:value-of select=&amp;quot;id&amp;quot;/&gt;&lt;/xsl:attribute&gt; </li></ul><ul><li>&lt;notes&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;remarks&amp;quot;/&gt; </li></ul><ul><li>&lt;/notes&gt; </li></ul><ul><li>&lt;/xsl:element&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 155. Explanation - 1 <ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><ul><li>match = “/” means select root </li></ul></ul><ul><ul><li>&lt;xsl:apply-templates/&gt; means look for a “xsl:template match” at the root level; i.e. look for a definition that says &lt;xsl:template match=“students&amp;quot;&gt; </li></ul></ul>
  • 156. Explanation – 2 <ul><li>&lt;xsl:template match=&amp;quot;students&amp;quot;&gt; </li></ul><ul><li>&lt;students&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/students&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>When the students element is found, output &lt;students&gt; &lt;/students&gt; </li></ul><ul><li>Then look for a match inside &lt; students&gt; tag, i.e. for &lt;student&gt; tag now </li></ul>
  • 157. Explanation – 3 <ul><li>&lt;xsl:template match=&amp;quot;student&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:element name=&amp;quot;{@first_name}&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:attribute name=&amp;quot;id&amp;quot;&gt;&lt;xsl:value-of select=&amp;quot;id&amp;quot;/&gt;&lt;/xsl:attribute&gt; </li></ul><ul><li>&lt;notes&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;remarks&amp;quot;/&gt; </li></ul><ul><li>&lt;/notes&gt; </li></ul><ul><li>&lt;/xsl:element&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>When the &lt; student&gt; element is found, create a new element in the output XML whose element name equals the value of the attribute first_name in the input XML </li></ul><ul><li>So: </li></ul><ul><ul><li>&lt;student first_name=&amp;quot;Raju&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;id&gt;101&lt;/id&gt; </li></ul></ul><ul><ul><li>&lt;remarks&gt; A student who is not at all sincere!&lt;/remarks&gt; </li></ul></ul><ul><ul><li>&lt;/student&gt; </li></ul></ul><ul><li>Will now become </li></ul><ul><ul><li>&lt;Raju&gt; &lt;/Raju&gt; </li></ul></ul>
  • 158. Explanation – 4 <ul><li>&lt;xsl:attribute name=&amp;quot;id&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;id&amp;quot;/&gt; </li></ul><ul><li>&lt;/xsl:attribute&gt; </li></ul><ul><li>Add an attribute named id to the output XML, which equals the value of the element id of the input XML </li></ul>
  • 159. Explanation – 5 <ul><li>&lt;notes&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;remarks&amp;quot;/&gt; </li></ul><ul><li>&lt;/notes&gt; </li></ul><ul><li>Add a new element called as notes to the output XML, which should contain the value of the remarks element of the input XML </li></ul>
  • 160. Explanation – 6 <ul><li>Input XML </li></ul><ul><ul><li>&lt;student first_name=&amp;quot;Raju&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;id&gt;101&lt;/id&gt; </li></ul></ul><ul><ul><li>&lt;remarks&gt; A student who is not at all sincere!&lt;/remarks&gt; </li></ul></ul><ul><ul><li>&lt;/student&gt; </li></ul></ul><ul><li>Output XML </li></ul><ul><li>&lt;Raju id = “101”&gt; </li></ul><ul><li>&lt;comments&gt; A student who is not at all sincere!&lt;/comments&gt; </li></ul><ul><li>&lt;/Raju&gt; </li></ul>
  • 161. Running the Example <ul><li>set CLASSPATH=C:xalanxalan.jar;C:xalan.xerces.jar </li></ul><ul><li>java org.apache.xalan.xslt.Process -INDENT 3 -IN three.xml -XSL three.xsl -OUT output.xml </li></ul>
  • 162. Xalan and Xerces <ul><li>Xalan: Fully implements XSLT, XPath, and JAXP </li></ul><ul><li>Xerces: Implements XML specifications, namespaces, schema, SAX, DOM, and JAXP </li></ul>
  • 163. Looping using for-each
  • 164. Example: Change XML Contents into an HTML Table – XML File <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;cdcatalog.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;catalog&gt; </li></ul><ul><li>&lt;cd&gt; </li></ul><ul><li>&lt;title&gt;Empire Burlesque&lt;/title&gt; </li></ul><ul><li>&lt;artist&gt;Bob Dylan&lt;/artist&gt; </li></ul><ul><li>&lt;country&gt;USA&lt;/country&gt; </li></ul><ul><li>&lt;company&gt;Columbia&lt;/company&gt; </li></ul><ul><li>&lt;price&gt;10.90&lt;/price&gt; </li></ul><ul><li>&lt;year&gt;1985&lt;/year&gt; </li></ul><ul><li>&lt;/cd&gt; </li></ul><ul><li>&lt;cd&gt; </li></ul><ul><li>&lt;title&gt;Candle in the wind&lt;/title&gt; </li></ul><ul><li>&lt;artist&gt;Elton John&lt;/artist&gt; </li></ul><ul><li>&lt;country&gt;UK&lt;/country&gt; </li></ul><ul><li>&lt;company&gt;HMV&lt;/company&gt; </li></ul><ul><li>&lt;price&gt;8.20&lt;/price&gt; </li></ul><ul><li>&lt;year&gt;1998&lt;/year&gt; </li></ul><ul><li>&lt;/cd&gt; </li></ul><ul><li>&lt;/catalog&gt; </li></ul>
  • 165. Example: Change XML Contents into an HTML Table – XSL File <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;h2&gt;My CD Collection&lt;/h2&gt; </li></ul><ul><li>&lt;table border=&amp;quot;1&amp;quot;&gt; </li></ul><ul><li>&lt;tr bgcolor=&amp;quot;#9acd32&amp;quot;&gt; </li></ul><ul><li>&lt;th align=&amp;quot;left&amp;quot;&gt;Title&lt;/th&gt; </li></ul><ul><li>&lt;th align=&amp;quot;left&amp;quot;&gt;Artist&lt;/th&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;catalog/cd&amp;quot;&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;title&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;artist&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 166. Exercise <ul><li>Display name, address, and phone number for all customers in a table </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;foreach.xsl&amp;quot; ?&gt; </li></ul><ul><li>&lt;customers&gt; </li></ul><ul><li>&lt;customer&gt; </li></ul><ul><li>&lt;name&gt;Mahesh Katare&lt;/name&gt; </li></ul><ul><li>&lt;address&gt;Eve&apos;s Plaza, Bangalore&lt;/address&gt; </li></ul><ul><li>&lt;state&gt;Karnataka&lt;/state&gt; </li></ul><ul><li>&lt;phone&gt;(80) 3247890&lt;/phone&gt; </li></ul><ul><li>&lt;/customer&gt; </li></ul><ul><li>&lt;customer&gt; </li></ul><ul><li>&lt;name&gt;Naren Limaye&lt;/name&gt; </li></ul><ul><li>&lt;address&gt;Shanti Apartments, Thane&lt;/address&gt; </li></ul><ul><li>&lt;state&gt;Maharashtra&lt;/state&gt; </li></ul><ul><li>&lt;phone&gt;(22) 82791810&lt;/phone&gt; </li></ul><ul><li>&lt;/customer&gt; </li></ul><ul><li>&lt;customer&gt; </li></ul><ul><li>&lt;name&gt;Uday Bhalerao&lt;/name&gt; </li></ul><ul><li>&lt;address&gt;Kothrud, Pune&lt;/address&gt; </li></ul><ul><li>&lt;state&gt;Maharashtra&lt;/state&gt; </li></ul><ul><li>&lt;phone&gt;(20) 25530834&lt;/phone&gt; </li></ul><ul><li>&lt;/customer&gt; </li></ul><ul><li>&lt;customer&gt; </li></ul><ul><li>&lt;name&gt;Amol Kavthekar&lt;/name&gt; </li></ul><ul><li>&lt;address&gt;Station Road, Solapur&lt;/address&gt; </li></ul><ul><li>&lt;state&gt;Maharashtra&lt;/state&gt; </li></ul><ul><li>&lt;phone&gt;(217) 2729345&lt;/phone&gt; </li></ul><ul><li>&lt;/customer&gt; </li></ul><ul><li>&lt;customer&gt; </li></ul><ul><li>&lt;name&gt;Meghraj Mane&lt;/name&gt; </li></ul><ul><li>&lt;address&gt;Cannuaght Place, Delhi&lt;/address&gt; </li></ul><ul><li>&lt;state&gt;Delhi&lt;/state&gt; </li></ul><ul><li>&lt;phone&gt;(11) 57814091&lt;/phone&gt; </li></ul><ul><li>&lt;/customer&gt; </li></ul><ul><li>&lt;customer&gt; </li></ul><ul><li>&lt;name&gt;Sameer Joshi&lt;/name&gt; </li></ul><ul><li>&lt;address&gt;Gullapetti, Hyderabad&lt;/address&gt; </li></ul><ul><li>&lt;state&gt;Andhra Pradesh&lt;/state&gt; </li></ul><ul><li>&lt;phone&gt;93717-90911&lt;/phone&gt; </li></ul><ul><li>&lt;/customer&gt; </li></ul><ul><li>&lt;/customers&gt; </li></ul>
  • 167. Solution <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;HTML&gt; </li></ul><ul><li>&lt;BODY&gt; </li></ul><ul><li>&lt;TABLE border = &amp;quot;2&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;customers/customer&amp;quot;&gt; </li></ul><ul><li>&lt;TR&gt; </li></ul><ul><li>&lt;TD&gt;&lt;xsl:value-of select=&amp;quot;name&amp;quot; /&gt;&lt;/TD&gt; </li></ul><ul><li>&lt;TD&gt;&lt;xsl:value-of select=&amp;quot;address&amp;quot; /&gt;&lt;/TD&gt; </li></ul><ul><li>&lt;TD&gt;&lt;xsl:value-of select=&amp;quot;phone&amp;quot; /&gt;&lt;/TD&gt; </li></ul><ul><li>&lt;/TR&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/TABLE&gt; </li></ul><ul><li>&lt;/BODY&gt; </li></ul><ul><li>&lt;/HTML&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul>
  • 168. Exercise <ul><li>Achieve the same results without using for-each </li></ul>
  • 169. Solution <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;HTML&gt; </li></ul><ul><li>&lt;BODY&gt; </li></ul><ul><li>&lt;TABLE border=&amp;quot;2&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/TABLE&gt; </li></ul><ul><li>&lt;/BODY&gt; </li></ul><ul><li>&lt;/HTML&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;customers/customer&amp;quot;&gt; </li></ul><ul><li>&lt;TR&gt; </li></ul><ul><li>&lt;TD&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;name&amp;quot;/&gt; </li></ul><ul><li>&lt;/TD&gt; </li></ul><ul><li>&lt;TD&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;address&amp;quot;/&gt; </li></ul><ul><li>&lt;/TD&gt; </li></ul><ul><li>&lt;TD&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;phone&amp;quot;/&gt; </li></ul><ul><li>&lt;/TD&gt; </li></ul><ul><li>&lt;/TR&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 170. Another for-each Example
  • 171. Exercise: XML <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;SalesToHTML.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;SalesReport&gt; </li></ul><ul><li>&lt;Company&gt;i-flex Solutions Limited&lt;/Company&gt; </li></ul><ul><li>&lt;Period&gt;2005-06&lt;/Period&gt; </li></ul><ul><li>&lt;Sales Region=&amp;quot;US&amp;quot;&gt;USD 250 Million&lt;/Sales&gt; </li></ul><ul><li>&lt;Sales Region=&amp;quot;Europe&amp;quot;&gt;USD 100 Million&lt;/Sales&gt; </li></ul><ul><li>&lt;Sales Region=&amp;quot;Asia&amp;quot;&gt;USD 50 Million&lt;/Sales&gt; </li></ul><ul><li>&lt;/SalesReport&gt; </li></ul>
  • 172. Solution: XSL <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:output method=&amp;quot;html&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt; </li></ul><ul><li>&lt;title&gt;Sales Report, &lt;xsl:value-of select=&amp;quot;/SalesReport/Company&amp;quot;/&gt;: </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;/SalesReport/Period&amp;quot;/&gt; </li></ul><ul><li>&lt;/title&gt; </li></ul><ul><li>&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;br/&gt; </li></ul><ul><li>&lt;h2&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;/SalesReport/Company&amp;quot;/&gt;, Sales Report: &lt;xsl:value-of select=&amp;quot;/SalesReport/Period&amp;quot;/&gt; </li></ul><ul><li>&lt;/h2&gt; </li></ul><ul><li>&lt;br/&gt; </li></ul><ul><li>&lt;table width=&amp;quot;50%&amp;quot;&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;th&gt;Region&lt;/th&gt; </li></ul><ul><li>&lt;th&gt;Sales&lt;/th&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;/SalesReport/Sales&amp;quot;&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;td align=&amp;quot;center&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;@Region&amp;quot;/&gt; </li></ul><ul><li>&lt;/td&gt; </li></ul><ul><li>&lt;td align=&amp;quot;center&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt; </li></ul><ul><li>&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 173. Sorting Data
  • 174. Sorting <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial&gt; </li></ul></ul><ul><ul><li>&lt;name&gt;John&lt;/name&gt; </li></ul></ul><ul><ul><li>&lt;name&gt;Josua&lt;/name&gt; </li></ul></ul><ul><ul><li>&lt;name&gt;Charles&lt;/name&gt; </li></ul></ul><ul><ul><li>&lt;name&gt;Alice&lt;/name&gt; </li></ul></ul><ul><ul><li>&lt;name&gt;Martha&lt;/name&gt; </li></ul></ul><ul><ul><li>&lt;name&gt;George&lt;/name&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;TABLE&gt; </li></ul></ul><ul><ul><li>&lt;xsl:for-each select=&amp;quot;//name&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:sort order=&amp;quot;ascending&amp;quot; select=&amp;quot;.&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;TR&gt;&lt;TH&gt;&lt;xsl:value-of select=&amp;quot;.&amp;quot;/&gt;&lt;/TH&gt;&lt;/TR&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:for-each&gt; </li></ul></ul><ul><ul><li>&lt;/TABLE&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 175. Sorting Information <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;h2&gt;My CD Collection&lt;/h2&gt; </li></ul><ul><li>&lt;table border=&amp;quot;1&amp;quot;&gt; </li></ul><ul><li>&lt;tr bgcolor=&amp;quot;#9acd32&amp;quot;&gt; </li></ul><ul><li>&lt;th&gt;Title&lt;/th&gt; </li></ul><ul><li>&lt;th&gt;Artist&lt;/th&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;catalog/cd&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:sort select=&amp;quot;artist&amp;quot;/&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;title&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;artist&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 176. Sorting – Another Example – XML File <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; ?&gt; </li></ul><ul><li>&lt;famous-persons&gt; </li></ul><ul><li>&lt;persons category=&amp;quot;medicine&amp;quot;&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Edward &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Jenner &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Gertrude &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Elion &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;/persons&gt; </li></ul><ul><li>&lt;persons category=&amp;quot;computer science&amp;quot;&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Charles &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Babbage &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Alan &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Touring &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Ada &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Byron &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;/persons&gt; </li></ul><ul><li>&lt;persons category=&amp;quot;astronomy&amp;quot;&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Tycho &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Brahe &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Johannes &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Kepler &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;person&gt; </li></ul><ul><li>&lt;firstname&gt; Galileo &lt;/firstname&gt; </li></ul><ul><li>&lt;name&gt; Galilei &lt;/name&gt; </li></ul><ul><li>&lt;/person&gt; </li></ul><ul><li>&lt;/persons&gt; </li></ul><ul><li>&lt;/famous-persons&gt; </li></ul>
  • 177. Sorting – Another Example – XSLT <ul><li>Write an XSLT for sorting data appropriately </li></ul>
  • 178. Sorting and Data Types – 1 <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial &gt; </li></ul></ul><ul><ul><li>&lt;car id=&amp;quot;11&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;car id=&amp;quot;6&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;car id=&amp;quot;105&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;car id=&amp;quot;28&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;car id=&amp;quot;9&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;TABLE&gt; </li></ul></ul><ul><ul><li>&lt;xsl:for-each select=&amp;quot;//car&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:sort data-type=&amp;quot;text&amp;quot; select=&amp;quot;@id&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;TR&gt;&lt;TH&gt;&lt;xsl:text&gt; Car-&lt;/xsl:text&gt; &lt;xsl:value-of </li></ul></ul><ul><ul><li>select=&amp;quot;@id&amp;quot;/&gt;&lt;/TH&gt;&lt;/TR&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:for-each&gt; </li></ul></ul><ul><ul><li>&lt;/TABLE&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 179. Sorting and Data Types – 2 <ul><li>Modified XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;TABLE&gt; </li></ul></ul><ul><ul><li>&lt;xsl:for-each select=&amp;quot;//car&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:sort data-type=“number&amp;quot; select=&amp;quot;@id&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;TR&gt;&lt;TH&gt;&lt;xsl:text&gt; Car-&lt;/xsl:text&gt; &lt;xsl:value-of </li></ul></ul><ul><ul><li>select=&amp;quot;@id&amp;quot;/&gt;&lt;/TH&gt;&lt;/TR&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:for-each&gt; </li></ul></ul><ul><ul><li>&lt;/TABLE&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 180. Sorting on Multiple Keys <ul><li>Specify multiple xsl:sort elements, one after the other </li></ul><ul><li>Example </li></ul><ul><ul><li>&lt;xsl:sort select=“SURNAME”/&gt; </li></ul></ul><ul><ul><li>&lt;xsl:sort select=“FIRSTNAME”/&gt; </li></ul></ul><ul><ul><li>&lt;xsl:sort select=“BIRTH_DATE” order=“ascending”/&gt; </li></ul></ul>
  • 181. Another Sort Example <ul><li>sort-1.xml </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;sort-1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; europe &gt; </li></ul><ul><li>&lt; state &gt; Belgium &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Germany &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; United Kingdom &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; France &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Spain &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Italy &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Turkey &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Sweden &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Ireland &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Greece &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Malta &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Vatican City &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Portugal &lt;/ state &gt; </li></ul><ul><li>&lt;/ europe &gt; </li></ul><ul><li>sort-1.xsl </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:text &gt; Alphabetical List of European States &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10;Total Number of States: &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; count(state) &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10;&amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; state &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:sort /&gt; </li></ul><ul><li>&lt;/ xsl:apply-templates &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; state &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:text &gt; - &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:apply-templates /&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 182. Producing HTML Output <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; html &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe &amp;quot;&gt; </li></ul><ul><li>&lt; html &gt; </li></ul><ul><li>&lt; head &gt; </li></ul><ul><li>&lt; title &gt; European States &lt;/ title &gt; </li></ul><ul><li>&lt;/ head &gt; </li></ul><ul><li>&lt; style type =&amp;quot; text/css &amp;quot;&gt; body {font-family: sans-serif} &lt;/ style &gt; </li></ul><ul><li>&lt; body &gt; </li></ul><ul><li>&lt; h3 &gt; Alphabetical List of European States &lt;/ h3 &gt; </li></ul><ul><li>&lt; p &gt; </li></ul><ul><li>&lt; b &gt; Total Number of States: &lt;/ b &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; count(state) &amp;quot;/&gt; </li></ul><ul><li>&lt;/ p &gt; </li></ul><ul><li>&lt; ul &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; state &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:sort /&gt; </li></ul><ul><li>&lt;/ xsl:apply-templates &gt; </li></ul><ul><li>&lt;/ ul &gt; </li></ul><ul><li>&lt;/ body &gt; </li></ul><ul><li>&lt;/ html &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; state &amp;quot;&gt; </li></ul><ul><li>&lt; li &gt; </li></ul><ul><li>&lt; xsl:apply-templates /&gt; </li></ul><ul><li>&lt;/ li &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 183. Sorting on Attributes and Formatting <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; text &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:text &gt; Number of EU Member States: &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; count(state) &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; state/@joined &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:sort data-type =&amp;quot; number &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:apply-templates &gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; state/@joined &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:text &gt; - &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; .. &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; ( &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; . &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; )&amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 184. Another Sort Example for Multiple Fields <ul><li>item-list.xml </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; list &gt; </li></ul><ul><li>&lt; freezer &gt; </li></ul><ul><li>&lt; item &gt; peas &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; green beans &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; pot pie &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; ice cream &lt;/ item &gt; </li></ul><ul><li>&lt;/ freezer &gt; </li></ul><ul><li>&lt; bakery &gt; </li></ul><ul><li>&lt; item &gt; rolls &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; jelly doughnuts &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; bread &lt;/ item &gt; </li></ul><ul><li>&lt;/ bakery &gt; </li></ul><ul><li>&lt; produce &gt; </li></ul><ul><li>&lt; item &gt; tomato &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; apple &lt;/ item &gt; </li></ul><ul><li>&lt; item &gt; potato &lt;/ item &gt; </li></ul><ul><li>&lt;/ produce &gt; </li></ul><ul><li>&lt;/ list &gt; </li></ul><ul><li>item-list.xsl </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; text &amp;quot; version =&amp;quot; 1.0 &amp;quot; encoding =&amp;quot; UTF-8 &amp;quot; indent =&amp;quot; yes &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; list &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; * &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:sort select =&amp;quot; name() &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:apply-templates &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; * &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:text &gt; Section: &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; name() &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; item &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:sort /&gt; </li></ul><ul><li>&lt;/ xsl:apply-templates &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; item &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:text &gt; * &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:apply-templates /&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 185. Explanation <ul><li>The first template matches the list element and sorts on the names (using name () ) of the element children (using *) of the list </li></ul><ul><ul><li>This is the first sort </li></ul></ul><ul><li>The second template matches only on the element children of list , again using * </li></ul><ul><ul><li>After inserting some text (such as Section: ), and the name of the element (again using name() ), the template sorts the text node content of item children </li></ul></ul><ul><ul><ul><li>This is the second sort </li></ul></ul></ul>
  • 186. Conditional Processing
  • 187. Writing Conditions Using &lt;xsl:if&gt; <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;h2&gt;My CD Collection&lt;/h2&gt; </li></ul><ul><li>&lt;table border=&amp;quot;1&amp;quot;&gt; </li></ul><ul><li>&lt;tr bgcolor=&amp;quot;#9acd32&amp;quot;&gt; </li></ul><ul><li>&lt;th&gt;Title&lt;/th&gt; </li></ul><ul><li>&lt;th&gt;Artist&lt;/th&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;catalog/cd&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:if test=&amp;quot;price &amp;gt; 10&amp;quot;&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;title&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;artist&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:if&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 188. &lt;xsl:apply-templates&gt; Element <ul><li>The &lt;xsl:apply-templates&gt; element applies a template rule to the current element or to the current element&apos;s child nodes </li></ul><ul><li>Example follows </li></ul>
  • 189. Exercise <ul><li>Consider the following XML document </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;books3.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;BOOKS&gt; </li></ul><ul><li>&lt;BOOK pubyear=&amp;quot;1929&amp;quot;&gt; </li></ul><ul><li>&lt;BOOK_TITLE&gt;Look Homeward, Angel&lt;/BOOK_TITLE&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Wolfe, Thomas&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>&lt;BOOK pubyear=&amp;quot;1973&amp;quot;&gt; </li></ul><ul><li>&lt;BOOK_TITLE&gt;Gravity&apos;s Rainbow&lt;/BOOK_TITLE&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Pynchon, Thomas&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>&lt;BOOK pubyear=&amp;quot;1977&amp;quot;&gt; </li></ul><ul><li>&lt;BOOK_TITLE&gt;Cards as Weapons&lt;/BOOK_TITLE&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Jay, Ricky&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>&lt;BOOK pubyear=&amp;quot;2001&amp;quot;&gt; </li></ul><ul><li>&lt;BOOK_TITLE&gt;Computer Networks&lt;/BOOK_TITLE&gt; </li></ul><ul><li>&lt;AUTHOR&gt;Tanenbaum, Andrew&lt;/AUTHOR&gt; </li></ul><ul><li>&lt;/BOOK&gt; </li></ul><ul><li>&lt;/BOOKS&gt; </li></ul><ul><li>Do the following: </li></ul><ul><ul><li>Display all the books published in the 1970s. </li></ul></ul><ul><ul><li>Display the same information in a tabular form with an asterisk against the book title. </li></ul></ul><ul><ul><li>Display the same information as in (2), sorted on author name. </li></ul></ul>
  • 190. Solution <ul><li>Refer to books.xml, books.xsl, books2.xsl, books3.xsl </li></ul>
  • 191. Understanding position () and number <ul><li>XML (functions-example-1) </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;functions-example-1.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;europe&gt; </li></ul><ul><li>&lt;state&gt;Belgium&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Germany&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;United Kingdom&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;France&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Spain&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Italy&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Turkey&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Sweden&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Ireland&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Greece&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Malta&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Vatican City&lt;/state&gt; </li></ul><ul><li>&lt;state&gt;Portugal&lt;/state&gt; </li></ul><ul><li>&lt;/europe&gt; </li></ul><ul><li>XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; html &amp;quot; version =&amp;quot; 1.0 &amp;quot; encoding =&amp;quot; UTF-8 &amp;quot; indent =&amp;quot; yes &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; / &amp;quot;&gt; </li></ul><ul><li>&lt; table border =&amp;quot; 2 &amp;quot;&gt; </li></ul><ul><li>&lt; tbody &gt; </li></ul><ul><li>&lt; tr &gt; </li></ul><ul><li>&lt; th &gt; Title &lt;/ th &gt; </li></ul><ul><li>&lt; th &gt; Position &lt;/ th &gt; </li></ul><ul><li>&lt; th &gt; Number &lt;/ th &gt; </li></ul><ul><li>&lt;/ tr &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; europe/state &amp;quot;/&gt; </li></ul><ul><li>&lt;/ tbody &gt; </li></ul><ul><li>&lt;/ table &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe/state &amp;quot;&gt; </li></ul><ul><li>&lt; tr &gt; </li></ul><ul><li>&lt; td &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; . &amp;quot;/&gt; </li></ul><ul><li>&lt;/ td &gt; </li></ul><ul><li>&lt; td align =&amp;quot; center &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; position() &amp;quot;/&gt; </li></ul><ul><li>&lt;/ td &gt; </li></ul><ul><li>&lt; td align =&amp;quot; center &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:number /&gt; </li></ul><ul><li>&lt;/ td &gt; </li></ul><ul><li>&lt;/ tr &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 192. Writing Conditions
  • 193. if Example – 1 <ul><li>Consider an XML file (names.xml) as follows. Display all the names comma-separated in the output HTML file. </li></ul><ul><li>&lt;?xml version=&apos;1.0&apos;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;ifcomma2.xsl&amp;quot; ?&gt; </li></ul><ul><li>&lt;namelist&gt; </li></ul><ul><li>&lt;name&gt;Albert&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Terrance&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Will&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Sylvia&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Timothy&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Gordon&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;James&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Robert&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Dan&lt;/name&gt; </li></ul><ul><li>&lt;name&gt;Sasha&lt;/name&gt; </li></ul><ul><li>&lt;/namelist&gt; </li></ul>
  • 194. if Example – 1 <ul><li>&lt;?xml version=&apos;1.0&apos;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;namelist/name&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;xsl:if test=&amp;quot;position()!=last()&amp;quot;&gt;, &lt;/xsl:if&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 195. if Example – 2 <ul><li>Another way to achieve the same objective </li></ul><ul><li>&lt;?xml version=&apos;1.0&apos;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;namelist/name&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:if test=&amp;quot;position()!=1&amp;quot;&gt;, &lt;/xsl:if&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 196. if Example – 3 <ul><li>Consider the following XML file (items.xml) and display alternate rows in yellow background. </li></ul><ul><li>&lt;?xml version=&apos;1.0&apos;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;ifyellow.xsl&amp;quot; ?&gt; </li></ul><ul><li>&lt;items&gt; </li></ul><ul><li>&lt;item&gt;Car&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Pen&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;LP Record&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Wisdom&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Cell phone&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Film projector&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Hole&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Canopy&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Widget&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Concept&lt;/item&gt; </li></ul><ul><li>&lt;item&gt;Null character&lt;/item&gt; </li></ul><ul><li>&lt;/items&gt; </li></ul>
  • 197. if Example – 3 <ul><li>&lt;?xml version=&apos;1.0&apos;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;table border=&amp;quot;1&amp;quot; cellpadding=&amp;quot;2&amp;quot; cellspacing=&amp;quot;0&amp;quot; width=&amp;quot;50%&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;item&amp;quot;&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;xsl:if test=&amp;quot;position() mod 2 = 0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:attribute name=&amp;quot;bgcolor&amp;quot;&gt;yellow&lt;/xsl:attribute&gt; </li></ul><ul><li>&lt;/xsl:if&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 198. Writing Conditions Using &lt;xsl:choose&gt; <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;ISO-8859-1&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;h2&gt;My CD Collection&lt;/h2&gt; </li></ul><ul><li>&lt;table border=&amp;quot;1&amp;quot;&gt; </li></ul><ul><li>&lt;tr bgcolor=&amp;quot;#9acd32&amp;quot;&gt; </li></ul><ul><li>&lt;th&gt;Title&lt;/th&gt; </li></ul><ul><li>&lt;th&gt;Artist&lt;/th&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;catalog/cd&amp;quot;&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;title&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;xsl:choose&gt; </li></ul><ul><li>&lt;xsl:when test=&amp;quot;price &amp;gt; 10&amp;quot;&gt; </li></ul><ul><li>&lt;td bgcolor=&amp;quot;#ff00ff&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;artist&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/xsl:when&gt; </li></ul><ul><li>&lt;xsl:otherwise&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;artist&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/xsl:otherwise&gt; </li></ul><ul><li>&lt;/xsl:choose&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 199. choice Example <ul><li>Consider the following XML file (order.xml). If total number of items selected is &lt;10, display small, if between 10 and 19, display medium, else large. </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;refchoose.xsl&amp;quot; ?&gt; </li></ul><ul><li>&lt;orders&gt; </li></ul><ul><li>&lt;order&gt; </li></ul><ul><li>&lt;lineitem/&gt; </li></ul><ul><li>&lt;lineitem/&gt; </li></ul><ul><li>&lt;total&gt;9&lt;/total&gt; </li></ul><ul><li>&lt;/order&gt; </li></ul><ul><li>&lt;order&gt; </li></ul><ul><li>&lt;lineitem/&gt; </li></ul><ul><li>&lt;lineitem/&gt; </li></ul><ul><li>&lt;total&gt;19&lt;/total&gt; </li></ul><ul><li>&lt;/order&gt; </li></ul><ul><li>&lt;order&gt; </li></ul><ul><li>&lt;lineitem/&gt; </li></ul><ul><li>&lt;lineitem/&gt; </li></ul><ul><li>&lt;total&gt;29&lt;/total&gt; </li></ul><ul><li>&lt;/order&gt; </li></ul><ul><li>&lt;/orders&gt; </li></ul>
  • 200. choice Example <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; &gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;order&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:choose&gt; </li></ul><ul><li>&lt;xsl:when test=&amp;quot;total &amp;lt; 10&amp;quot;&gt; </li></ul><ul><li>(small) </li></ul><ul><li>&lt;/xsl:when&gt; </li></ul><ul><li>&lt;xsl:when test=&amp;quot;total &amp;lt; 20&amp;quot;&gt; </li></ul><ul><li>(medium) </li></ul><ul><li>&lt;/xsl:when&gt; </li></ul><ul><li>&lt;xsl:otherwise&gt; </li></ul><ul><li>(large) </li></ul><ul><li>&lt;/xsl:otherwise&gt; </li></ul><ul><li>&lt;/xsl:choose&gt; </li></ul><ul><li>&lt;xsl:apply-templates /&gt; </li></ul><ul><li>&lt;BR/&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 201. Using Variables
  • 202. XML (Variable-example.xml) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;Variable-example.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; catalog &gt; </li></ul><ul><li>&lt; item id =&amp;quot; SC-001 &amp;quot;&gt; </li></ul><ul><li>&lt; maker &gt; Reliance &lt;/ maker &gt; </li></ul><ul><li>&lt; description &gt; Gas pipe &lt;/ description &gt; </li></ul><ul><li>&lt; size &gt; Large &lt;/ size &gt; </li></ul><ul><li>&lt; price &gt; 15000 &lt;/ price &gt; </li></ul><ul><li>&lt; currency &gt; INR &lt;/ currency &gt; </li></ul><ul><li>&lt;/ item &gt; </li></ul><ul><li>&lt;/ catalog &gt; </li></ul>
  • 203. XSL (Variable-example.xsl) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; xml &amp;quot; version =&amp;quot; 1.0 &amp;quot; encoding =&amp;quot; UTF-8 &amp;quot; indent =&amp;quot; yes &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:variable name =&amp;quot; discount &amp;quot; select =&amp;quot; 0.10 &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; catalog &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:copy &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; item &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:copy &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; item &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:copy &gt; </li></ul><ul><li>&lt; xsl:attribute name =&amp;quot; id &amp;quot;&gt;&lt; xsl:value-of select =&amp;quot; @id &amp;quot;/&gt;&lt;/ xsl:attribute &gt; </li></ul><ul><li>&lt; xsl:copy-of select =&amp;quot; maker | description | size | price &amp;quot;/&gt; </li></ul><ul><li>&lt; discount &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; $discount &amp;quot;/&gt; </li></ul><ul><li>&lt;/ discount &gt; </li></ul><ul><li>&lt; discountPrice &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; price - (price * $discount) &amp;quot;/&gt; </li></ul><ul><li>&lt;/ discountPrice &gt; </li></ul><ul><li>&lt; xsl:copy-of select =&amp;quot; currency &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:copy &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 204. Running the Example <ul><li>set classpath=c:xalanbinxalan.jar </li></ul><ul><li>java org.apache.xalan.xslt.Process -INDENT 3 -IN Variable-example.xml -XSL Variable-example.xsl -OUT output.xml </li></ul>
  • 205. Using Parameters – Modified XSL <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; xml &amp;quot; version =&amp;quot; 1.0 &amp;quot; encoding =&amp;quot; UTF-8 &amp;quot; indent =&amp;quot; yes &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:param name =&amp;quot; discount &amp;quot; select =&amp;quot; 0.10 &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; catalog &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:copy &gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; item &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:copy &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; item &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:copy &gt; </li></ul><ul><li>&lt; xsl:attribute name =&amp;quot; id &amp;quot;&gt;&lt; xsl:value-of select =&amp;quot; @id &amp;quot;/&gt;&lt;/ xsl:attribute &gt; </li></ul><ul><li>&lt; xsl:copy-of select =&amp;quot; maker | description | size | price &amp;quot;/&gt; </li></ul><ul><li>&lt; discount &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; $discount &amp;quot;/&gt; </li></ul><ul><li>&lt;/ discount &gt; </li></ul><ul><li>&lt; discountPrice &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; price - (price * $discount) &amp;quot;/&gt; </li></ul><ul><li>&lt;/ discountPrice &gt; </li></ul><ul><li>&lt; xsl:copy-of select =&amp;quot; currency &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:copy &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 206. What is the Impact? <ul><li>We have changed variable to parameter </li></ul><ul><li>Now, we can pass the value of discount from the command prompt! </li></ul><ul><li>java org.apache.xalan.xslt.Process -INDENT 3 -param discount 0 .20 -IN Variable-example.xml -XSL Variable-example-1.xsl -OUT output.xml </li></ul>
  • 207. &lt;xsl:number&gt; Tag <ul><li>XML </li></ul><ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul></ul><ul><ul><li>&lt;xslTutorial &gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;First Chapter&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;Second Chapter </li></ul></ul><ul><ul><li>&lt;chapter&gt;Subchapter 1&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;Subchapter 2&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;Third Chapter </li></ul></ul><ul><ul><li>&lt;chapter&gt;Subchapter A&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;Subchapter B </li></ul></ul><ul><ul><li>&lt;chapter&gt;sub a&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;sub b&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;chapter&gt;Subchapter C&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;/chapter&gt; </li></ul></ul><ul><ul><li>&lt;/xslTutorial&gt; </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li>&lt;xsl:stylesheet xmlns:xsl=&apos;http://www.w3.org/1999/XSL/Transform&apos; version=&amp;quot;1.0&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;TABLE BORDER=&amp;quot;1&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;TR&gt;&lt;TH&gt;Number&lt;/TH&gt;&lt;TH&gt;text&lt;/TH&gt;&lt;/TR&gt; </li></ul></ul><ul><ul><li>&lt;xsl:for-each select=&amp;quot;//chapter&amp;quot;&gt; </li></ul></ul><ul><ul><li>&lt;TR&gt;&lt;TD&gt; </li></ul></ul><ul><ul><li>&lt;xsl:number/&gt; </li></ul></ul><ul><ul><li>&lt;/TD&gt;&lt;TD&gt; </li></ul></ul><ul><ul><li>&lt;xsl:value-of select=&amp;quot;./text()&amp;quot;/&gt; </li></ul></ul><ul><ul><li>&lt;/TD&gt;&lt;/TR&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:for-each&gt; </li></ul></ul><ul><ul><li>&lt;/TABLE&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:template&gt; </li></ul></ul><ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul></ul>
  • 208. &lt;xsl:number&gt; Continued <ul><li>Change the &lt;xsl:number&gt; tag to the following </li></ul><ul><ul><li>&lt;xsl:number level=&amp;quot;multiple&amp;quot;/&gt; </li></ul></ul>
  • 209. Using position () <ul><li>XML (functions-example-2) </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet type=&amp;quot;text/xsl&amp;quot; href=&amp;quot;functions-example-2.xsl&amp;quot;?&gt; </li></ul><ul><li>&lt; europe &gt; </li></ul><ul><li>&lt; state &gt; Belgium &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Germany &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; United Kingdom &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; France &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Spain &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Italy &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Turkey &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Sweden &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Ireland &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Greece &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Malta &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Vatican City &lt;/ state &gt; </li></ul><ul><li>&lt; state &gt; Portugal &lt;/ state &gt; </li></ul><ul><li>&lt;/ europe &gt; </li></ul><ul><li>XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; text &amp;quot; indent =&amp;quot; yes &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; state &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe/state &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; position() &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; . &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; . &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 210. Now Using number <ul><li>Modified XSL </li></ul><ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;UTF-8&amp;quot;?&gt; </li></ul><ul><li>&lt; xsl:stylesheet version =&amp;quot; 1.0 &amp;quot; xmlns:xsl =&amp;quot; http://www.w3.org/1999/XSL/Transform &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:output method =&amp;quot; text &amp;quot; indent =&amp;quot; yes &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:apply-templates select =&amp;quot; state &amp;quot;/&gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt; xsl:template match =&amp;quot; europe/state &amp;quot;&gt; </li></ul><ul><li>&lt; xsl:number format =&amp;quot; 1 &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; . &lt;/ xsl:text &gt; </li></ul><ul><li>&lt; xsl:value-of select =&amp;quot; . &amp;quot;/&gt; </li></ul><ul><li>&lt; xsl:text &gt; &amp;#10; &lt;/ xsl:text &gt; </li></ul><ul><li>&lt;/ xsl:template &gt; </li></ul><ul><li>&lt;/ xsl:stylesheet &gt; </li></ul>
  • 211. Using XSL and CSS Together
  • 212. XML <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;test.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;products&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/text&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield Text&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;usd&amp;quot;&gt;299&lt;/price&gt; </li></ul><ul><li>&lt;description&gt;Faster than the competition.&lt;/description&gt; </li></ul><ul><li>&lt;version&gt;1.0&lt;/version&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/virus&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield Virus&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;eur&amp;quot;&gt;199&lt;/price&gt; </li></ul><ul><li>&lt;description&gt; </li></ul><ul><li>Protect yourself against malicious code. </li></ul><ul><li>&lt;/description&gt; </li></ul><ul><li>&lt;version&gt;5.0&lt;/version&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/calc&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield Calc&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;usd&amp;quot;&gt;299&lt;/price&gt; </li></ul><ul><li>&lt;description&gt;Clear picture on your data.&lt;/description&gt; </li></ul><ul><li>&lt;version&gt;1.5&lt;/version&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/db&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield DB&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;cad&amp;quot;&gt;599&lt;/price&gt; </li></ul><ul><li>&lt;description&gt;Organize your data.&lt;/description&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;/products&gt; </li></ul>
  • 213. XSL <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet </li></ul><ul><li>version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:output method=&amp;quot;html&amp;quot; indent=&amp;quot;no&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/products&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt; </li></ul><ul><li>&lt;title&gt;Cascading Style Sheet&lt;/title&gt; </li></ul><ul><li>&lt;link rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; href=&amp;quot;table.css&amp;quot; </li></ul><ul><li>title=&amp;quot;Style&amp;quot;/&gt; </li></ul><ul><li>&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;table&gt; </li></ul><ul><li>&lt;tr class=&amp;quot;header&amp;quot;&gt; </li></ul><ul><li>&lt;td&gt;Name&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;Price&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;Description&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;product[position() mod 2 = 1]&amp;quot;&gt; </li></ul><ul><li>&lt;tr class=&amp;quot;odd&amp;quot;&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;name&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;price&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;description&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;product&amp;quot;&gt; </li></ul><ul><li>&lt;tr class=&amp;quot;even&amp;quot;&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;name&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;price&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;description&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 214. CSS <ul><li>.header { background-color: #999999; font-weight: bold; } </li></ul><ul><li>.odd { background-color: normal; } </li></ul><ul><li>.even { background-color: #dfdfdf; } </li></ul>
  • 215. Processing Multiple XML Files Using a Single XSL
  • 216. XML File 1 (products.xml) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;?xml-stylesheet href=&amp;quot;products.xsl&amp;quot; type=&amp;quot;text/xsl&amp;quot;?&gt; </li></ul><ul><li>&lt;products&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/text&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield Text&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;usd&amp;quot;&gt;299&lt;/price&gt; </li></ul><ul><li>&lt;description&gt;Faster than the competition.&lt;/description&gt; </li></ul><ul><li>&lt;version&gt;1.0&lt;/version&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/virus&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield Virus&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;eur&amp;quot;&gt;199&lt;/price&gt; </li></ul><ul><li>&lt;description&gt; </li></ul><ul><li>Protect yourself against malicious code. </li></ul><ul><li>&lt;/description&gt; </li></ul><ul><li>&lt;version&gt;5.0&lt;/version&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/calc&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield Calc&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;usd&amp;quot;&gt;299&lt;/price&gt; </li></ul><ul><li>&lt;description&gt;Clear picture on your data.&lt;/description&gt; </li></ul><ul><li>&lt;version&gt;1.5&lt;/version&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;product href=&amp;quot;http://www.playfield.com/db&amp;quot;&gt; </li></ul><ul><li>&lt;name&gt;Playfield DB&lt;/name&gt; </li></ul><ul><li>&lt;price currency=&amp;quot;cad&amp;quot;&gt;599&lt;/price&gt; </li></ul><ul><li>&lt;description&gt;Organize your data.&lt;/description&gt; </li></ul><ul><li>&lt;/product&gt; </li></ul><ul><li>&lt;/products&gt; </li></ul>
  • 217. XML File 2 (currencies.xml) <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;currencies&gt; </li></ul><ul><li>&lt;currency&gt; </li></ul><ul><li>&lt;code&gt;eur&lt;/code&gt; </li></ul><ul><li>&lt;name&gt;Euros&lt;/name&gt; </li></ul><ul><li>&lt;/currency&gt; </li></ul><ul><li>&lt;currency&gt; </li></ul><ul><li>&lt;code&gt;usd&lt;/code&gt; </li></ul><ul><li>&lt;name&gt;Dollars&lt;/name&gt; </li></ul><ul><li>&lt;/currency&gt; </li></ul><ul><li>&lt;currency&gt; </li></ul><ul><li>&lt;code&gt;cad&lt;/code&gt; </li></ul><ul><li>&lt;name&gt;Canadian dollars&lt;/name&gt; </li></ul><ul><li>&lt;/currency&gt; </li></ul><ul><li>&lt;/currencies&gt; </li></ul>
  • 218. XSL File <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet </li></ul><ul><li>version=&amp;quot;1.0&amp;quot; </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:output method=&amp;quot;html&amp;quot; indent=&amp;quot;no&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:variable </li></ul><ul><li>name=&amp;quot;currencies&amp;quot; </li></ul><ul><li>select=&amp;quot;document(&apos;currencies.xml&apos;)/currencies&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt;&lt;title&gt;Multiple documents&lt;/title&gt;&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;table&gt; </li></ul><ul><li>&lt;tr bgcolor=&amp;quot;#999999&amp;quot;&gt; </li></ul><ul><li>&lt;td&gt;Name&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;Price&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;Description&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;Version&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;xsl:apply-templates/&gt; </li></ul><ul><li>&lt;/table&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;product&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:variable name=&amp;quot;currency&amp;quot; select=&amp;quot;price/@currency&amp;quot;/&gt; </li></ul><ul><li>&lt;tr&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;name&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;price&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:text&gt; &lt;/xsl:text&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;$currencies/currency[code=$currency]/name&amp;quot;/&gt; </li></ul><ul><li>&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;description&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;td&gt;&lt;xsl:value-of select=&amp;quot;version&amp;quot;/&gt;&lt;/td&gt; </li></ul><ul><li>&lt;/tr&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 219. Using XSLT and JavaScript Together (on products.xml)
  • 220. products.xsl modified <ul><li>&lt;?xml version=&amp;quot;1.0&amp;quot;?&gt; </li></ul><ul><li>&lt;xsl:stylesheet </li></ul><ul><li>xmlns:xsl=&amp;quot;http://www.w3.org/1999/XSL/Transform&amp;quot; </li></ul><ul><li>version=&amp;quot;1.0&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:output method=&amp;quot;html&amp;quot; indent=&amp;quot;no&amp;quot;/&gt; </li></ul><ul><li>&lt;xsl:template match=&amp;quot;/&amp;quot;&gt; </li></ul><ul><li>&lt;html&gt; </li></ul><ul><li>&lt;head&gt; </li></ul><ul><li>&lt;title&gt;JavaScript&lt;/title&gt; </li></ul><ul><li>&lt;script language=&amp;quot;JavaScript&amp;quot;&gt;&lt;xsl:comment&gt; </li></ul><ul><li>// creates and initializes an array of product descriptions </li></ul><ul><li>var urls = new Array() </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;products/product&amp;quot;&gt; </li></ul><ul><li>urls[&lt;xsl:value-of select=&amp;quot;position()&amp;quot;/&gt;] = </li></ul><ul><li>&amp;quot;&lt;xsl:value-of select=&amp;quot;@href&amp;quot;/&gt;&amp;quot; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>// user function </li></ul><ul><li>function doSelect(i) </li></ul><ul><li>{ </li></ul><ul><li>open(urls[i]) </li></ul><ul><li>} </li></ul><ul><li>// &lt;/xsl:comment&gt;&lt;/script&gt; </li></ul><ul><li>&lt;/head&gt; </li></ul><ul><li>&lt;body&gt; </li></ul><ul><li>&lt;ul&gt; </li></ul><ul><li>&lt;xsl:for-each select=&amp;quot;products/product&amp;quot;&gt; </li></ul><ul><li>&lt;li&gt;&lt;a href=&amp;quot;javascript:doSelect({position()})&amp;quot;&gt; </li></ul><ul><li>&lt;xsl:value-of select=&amp;quot;name&amp;quot;/&gt; </li></ul><ul><li>&lt;/a&gt;&lt;/li&gt; </li></ul><ul><li>&lt;/xsl:for-each&gt; </li></ul><ul><li>&lt;/ul&gt; </li></ul><ul><li>&lt;/body&gt; </li></ul><ul><li>&lt;/html&gt; </li></ul><ul><li>&lt;/xsl:template&gt; </li></ul><ul><li>&lt;/xsl:stylesheet&gt; </li></ul>
  • 221. Thank you! Any Questions?

×