• Like
5   xsl (formatting xml documents)
Upcoming SlideShare
Loading in...5
×

5 xsl (formatting xml documents)

  • 1,403 views
Uploaded on

XSL - Formatting XML Documents

XSL - Formatting XML Documents

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • Good information about style sheets..
    Are you sure you want to
    Your message goes here
    Be the first to like this
No Downloads

Views

Total Views
1,403
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

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