Your SlideShare is downloading. ×
0
Extensible Stylesheet Language (XSL) Atul Kahate [email_address]
Agenda <ul><li>Stylesheets </li></ul><ul><li>Cascading Style Sheets (CSS) </li></ul><ul><li>Extensible Stylesheet Language...
Style Sheets Basics <ul><li>XML concentrates on the  structure  of information </li></ul><ul><li>Normally, the  appearance...
Style Sheets <ul><li>Formatting instructions for XML documents are organized/grouped </li></ul><ul><li>These organized ins...
Standards For XML Styling <ul><li>Two main standards </li></ul><ul><li>Developed by W3C </li></ul><ul><ul><li>CSS (Cascadi...
CSS versus XSL <ul><li>CSS Approach </li></ul><ul><li>XSL Approach </li></ul>XML  Document CSS Style Sheet XML  Document X...
CSS (Cascading Style Sheet) <ul><li>Originally developed for HTML </li></ul><ul><li>Browsers that support XML also work we...
Need for CSS <ul><li>HTML is a display-oriented language </li></ul><ul><li>CSS was still developed in addition to HTML -- ...
CSS Terminology <ul><li>Style sheets are used to define rules </li></ul><ul><li>A rule is composed of two parts </li></ul>...
Example of CSS Syntax <ul><li>section.title </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times ...
DTD and Stylesheet Example - 1 <ul><li>Consider a MEMO DTD </li></ul><ul><li>… </li></ul><ul><li><!DOCTYPE MEMODTD [ </li>...
DTD and Stylesheet Example - 2 <ul><li>Suppose we want to do the following </li></ul><ul><ul><li>Display the elements TO, ...
DTD and Stylesheet Example - 3 <ul><li>CSS declaration follows </li></ul><ul><li>… </li></ul><ul><li>MEMO {font-family: “T...
Sample XML Document <ul><li><?xml version=“1.0”?> </li></ul><ul><li><?xml-stylesheet href=“article.css” type=“text/css”?> ...
Corresponding CSS Document <ul><li>/* a simple style sheet */ </li></ul><ul><li>article </li></ul><ul><li>{ </li></ul><ul>...
Output in Web Browser
Understanding CSS Example - 1 <ul><li>/* a simple style sheet */ </li></ul><ul><li>article </li></ul><ul><li>{ </li></ul><...
Understanding CSS Example - 2 <ul><li>article, p, title </li></ul><ul><li>{ </li></ul><ul><li>display: block; </li></ul><u...
Understanding CSS Example - 3 <ul><li>article title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 24pt; </li></ul><ul><...
Exercise <ul><li>Consider employee information to be stored in the following format in an XML file: </li></ul><ul><ul><li>...
Introduction to XSLT Concepts
Programming Language Classification <ul><li>Imperative Languages </li></ul><ul><ul><li>Set some variables, call methods, u...
Recursion <ul><li>Using recursion, imperative languages such as Java can behave like declarative languages such as XSLT </...
Recursion Illustrated
Recursion and XSL: When? <ul><li>When we have a set of repeating values in the source XML and we want the transformation r...
XSL Terminologies XSL, XSLT, XSLFO, XPath, XPointer, Stylesheet, Template, …
XSL (XML Stylesheet Language) <ul><li>Two parts </li></ul><ul><ul><li>XSLT (XSL Transformation) </li></ul></ul><ul><ul><li...
XPath <ul><li>Allows searching and navigation of XML documents </li></ul><ul><li>Can specify which parts of an XML documen...
XSLT Usage <ul><li>Styling </li></ul><ul><li>Add elements specific to viewing (e.g. logo) </li></ul><ul><li>Create new con...
XSLT Stylesheets <ul><li>An XSLT  stylesheet  consists of a series of  templates , together with instructions based on XPa...
XSLT Processing Concept Input XML document XSLT Processor Output XML document XSLT Template XSLT Stylesheet
XSLT Basics
Simple XSLT Example <ul><li>XML (test.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><l...
Change to the XML <ul><li>The XML document has an < xml-stylesheet>  tag, which informs the parser that we want to use an ...
Now look at the XSL <ul><li>The XSLT stylesheet is also a well-formed XML document </li></ul><ul><li>The <xsl:stylesheet> ...
Question <ul><li>What if our XML document has multiple occurrences of the  title  and  author  tags? </li></ul><ul><ul><li...
Modified XSL <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >  </li></ul><ul><li><xsl:template m...
Interesting Tricks – 1 <ul><li>XML (trick-1.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul>...
Answer <ul><li>The full XML contents </li></ul><ul><li>Why? </li></ul><ul><ul><li>If no template is specified, XSLT produc...
Interesting Tricks – 2 <ul><li>XML (trick-2.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul>...
Answer <ul><li>Now we have said, match root, but once root is matched, we say do nothing (since there is nothing between <...
Interesting Tricks – 3 <ul><li>XML (trick-3.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul>...
Answer <ul><li>Now, we try to find a match on the title element inside /. </li></ul><ul><li>But the path for title should ...
Interesting Tricks – 4 <ul><li>XML (trick-4.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul>...
Answer <ul><li>It would produce the contents of the title element, as expected, now </li></ul>
Template Basics
Usage of Templates <ul><li><xsl:template match = “…”> </li></ul><ul><ul><li>We know that this clause is used to match a pa...
Understanding <apply-templates> <ul><li>class.xml </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8...
How it Works? <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>  <?xml-stylesheet type=&quot;text/xsl&quot; href=...
Plain English Version <ul><li>For all the elements in the given XML </li></ul><ul><ul><li>If the current element = “studen...
Explanation <ul><li>The way this works is: </li></ul><ul><ul><li>Use a template if one defined </li></ul></ul><ul><ul><li>...
Notes on the Result – 1 <ul><li>Note that we would see display for  all  the student tags </li></ul><ul><li>However, in th...
Notes on the Result – 2 <ul><li>This style of coding is ambiguous! </li></ul><ul><li>It can lead to completely unexpected ...
Modified XML and XSL <ul><li>class1.xml </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>   </l...
Output <ul><li>Bye10000 </li></ul><ul><li>Why? </li></ul><ul><ul><li>The attempt is to find a match for the tag or element...
Further Modifications <ul><li>Modified XML </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>   ...
Still More Changes <ul><li>class3.xml </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>   </li>...
Output <ul><li>Found a learner!  </li></ul><ul><li>This is because we have suppressed the output for the salary tag now </...
Introducing apply-templates
Use of <apply-templates>  - Tricky <ul><li>class2.xml </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8...
Output <ul><li>Testone10000two20000three30000 </li></ul><ul><li>Reason: The logic works as: </li></ul><ul><ul><li>If there...
Suppressing Unwanted Output <ul><li>Modified XSL </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot...
Controlling the Output the Way We Want
XSL Changed – 1 <ul><li>Now changes the XSL to this: </li></ul><ul><li><?xml version=&quot;1.0&quot; ?>  </li></ul><ul><li...
Plain English Version <ul><li>For each element in the given XML </li></ul><ul><ul><li>If the current element is class </li...
Output of the second XSL <ul><li>Found a learner! Found a learner! Found a learner! </li></ul><ul><li>Explanation </li></u...
Beware of this Problem! <ul><li>Suppose the XSL was like this: </li></ul><ul><li><?xml version=&quot;1.0&quot; ?>  </li></...
Analysis <ul><li>The only change we have made is to remove the  select  attribute from  apply-templates </li></ul><ul><li>...
Plain English Version <ul><li>For each element in the given XML </li></ul><ul><ul><li>If the current element is class </li...
What about this? <ul><li>XML </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>   </li></ul><ul>...
Output <ul><li>SICSR Found a learner! Found a learner! Found a learner! Mr. Bean </li></ul><ul><li>Explanation </li></ul><...
Another Variation <ul><li>XML </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>   </li></ul><ul...
Summary <ul><li>Do not leave our code in an ambiguous state </li></ul><ul><li>This happens if we specify  <apply-templates...
Another XSLT Example – 1  <ul><li>Consider the following XML document </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </...
Another XSLT Example – 2  <ul><li>Here is the corresponding XSLT document </li></ul><ul><li><xsl:stylesheet version=&quot;...
Using Parameters in Templates
Using Parameters in Templates <ul><li><xsl:template name = &quot;print&quot; >  </li></ul><ul><li><xsl:param name = &quot;...
Calling Templates <ul><li><xsl:template match = &quot;/&quot; >  </li></ul><ul><li><xsl:call-template name = &quot;print&q...
Code <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet type=&q...
Attributes
Dealing with Attributes <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml...
Another Example <ul><li>XML </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet href=&quo...
A Detailed Example
discussionForumHome.xml <ul><li><? xml   version =&quot;1.0&quot;  encoding =&quot;utf-8&quot;?> </li></ul><ul><li><? xml-...
discussionForumHome.xsl <ul><li><? xml   version =&quot;1.0&quot;  encoding =&quot;utf-8&quot;?> </li></ul><ul><li>< xsl:s...
<xsl:template match = “/”> <ul><li>Read this as: Start processing the XML document at the root of the document </li></ul><...
Understanding  <xsl:template-match>  - 1 <ul><li>< xsl:template   match =&quot;/&quot;> </li></ul><ul><li>< html > </li></...
Understanding  <xsl:template-match>  - 2 <ul><li><xsl:template match> </li></ul><ul><ul><li>Has four optional attributes <...
Understanding  <xsl:template-match>  - 3 <ul><li>But the XSLT also has one more <xsl:template> element as follows: </li></...
<xsl:apply-templates> - 1 <ul><li>Tells the XSLT processor to  </li></ul><ul><ul><li>Begin a new search for elements in th...
<xsl:apply-templates> - 2 <ul><li>The <xsl:apply-templates> element works recursively </li></ul><ul><li>Here, it tells the...
<xsl:apply-templates> - 3 <ul><li>Assuming that the XSLT processor locates the <discussionForumHome> element, it searches ...
<xsl:template match = “messageBoard”> <ul><li>This template gets invoked for each instance of the <messageBoard> element <...
Summary <ul><li>Most transformation in  XSLT is driven by two elements, <xsl:template> and <xsl:apply-templates>. </li></u...
Comparing <xsl:template> to <xsl:apply-template> <ul><li>Think about the former as similar to a Java method definition, an...
Another Example <ul><li>college.xml </li></ul><ul><li><? xml   version =&quot;1.0&quot;  encoding =&quot;utf-8&quot;?> </l...
Explanation <ul><li>Elements that do not start with <xsl:> are simply copied into the result tree </li></ul><ul><li><xsl:v...
An Example for Conceptual Clarity <ul><li><customer> </li></ul><ul><ul><li><customer> (Many instance) </li></ul></ul><ul><...
Value-of select = ‘.’ <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-s...
Another Example –  Can be Confusing! <ul><li>What would be the output? </li></ul><ul><li>XML </li></ul><ul><ul><li><?xml v...
Explanation <ul><li>XSLT tries to process the first match (at the outermost possible level), i.e. on xslTutorial – There i...
Modified Example <ul><li>Is this correct now? </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li><?xml version=&quot;1.0...
Analysis <ul><li>No! </li></ul><ul><li>Inside an <xsl:template-match>, another <xsl:template match> cannot exist </li></ul...
Modified Example <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-styles...
Another Version <ul><li>XSL </li></ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&qu...
Modified Further <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;>  </li><...
Modified Further <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;>  </li><...
Yet Another Version –  Only middle name would get displayed after the complete name <ul><li><xsl:stylesheet xmlns:xsl='htt...
Controlling the Output Method <ul><li>Try adding  </li></ul><ul><ul><li><xsl:output method=“html” /> </li></ul></ul><ul><u...
More XSLT Examples
First XSL Example: Hello World! <ul><li>XML Document (HelloWorld.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ...
Second XSL example <ul><li>XML Document (second.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UT...
Third XSL Example <ul><li>XML document (third.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-...
XPath Basics
Nodes <ul><li>XPath views a document as a tree </li></ul><ul><li>This tree consists of nodes </li></ul><ul><li>One node ca...
Sample XML Document <ul><li><?xml version = “1.0”?> </li></ul><ul><li><!-- This is a comment --> </li></ul><ul><li><BOOK a...
XPath View Root Comment This is a comment Element BOOK Attribute author Andrew Tanenbaum Attribute edition 3 Element REVIE...
XPath Basics <ul><li>XPath allows selection of nodes/attributes based on certain conditions </li></ul><ul><li>XSLT process...
XPath Example <ul><li>child::chapter[child::title] </li></ul><ul><ul><li>Look for child nodes of the current node (called ...
XPath Examples – 1 If context node is the <book> element, an empty node-set would be returned. If the context node is <par...
XPath Examples – 2 If context node is the <chapter> 1 element, a node-set containing two attribute nodes would be returned...
XPath Examples – 3 NA Select the context node if it is the <paragraph> element. self:paragraph If the context node is <cha...
Unabbreviated relative location path Abbreviated relative location path Selection criteria child::BOOK BOOK Element nodes ...
Template Rules <ul><li>Describes how an XML element node is converted into an XSL element node for displaying </li></ul><u...
<xsl:value-of> Element <ul><li>The <xsl:value-of> element can be used to select the value of an XML element and add it to ...
XSLT Example <ul><li>Consider the following XML document </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><...
Exercise <ul><li>Write an XSLT document for the following XML document to display it as HTML </li></ul><ul><li><?xml versi...
Solution <ul><li><xsl:stylesheet version = “1.0” xmlns:xsl = “http://www/w3.org/1999/XSL/Transform”> </li></ul><ul><li><xs...
Exercise <ul><li>Consider the following XML file: </li></ul><ul><li><?xml version=“1.0”?> </li></ul><ul><li><BOOK> </li></...
Solution <ul><li><xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> </li></ul><ul><li><xsl:te...
Exercise <ul><li>Consider this XML and write an XSL to display only the book title and price </li></ul><ul><li><?xml versi...
Solution <ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li>...
Exercise <ul><li>Consider the following XML document, titled emp.xml: </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </...
Solution <ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li>...
Concept of apply-templates
apply-templates <ul><li>We know that  <xsl:template match=“…”>  allows us to search for a location path in our XML documen...
Source XML (apply.xml) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot...
Basic XSL Example (apply0.xml) <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&...
Explanation <ul><li>We have not specified what action should be taken when root is applied </li></ul><ul><li>This causes t...
XSL Example: 1 (apply1.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&qu...
Explanation <ul><li>Now there is an <apply-templates> inside the <template match> </li></ul><ul><li>This causes the defaul...
XSL Example: 2 (apply2.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&qu...
Explanation <ul><li>Empty output, since  <apply-templates/>  will bring search from the root level to all non-root element...
XSL Example: 3 (apply3.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&qu...
XSL Example: 4 (apply4.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&qu...
XSL Example: 6 (apply6.xsl)  - Tricky <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&q...
XSL Example: 5 (apply5.xsl) –  Tricky! <ul><li>What if we remove all search paths except the absolute ones? </li></ul><ul>...
Explanation <ul><li>Here, there is no <apply-templates> calling any of the defined <template match> tags </li></ul><ul><li...
Using the Mode Attribute
Using Mode <ul><li>If more than one template matches an identical pattern, a conflict arises </li></ul><ul><li>This can be...
Mode Example, also uses CSS <ul><li>XML (css-example-1) </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF...
Creating New Elements and Attributes
Creating Elements and Attributes <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><students> </li></ul><ul><li><s...
Corresponding XSL <ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quo...
Explanation - 1 <ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xs...
Explanation – 2 <ul><li><xsl:template match=&quot;students&quot;> </li></ul><ul><li><students> </li></ul><ul><li><xsl:appl...
Explanation – 3 <ul><li><xsl:template match=&quot;student&quot;> </li></ul><ul><li><xsl:element name=&quot;{@first_name}&q...
Explanation – 4 <ul><li><xsl:attribute name=&quot;id&quot;> </li></ul><ul><li><xsl:value-of select=&quot;id&quot;/> </li><...
Explanation – 5 <ul><li><notes> </li></ul><ul><li><xsl:value-of select=&quot;remarks&quot;/> </li></ul><ul><li></notes> </...
Explanation – 6 <ul><li>Input XML </li></ul><ul><ul><li><student first_name=&quot;Raju&quot;> </li></ul></ul><ul><ul><li><...
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
5   xsl (formatting xml documents)
Upcoming SlideShare
Loading in...5
×

5 xsl (formatting xml documents)

1,581

Published on

XSL - Formatting XML Documents

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

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

No notes for slide

Transcript of "5 xsl (formatting xml documents)"

  1. 1. Extensible Stylesheet Language (XSL) Atul Kahate [email_address]
  2. 2. Agenda <ul><li>Stylesheets </li></ul><ul><li>Cascading Style Sheets (CSS) </li></ul><ul><li>Extensible Stylesheet Language (XSL) </li></ul><ul><li>XSL Transformations (XSLT) </li></ul>
  3. 3. Style Sheets Basics <ul><li>XML concentrates on the structure of information </li></ul><ul><li>Normally, the appearance of information is secondary </li></ul><ul><li>Sometimes, it is necessary to format XML information so as to view it in a particular manner </li></ul><ul><li>This is called as styling an XML document </li></ul>
  4. 4. Style Sheets <ul><li>Formatting instructions for XML documents are organized/grouped </li></ul><ul><li>These organized instructions are called as style sheets </li></ul><ul><li>A style sheet can be applied to view a particular XML document in a specific format </li></ul>
  5. 5. Standards For XML Styling <ul><li>Two main standards </li></ul><ul><li>Developed by W3C </li></ul><ul><ul><li>CSS (Cascading Style Sheet) </li></ul></ul><ul><ul><li>XSL (XML Stylesheet Language) </li></ul></ul>
  6. 6. CSS versus XSL <ul><li>CSS Approach </li></ul><ul><li>XSL Approach </li></ul>XML Document CSS Style Sheet XML Document XSL Style Sheet HTML Document
  7. 7. CSS (Cascading Style Sheet) <ul><li>Originally developed for HTML </li></ul><ul><li>Browsers that support XML also work well with CSS </li></ul><ul><li>CSS </li></ul><ul><ul><li>Set of rules that tells the browser about display information </li></ul></ul><ul><ul><li>e.g. which fonts to use, what size, style, margins, etc </li></ul></ul>
  8. 8. Need for CSS <ul><li>HTML is a display-oriented language </li></ul><ul><li>CSS was still developed in addition to HTML -- Why? </li></ul><ul><li>HTML became quite complex over the years </li></ul><ul><li>Adding more and more elements to HTML was not a viable option </li></ul><ul><li>CSS was developed to separate HTML elements from precisely how they should be styled </li></ul>
  9. 9. CSS Terminology <ul><li>Style sheets are used to define rules </li></ul><ul><li>A rule is composed of two parts </li></ul><ul><ul><li>Selector (Element to which the rule applies) </li></ul></ul><ul><ul><li>Declaration (Attributes and values) </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>PARA {FONT: 12pt “Times New Roman”} </li></ul></ul><ul><ul><li>Here, PARA is the selector, the rest is the declaration </li></ul></ul>
  10. 10. Example of CSS Syntax <ul><li>section.title </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times New Roman”, serif; </li></ul><ul><li>font-size: 10pt; </li></ul><ul><li>margin: 5px; </li></ul><ul><li>display: block; </li></ul><ul><li>font-style: italic </li></ul><ul><li>} </li></ul>
  11. 11. DTD and Stylesheet Example - 1 <ul><li>Consider a MEMO DTD </li></ul><ul><li>… </li></ul><ul><li><!DOCTYPE MEMODTD [ </li></ul><ul><li><!ELEMENT MEMO (TO, FROM, SUBJECT, BODY)> </li></ul><ul><li><!ELEMENT TO (#PCDATA)> </li></ul><ul><li><!ELEMENT FROM (#PCDATA)> </li></ul><ul><li><!ELEMENT SUBJECT (#PCDATA)> </li></ul><ul><li><!ELEMENT BODY (#PCDATA)> </li></ul><ul><li>]> </li></ul>
  12. 12. DTD and Stylesheet Example - 2 <ul><li>Suppose we want to do the following </li></ul><ul><ul><li>Display the elements TO, FROM, and SUBJECT in bold along with their contents </li></ul></ul><ul><ul><li>Display the entire memo in Times New Roman font with size as 12 points, and margin of 1 line </li></ul></ul><ul><li>Use the stylesheet as follows </li></ul>
  13. 13. DTD and Stylesheet Example - 3 <ul><li>CSS declaration follows </li></ul><ul><li>… </li></ul><ul><li>MEMO {font-family: “Times New Roman”, “Book Antigua”, serif; </li></ul><ul><li>font-size: 12pt; </li></ul><ul><li>margin: lin </li></ul><ul><li>} </li></ul><ul><li>TO, FROM, SUBJECT {font-weight: bold} </li></ul>
  14. 14. Sample XML Document <ul><li><?xml version=“1.0”?> </li></ul><ul><li><?xml-stylesheet href=“article.css” type=“text/css”?> </li></ul><ul><li><article> </li></ul><ul><li><title> Style Sheets Example </title> </li></ul><ul><li><section> </li></ul><ul><li><p> This example illustrates how style sheets can be applied to XML documents. </p> </li></ul><ul><li></section> </li></ul><ul><li><section> </li></ul><ul><li><title> Styling </title> </li></ul><ul><li><p> Style sheets format XML documents in a desired manner. </p> </li></ul><ul><li></section> </li></ul><ul><li></article> </li></ul>
  15. 15. Corresponding CSS Document <ul><li>/* a simple style sheet */ </li></ul><ul><li>article </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times New Roman”, serif; </li></ul><ul><li>font-size: 18pt; </li></ul><ul><li>margin: 5px </li></ul><ul><li>} </li></ul><ul><li>article, p, title </li></ul><ul><li>{ </li></ul><ul><li>display: block; </li></ul><ul><li>margin-bottom: 10px </li></ul><ul><li>} </li></ul><ul><li>article title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 24pt; </li></ul><ul><li>font-weight: bold </li></ul><ul><li>} </li></ul><ul><li>section title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 20pt; </li></ul><ul><li>font-style: italic </li></ul><ul><li>} </li></ul>
  16. 16. Output in Web Browser
  17. 17. Understanding CSS Example - 1 <ul><li>/* a simple style sheet */ </li></ul><ul><li>article </li></ul><ul><li>{ </li></ul><ul><li>font-family: Palatino, Garamond, “Times New Roman”, serif; </li></ul><ul><li>font-size: 18pt; </li></ul><ul><li>margin: 5px; </li></ul><ul><li>} </li></ul><ul><li>Comments are enclosed between /* and */ </li></ul><ul><li>The above declaration specifies the formatting of the entire contents of the article element </li></ul>
  18. 18. Understanding CSS Example - 2 <ul><li>article, p, title </li></ul><ul><li>{ </li></ul><ul><li>display: block; </li></ul><ul><li>margin-bottom: 10px; </li></ul><ul><li>} </li></ul><ul><li>In addition to the basic definition applied to the article element, this defines more styles for article , p , and title elements </li></ul>
  19. 19. Understanding CSS Example - 3 <ul><li>article title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 24pt; </li></ul><ul><li>font-weight: bold; </li></ul><ul><li>} </li></ul><ul><li>section title </li></ul><ul><li>{ </li></ul><ul><li>font-size: 20pt; </li></ul><ul><li>font-style: italic; </li></ul><ul><li>} </li></ul><ul><li>Specify attributes for the title element within article and within section separately </li></ul>
  20. 20. Exercise <ul><li>Consider employee information to be stored in the following format in an XML file: </li></ul><ul><ul><li>Employee ID </li></ul></ul><ul><ul><li>Employee Name </li></ul></ul><ul><ul><li>Salary </li></ul></ul><ul><ul><li>Department </li></ul></ul><ul><li>Create CSS to use different fonts, font sizes, font colors as per your choice </li></ul>
  21. 21. Introduction to XSLT Concepts
  22. 22. Programming Language Classification <ul><li>Imperative Languages </li></ul><ul><ul><li>Set some variables, call methods, use operators that change value, etc </li></ul></ul><ul><ul><li>C, C++, Java, C# </li></ul></ul><ul><li>Declarative or Functional Languages </li></ul><ul><ul><li>Perform logical operations using declarations, rather than writing code </li></ul></ul><ul><ul><li>Prolog, XSLT </li></ul></ul>
  23. 23. Recursion <ul><li>Using recursion, imperative languages such as Java can behave like declarative languages such as XSLT </li></ul><ul><li>Example </li></ul><ul><ul><li>public int factorial(int number) { </li></ul></ul><ul><ul><li>if (number <= 1) return 1; </li></ul></ul><ul><ul><li>return number * factorial(number-1); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  24. 24. Recursion Illustrated
  25. 25. Recursion and XSL: When? <ul><li>When we have a set of repeating values in the source XML and we want the transformation result to reflect something about all of those values. For example, if you have a catalog of items in XML and want to present those items along with the price for all of the items, you would have to find that total price using a recursive template. </li></ul><ul><li>When the source XML contains a number x in a tag, for example <countTo number=&quot;5&quot;/>, and you want to present some information that same x number of times in the transformation output. </li></ul>
  26. 26. XSL Terminologies XSL, XSLT, XSLFO, XPath, XPointer, Stylesheet, Template, …
  27. 27. XSL (XML Stylesheet Language) <ul><li>Two parts </li></ul><ul><ul><li>XSLT (XSL Transformation) </li></ul></ul><ul><ul><li>XSL-FO (XSL Formatting Objects) </li></ul></ul><ul><li>XSLFO is similar to CSS, quite complex </li></ul><ul><li>We will discuss XSLT in detail, XSL-FO in brief </li></ul>
  28. 28. XPath <ul><li>Allows searching and navigation of XML documents </li></ul><ul><li>Can specify which parts of an XML document we want to transform </li></ul><ul><li>Used heavily in XSLT for searching of information </li></ul>
  29. 29. XSLT Usage <ul><li>Styling </li></ul><ul><li>Add elements specific to viewing (e.g. logo) </li></ul><ul><li>Create new content from existing one (e.g. TOC) </li></ul><ul><li>Present information with the right level of details (e.g. overview for managers, details for staff) </li></ul><ul><li>Convert between different DTDs/schemas or different versions of a DTD/schema </li></ul><ul><li>Transform XML documents into HTML for compatibility with older browsers </li></ul>
  30. 30. XSLT Stylesheets <ul><li>An XSLT stylesheet consists of a series of templates , together with instructions based on XPath </li></ul><ul><li>Tell an XSLT processor how to match the template against the nodes in an XML input document </li></ul><ul><li>For each template, the processor reads the input document for all matching patterns and produces an output document </li></ul><ul><li>See next slide </li></ul>
  31. 31. XSLT Processing Concept Input XML document XSLT Processor Output XML document XSLT Template XSLT Stylesheet
  32. 32. XSLT Basics
  33. 33. Simple XSLT Example <ul><li>XML (test.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;test.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><title>XSL</title> </li></ul></ul><ul><ul><li><author>John Smith</author> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSLT (test.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul></ul><ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;> </li></ul></ul><ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul></ul><ul><ul><li><H1><xsl:value-of select=&quot;//title&quot;/></H1> </li></ul></ul><ul><ul><li><H2><xsl:value-of select=&quot;//author&quot;/></H2> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  34. 34. Change to the XML <ul><li>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 </li></ul>
  35. 35. Now look at the XSL <ul><li>The XSLT stylesheet is also a well-formed XML document </li></ul><ul><li>The <xsl:stylesheet> element has two attributes </li></ul><ul><ul><li>Version specifies the XSLT specifications version </li></ul></ul><ul><ul><li>Declares the namespace </li></ul></ul>
  36. 36. Question <ul><li>What if our XML document has multiple occurrences of the title and author tags? </li></ul><ul><ul><li>We would still see only the first occurrence, since we have not yet seen the recursion part of XSLT </li></ul></ul>
  37. 37. Modified XSL <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><H2><xsl:value-of select=&quot;//author&quot;/></H2> </li></ul><ul><li><H1><xsl:value-of select=&quot;//title&quot;/></H1> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  38. 38. Interesting Tricks – 1 <ul><li>XML (trick-1.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=“trick-1.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><title>XSL</title> </li></ul></ul><ul><ul><li><author>John Smith</author> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL (trick-1.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul></ul><ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul><ul><li>What would be the output? </li></ul>
  39. 39. Answer <ul><li>The full XML contents </li></ul><ul><li>Why? </li></ul><ul><ul><li>If no template is specified, XSLT produces the complete XML as output! </li></ul></ul>
  40. 40. Interesting Tricks – 2 <ul><li>XML (trick-2.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=“trick-2.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><title>XSL</title> </li></ul></ul><ul><ul><li><author>John Smith</author> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL (trick-2.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul></ul><ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;> </li></ul></ul><ul><ul><li><xsl:template match=“/”> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul><ul><li>What would be the output? </li></ul>
  41. 41. Answer <ul><li>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 </li></ul><ul><li>Hence, output is empty </li></ul>
  42. 42. Interesting Tricks – 3 <ul><li>XML (trick-3.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=“trick-3.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><title>XSL</title> </li></ul></ul><ul><ul><li><author>John Smith</author> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL (trick-3.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul></ul><ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;> </li></ul></ul><ul><ul><li><xsl:template match=“/”> </li></ul></ul><ul><ul><li><xsl:value-of select = “title” /> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul><ul><li>What would be the output? </li></ul>
  43. 43. Answer <ul><li>Now, we try to find a match on the title element inside /. </li></ul><ul><li>But the path for title should be /xslTutorial/title </li></ul><ul><li>Hence, output would be empty </li></ul>
  44. 44. Interesting Tricks – 4 <ul><li>XML (trick-4.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=“trick-4.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><title>XSL</title> </li></ul></ul><ul><ul><li><author>John Smith</author> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL (trick-4.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul></ul><ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot;> </li></ul></ul><ul><ul><li><xsl:template match=“/”> </li></ul></ul><ul><ul><li><xsl:value-of select = “/xslTutorial/title” /> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul><ul><li>What would be the output? </li></ul>
  45. 45. Answer <ul><li>It would produce the contents of the title element, as expected, now </li></ul>
  46. 46. Template Basics
  47. 47. Usage of Templates <ul><li><xsl:template match = “…”> </li></ul><ul><ul><li>We know that this clause is used to match a particular tag from an XML file and to do processing, accordingly </li></ul></ul><ul><li><xsl:template name = “…”> </li></ul><ul><ul><li>Allows us to define a template </li></ul></ul><ul><ul><li>Once such a template is defined, we can use <xsl:call-template name = “…”> to call that defined template </li></ul></ul>
  48. 48. Understanding <apply-templates> <ul><li>class.xml </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;class.xsl&quot;?> </li></ul></ul><ul><ul><li><class> </li></ul></ul><ul><ul><li><student>Jack</student> </li></ul></ul><ul><ul><li><student>Harry</student> </li></ul></ul><ul><ul><li><student>Rebecca</student> </li></ul></ul><ul><ul><li><teacher>Mr. Bean</teacher> </li></ul></ul><ul><ul><li></class> </li></ul></ul><ul><li>Class.xsl </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul></ul><ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul></ul><ul><ul><li><xsl:template match=“student&quot;> Found a learner! </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul><ul><li>Output </li></ul><ul><ul><li>Found a learner! Found a learner! Found a learner! Mr. Bean </li></ul></ul>
  49. 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. 50. Plain English Version <ul><li>For all the elements in the given XML </li></ul><ul><ul><li>If the current element = “student” </li></ul></ul><ul><ul><ul><li>Display “Found a learner” </li></ul></ul></ul><ul><ul><li>Else </li></ul></ul><ul><ul><ul><li>Display the actual contents of the element </li></ul></ul></ul><ul><ul><li>End-if </li></ul></ul><ul><li>End-For </li></ul>
  51. 51. Explanation <ul><li>The way this works is: </li></ul><ul><ul><li>Use a template if one defined </li></ul></ul><ul><ul><li>Blindly output the contents of the elements wherever there is no template defined </li></ul></ul><ul><li>For each <template match = “student”>, we display the text Found a learner! instead of the student name itself. </li></ul><ul><li>However, for the teacher tag, there is no <template match>. Therefore, its contents are displayed as they are. </li></ul>
  52. 52. Notes on the Result – 1 <ul><li>Note that we would see display for all the student tags </li></ul><ul><li>However, in the first example, we would have seen the output only for the first instance of title and author tags </li></ul><ul><li>Why? </li></ul><ul><ul><li>In the earlier example, there was no template , i.e. no recursion – the syntax used was value-of select </li></ul></ul><ul><ul><li>Now, we use a template , which introduces recursion </li></ul></ul>
  53. 53. Notes on the Result – 2 <ul><li>This style of coding is ambiguous! </li></ul><ul><li>It can lead to completely unexpected results </li></ul><ul><li>Let us modify our XML and XSL now </li></ul>
  54. 54. Modified XML and XSL <ul><li>class1.xml </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;class1.xsl&quot;?> </li></ul><ul><li>< class > </li></ul><ul><li>< dept > Bye </ dept > </li></ul><ul><li>< salary > 10000 </ salary > </li></ul><ul><li></ class > </li></ul><ul><li>class1.xsl </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:template match =&quot; hello &quot;> Found a learner! </ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul><ul><li>What would be the output? </li></ul>
  55. 55. Output <ul><li>Bye10000 </li></ul><ul><li>Why? </li></ul><ul><ul><li>The attempt is to find a match for the tag or element hello in our XML document, which is not found </li></ul></ul><ul><ul><li>For other tags in the XML document (i.e. dept and salary), there is no template defined; so no special processing is needed for them, except blindly outputting their contents, as before! </li></ul></ul>
  56. 56. Further Modifications <ul><li>Modified XML </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;class1.xsl&quot;?> </li></ul><ul><li>< class > </li></ul><ul><li>< dept > Bye </ dept > </li></ul><ul><li>< salary > 10000 </ salary > </li></ul><ul><li>< name > </li></ul><ul><li>< first > test </ first > </li></ul><ul><li>< last > test </ last > </li></ul><ul><li></ name > </li></ul><ul><li></ class > </li></ul><ul><li>Resulting Output </li></ul><ul><li>Bye10000testtest </li></ul><ul><li>This is based on same logic as earlier </li></ul>
  57. 57. Still More Changes <ul><li>class3.xml </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;class1.xsl&quot;?> </li></ul><ul><li>< class > </li></ul><ul><li>< dept > Bye </ dept > </li></ul><ul><li>< salary > 10000 </ salary ></ class > </li></ul><ul><li>class3.xsl </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:template match =&quot; dept &quot;> </li></ul><ul><li>Found a learner! </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot; salary &quot;> </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  58. 58. Output <ul><li>Found a learner! </li></ul><ul><li>This is because we have suppressed the output for the salary tag now </li></ul>
  59. 59. Introducing apply-templates
  60. 60. Use of <apply-templates> - Tricky <ul><li>class2.xml </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; </li></ul><ul><li>href=&quot;class2.xsl&quot;?> </li></ul><ul><li><class> </li></ul><ul><li><college>test</college> </li></ul><ul><li><dept>one</dept> </li></ul><ul><li><salary>10000</salary> </li></ul><ul><li><dept>two</dept> </li></ul><ul><li><salary>20000</salary> </li></ul><ul><li><dept>three</dept> </li></ul><ul><li><salary>30000</salary></class> </li></ul><ul><li>Class2.xsl </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:template match =“ dept &quot;> <xsl:apply-templates /> </ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  61. 61. Output <ul><li>Testone10000two20000three30000 </li></ul><ul><li>Reason: The logic works as: </li></ul><ul><ul><li>If there is at least one dept tag </li></ul></ul><ul><ul><ul><li><apply-templates> (which means, display the default output as it is, which means everything) </li></ul></ul></ul>
  62. 62. Suppressing Unwanted Output <ul><li>Modified XSL </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:template match =&quot; dept &quot;> </li></ul><ul><li>< xsl:apply-templates /> </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot; salary &quot;> </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  63. 63. Controlling the Output the Way We Want
  64. 64. XSL Changed – 1 <ul><li>Now changes the XSL to this: </li></ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;class&quot;> </li></ul><ul><li><xsl:apply-templates select=&quot;student&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;student&quot;> </li></ul><ul><li>Found a learner! </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul><ul><li>What would be the output? See next slide. </li></ul>
  65. 65. Plain English Version <ul><li>For each element in the given XML </li></ul><ul><ul><li>If the current element is class </li></ul></ul><ul><ul><ul><li>If the child element of the current element is student </li></ul></ul></ul><ul><ul><ul><ul><li>Output Found a learner </li></ul></ul></ul></ul><ul><ul><ul><li>End-if </li></ul></ul></ul><ul><ul><li>End-if </li></ul></ul><ul><li>End-for </li></ul><ul><li>Note: There is no Else now! </li></ul>
  66. 66. Output of the second XSL <ul><li>Found a learner! Found a learner! Found a learner! </li></ul><ul><li>Explanation </li></ul><ul><ul><li><xsl:template match=&quot;class&quot;> </li></ul></ul><ul><ul><ul><li>The XSLT processor begins at the root element when looking for template matches. It finds a match for the root element class . </li></ul></ul></ul><ul><ul><li><xsl:apply-templates select=&quot;student&quot;/> </li></ul></ul><ul><ul><ul><li>In our template that matched class we use xsl:apply-templates which will check for template matches on all the children of class . The children of class in our XML document are student and teacher . </li></ul></ul></ul><ul><ul><ul><li>To have the teacher element &quot;Mr. Bean&quot; ignored, we use the select attribute of xsl:apply-templates to specify only student children. </li></ul></ul></ul><ul><ul><ul><li>The XSLT processor then goes searching templates that only match student elements. </li></ul></ul></ul><ul><ul><li><xsl:template match=&quot;student“> </li></ul></ul><ul><ul><ul><li>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. </li></ul></ul></ul><ul><ul><ul><li>XSLT then finishes its processing and we are left with XSLT output that has eliminated the unwanted text, &quot;Mr. Bean!&quot; </li></ul></ul></ul>
  67. 67. Beware of this Problem! <ul><li>Suppose the XSL was like this: </li></ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;class&quot;> </li></ul><ul><li><xsl:apply-templates /> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;student&quot;> </li></ul><ul><li>Found a learner! </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul><ul><li>What would be the output? See next slide. </li></ul>
  68. 68. Analysis <ul><li>The only change we have made is to remove the select attribute from apply-templates </li></ul><ul><li>This would translate to something different! </li></ul><ul><li>See next slide </li></ul>
  69. 69. Plain English Version <ul><li>For each element in the given XML </li></ul><ul><ul><li>If the current element is class </li></ul></ul><ul><ul><ul><li>If the child element of the current element is student </li></ul></ul></ul><ul><ul><ul><ul><li>Output Found a learner </li></ul></ul></ul></ul><ul><ul><ul><li>Else </li></ul></ul></ul><ul><ul><ul><ul><li>Output the contents of the current element as they are </li></ul></ul></ul></ul><ul><ul><ul><li>End-if </li></ul></ul></ul><ul><ul><li>End-if </li></ul></ul><ul><li>End-for </li></ul><ul><li>Note: There is an Else now! </li></ul>
  70. 70. What about this? <ul><li>XML </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;class.xsl&quot;?> </li></ul><ul><li>< college > </li></ul><ul><li>< class > </li></ul><ul><li>< institution > SICSR </ institution > </li></ul><ul><li>< student > Jack </ student > </li></ul><ul><li>< student > Harry </ student > </li></ul><ul><li>< student > Rebecca </ student > </li></ul><ul><li>< teacher > Mr. Bean </ teacher > </li></ul><ul><li></ class > </li></ul><ul><li></ college > </li></ul><ul><li>XSL </li></ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:template match =&quot; class &quot;> </li></ul><ul><li>< xsl:apply-templates /> </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot; student &quot;> </li></ul><ul><li>Found a learner! </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  71. 71. Output <ul><li>SICSR Found a learner! Found a learner! Found a learner! Mr. Bean </li></ul><ul><li>Explanation </li></ul><ul><ul><li>The same logic as earlier applies </li></ul></ul><ul><ul><li>If a match is found, do something; else display contents of current element blindly </li></ul></ul><ul><ul><li>Remember, we do not have a select attribute in the template </li></ul></ul><ul><ul><li>If we have it, what would happen? </li></ul></ul>
  72. 72. Another Variation <ul><li>XML </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;class.xsl&quot;?> </li></ul><ul><li>< college > </li></ul><ul><li>< class > </li></ul><ul><li>< institution > SICSR </ institution > </li></ul><ul><li>< student > Jack </ student > </li></ul><ul><li>< student > Harry </ student > </li></ul><ul><li>< student > Rebecca </ student > </li></ul><ul><li>< teacher > Mr. Bean </ teacher > </li></ul><ul><li></ class > </li></ul><ul><li></ college > </li></ul><ul><li>XSL </li></ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:template match =&quot; class &quot;> </li></ul><ul><li>< xsl:apply-templates select =&quot; student &quot;/> </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot; student &quot;> </li></ul><ul><li>Found a learner! </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul><ul><li>Output </li></ul><ul><li>Found a learner! Found a learner! Found a learner! </li></ul>
  73. 73. Summary <ul><li>Do not leave our code in an ambiguous state </li></ul><ul><li>This happens if we specify <apply-templates> without any specific select attribute </li></ul><ul><li>It can also happen if we do not specify exact selection criteria inside <template match> </li></ul><ul><li>Output may not be as expected! </li></ul>
  74. 74. Another XSLT Example – 1 <ul><li>Consider the following XML document </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;pune.xsl&quot;?> </li></ul><ul><li><content> </li></ul><ul><li><about> PUNE </about> </li></ul><ul><li><city> </li></ul><ul><li><line1> Pune is a lovely city </line1> </li></ul><ul><li><line2> The education facilities are as best as you can get </line2> </li></ul><ul><li><line3> And the weather is great, too. </line3> </li></ul><ul><li></city> </li></ul><ul><li></content> </li></ul>
  75. 75. Another XSLT Example – 2 <ul><li>Here is the corresponding XSLT document </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;content&quot;> </li></ul><ul><li><html> </li></ul><ul><li><head><title>Welcome to Pune!</title></head> </li></ul><ul><li><body> </li></ul><ul><li><h1><xsl:value-of select=&quot;about&quot;/></h1> </li></ul><ul><li><h2><xsl:value-of select=&quot;city/line1&quot;/></h2> </li></ul><ul><li><h3><xsl:value-of select=&quot;city/line2&quot;/></h3> </li></ul><ul><li><h4><xsl:value-of select=&quot;city/line3&quot;/></h4> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  76. 76. Using Parameters in Templates
  77. 77. Using Parameters in Templates <ul><li><xsl:template name = &quot;print&quot; > </li></ul><ul><li><xsl:param name = &quot;A&quot; /> </li></ul><ul><li><xsl:param name = &quot;B&quot; >111</xsl:param> </li></ul><ul><li><xsl:value-of select = &quot;$A&quot; /> </li></ul><ul><li><xsl:text > + </xsl:text> </li></ul><ul><li><xsl:value-of select = &quot;$B&quot; /> </li></ul><ul><li><xsl:text > = </xsl:text> </li></ul><ul><li><xsl:value-of select = &quot;$A+$B&quot; /> </li></ul><ul><li></xsl:template> </li></ul><ul><li>This defines a template (like a method) called as print </li></ul><ul><li>This template can receive two parameters, named A and B. B has a default value of 111. </li></ul><ul><li>The template prints the following as the output: </li></ul><ul><li>Value of A + Value of B = Sum of A and B </li></ul><ul><li>For example, if a caller calls this template with A = 10 and B = 20, the output will be: </li></ul><ul><li>10 + 20 = 30 </li></ul>
  78. 78. Calling Templates <ul><li><xsl:template match = &quot;/&quot; > </li></ul><ul><li><xsl:call-template name = &quot;print&quot; > </li></ul><ul><li> <xsl:with-param name = &quot;A&quot; >11</xsl:with-param> </li></ul><ul><li> <xsl:with-param name = &quot;B&quot; >33</xsl:with-param> </li></ul><ul><li></xsl:call-template> </li></ul><ul><li><xsl:call-template name = &quot;print&quot; > </li></ul><ul><li> <xsl:with-param name = &quot;A&quot; >55</xsl:with-param> </li></ul><ul><li></xsl:call-template> </li></ul><ul><li></xsl:template> </li></ul><ul><li>Now, we are calling the template defined earlier ( print ) twice in succession: first time passing A = 11 and B = 33; and second time passing A = 55 and not passing any value for B </li></ul><ul><li>Output (Full code on next slide): </li></ul><ul><li>11 + 33 = 44 </li></ul><ul><li>55 + 111 = 166 </li></ul><ul><li>Note : 111 was B’s default value in the print template on the previous slide </li></ul>
  79. 79. Code <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;1.xsl&quot; ?> </li></ul></ul><ul><ul><li><AAA > </li></ul></ul><ul><ul><li><BBB>bbb </BBB> </li></ul></ul><ul><ul><li><CCC>ccc </CCC> </li></ul></ul><ul><ul><li></AAA> </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li><xsl:stylesheet xmlns:xsl = &quot;http://www.w3.org/1999/XSL/Transform&quot; version = &quot;1.0&quot; > </li></ul></ul><ul><ul><li><xsl:output method = &quot;text&quot; /> </li></ul></ul><ul><ul><li><xsl:template match = &quot;/&quot; > </li></ul></ul><ul><ul><li><xsl:call-template name = &quot;print&quot; > </li></ul></ul><ul><ul><li><xsl:with-param name = &quot;A&quot; >11</xsl:with-param> </li></ul></ul><ul><ul><li><xsl:with-param name = &quot;B&quot; >33</xsl:with-param> </li></ul></ul><ul><ul><li></xsl:call-template> </li></ul></ul><ul><ul><li><xsl:call-template name = &quot;print&quot; > </li></ul></ul><ul><ul><li><xsl:with-param name = &quot;A&quot; >55</xsl:with-param> </li></ul></ul><ul><ul><li></xsl:call-template> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li><xsl:template name = &quot;print&quot; > </li></ul></ul><ul><ul><li><xsl:param name = &quot;A&quot; /> </li></ul></ul><ul><ul><li><xsl:param name = &quot;B&quot; >111</xsl:param> </li></ul></ul><ul><ul><li><xsl:text > </li></ul></ul><ul><ul><li></xsl:text> </li></ul></ul><ul><ul><li><xsl:value-of select = &quot;$A&quot; /> </li></ul></ul><ul><ul><li><xsl:text > + </xsl:text> </li></ul></ul><ul><ul><li><xsl:value-of select = &quot;$B&quot; /> </li></ul></ul><ul><ul><li><xsl:text > = </xsl:text> </li></ul></ul><ul><ul><li><xsl:value-of select = &quot;$A+$B&quot; /> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  80. 80. Attributes
  81. 81. Dealing with Attributes <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;test.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><dog name='Joe'> </li></ul></ul><ul><ul><li><data weight='18 kg' color=&quot;black&quot;/> </li></ul></ul><ul><ul><li></dog> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </li></ul></ul><ul><ul><li><xsl:template match=&quot;dog&quot;> </li></ul></ul><ul><ul><li><P><B><xsl:text> Dog: </xsl:text> </B> </li></ul></ul><ul><ul><li><xsl:value-of select=&quot;@name&quot;/></P> </li></ul></ul><ul><ul><li><P><B><xsl:text> Color: </xsl:text> </B> </li></ul></ul><ul><ul><li><xsl:value-of select=&quot;data/@color&quot;/></P> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  82. 82. Another Example <ul><li>XML </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet href=&quot;products.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul><ul><li><card type=&quot;simple&quot;> </li></ul><ul><li><name>John Doe</name> </li></ul><ul><li><title>CEO, Widget Inc.</title> </li></ul><ul><li><email>john.doe@widget.com</email> </li></ul><ul><li><phone>(202) 456-1414</phone> </li></ul><ul><li></card> </li></ul><ul><li>XSL </li></ul><ul><li><xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; version=&quot;1.0&quot; </li></ul><ul><li>xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> </li></ul><ul><li><xsl:template match=&quot;card[@type='simple']&quot;> </li></ul><ul><li><html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;> </li></ul><ul><li><title>business card</title><body> </li></ul><ul><li><xsl:apply-templates select=&quot;name&quot;/> </li></ul><ul><li><xsl:apply-templates select=&quot;title&quot;/> </li></ul><ul><li><xsl:apply-templates select=&quot;email&quot;/> </li></ul><ul><li><xsl:apply-templates select=&quot;phone&quot;/> </li></ul><ul><li></body></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;card/name&quot;> </li></ul><ul><li><h1><xsl:value-of select=&quot;text()&quot;/></h1> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;email&quot;> </li></ul><ul><li><p>email: <a href=&quot;mailto:{text()}&quot;><tt> </li></ul><ul><li><xsl:value-of select=&quot;text()&quot;/> </li></ul><ul><li></tt></a></p> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  83. 83. A Detailed Example
  84. 84. discussionForumHome.xml <ul><li><? xml version =&quot;1.0&quot; encoding =&quot;utf-8&quot;?> </li></ul><ul><li><? xml-stylesheet href=&quot;discussionForumHome.xsl&quot; type=&quot;text/xsl&quot; ?> </li></ul><ul><li>< disussionForumHome > </li></ul><ul><li>< messageBoard id =&quot;1&quot; name =&quot;Java Programming&quot;/> </li></ul><ul><li>< messageBoard id =&quot;2&quot; name =&quot;XML Programming&quot;/> </li></ul><ul><li>< messageBoard id =&quot;3&quot; name =&quot;XSLT Programming&quot;/> </li></ul><ul><li></ disussionForumHome > </li></ul>
  85. 85. discussionForumHome.xsl <ul><li><? xml version =&quot;1.0&quot; encoding =&quot;utf-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot;1.0&quot; </li></ul><ul><li>xmlns:xsl =&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li>< xsl:template match =&quot;/&quot;> </li></ul><ul><li>< html > </li></ul><ul><li>< head > </li></ul><ul><li>< title >Discussion Forum Home Page</ title > </li></ul><ul><li></ head > </li></ul><ul><li>< body > </li></ul><ul><li>< h1 >Discussion Forum Home Page</ h1 > </li></ul><ul><li>< h3 >Please select a message board to view:</ h3 > </li></ul><ul><li>< ul > </li></ul><ul><li>< xsl:apply-templates select =&quot;disussionForumHome/messageBoard&quot;/> </li></ul><ul><li></ ul > </li></ul><ul><li></ body > </li></ul><ul><li></ html > </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot;messageBoard&quot;> </li></ul><ul><li>< li > </li></ul><ul><li>< a href =&quot;viewForum?id={@id}&quot;> </li></ul><ul><li>< xsl:value-of select =&quot;@name&quot;/> </li></ul><ul><li></ a > </li></ul><ul><li></ li > </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  86. 86. <xsl:template match = “/”> <ul><li>Read this as: Start processing the XML document at the root of the document </li></ul><ul><li>/ indicates root </li></ul><ul><li>There are some details underneath, as explained next </li></ul>
  87. 87. Understanding <xsl:template-match> - 1 <ul><li>< xsl:template match =&quot;/&quot;> </li></ul><ul><li>< html > </li></ul><ul><li>< head > </li></ul><ul><li>< title >Discussion Forum Home Page</ title > </li></ul><ul><li></ head > </li></ul><ul><li>< body > </li></ul><ul><li>< h1 >Discussion Forum Home Page</ h1 > </li></ul><ul><li>< h3 >Please select a message board to view:</ h3 > </li></ul><ul><li>< ul > </li></ul><ul><li>< xsl:apply-templates select =&quot;disussionForumHome/messageBoard&quot;/> </li></ul><ul><li></ ul > </li></ul><ul><li></ body > </li></ul><ul><li></ html > </li></ul><ul><li></ xsl:template > </li></ul><ul><li>Once root is located, start outputting HTML tags as shown, until the < xsl:apply-templates select =&quot;disussionForumHome/messageBoard&quot;/> is encountered </li></ul>
  88. 88. Understanding <xsl:template-match> - 2 <ul><li><xsl:template match> </li></ul><ul><ul><li>Has four optional attributes </li></ul></ul><ul><ul><ul><li>match – Almost always necessary, takes an Xpath expression as an argument. When the current node matches the node set defined in this expression, the template is executed. </li></ul></ul></ul><ul><ul><ul><li>name – Allows us to refer to the template from elsewhere in the document </li></ul></ul></ul><ul><ul><ul><li>priority – NA </li></ul></ul></ul><ul><ul><ul><li>mode – NA </li></ul></ul></ul>
  89. 89. Understanding <xsl:template-match> - 3 <ul><li>But the XSLT also has one more <xsl:template> element as follows: </li></ul><ul><ul><li>< xsl:template match =&quot;messageBoard&quot;> </li></ul></ul><ul><li>Why does this not get the processor’s attention? </li></ul><ul><li>This is because by default, only the root is loaded; all other templates must be explicitly called by other parts of the XSLT code, as we shall discuss shortly </li></ul>
  90. 90. <xsl:apply-templates> - 1 <ul><li>Tells the XSLT processor to </li></ul><ul><ul><li>Begin a new search for elements in the source XML document that match the search pattern </li></ul></ul><ul><ul><li>And to look for a matching <xsl:template> element in the XSLT for the same element </li></ul></ul><ul><li>Here, we have: </li></ul><ul><li>< xsl:apply-templates select =&quot;disussionForumHome/messageBoard&quot;/> </li></ul><ul><li>Hence, the XSLT processor searches for </li></ul><ul><ul><li>A pattern disussionForumHome/messageBoard in the XML document </li></ul></ul><ul><ul><li>A <template-match> element for the same in the XSLT document </li></ul></ul>
  91. 91. <xsl:apply-templates> - 2 <ul><li>The <xsl:apply-templates> element works recursively </li></ul><ul><li>Here, it tells the XSLT processor to first select the <discussionForumHome> elements of the current node </li></ul><ul><li>Current node here is the root element of the XML document </li></ul><ul><li>Hence, it selects all the <discussionForumHome> elements at the root level, which means just one element </li></ul><ul><li>However, if we had more elements with the same name deeper in the hierarchy, they would have been ignored </li></ul>
  92. 92. <xsl:apply-templates> - 3 <ul><li>Assuming that the XSLT processor locates the <discussionForumHome> element, it searches for all of its <messageBoard> children </li></ul><ul><li>For each <messageBoard> child, the processor looks for the template in our stylesheet that provides the best match </li></ul><ul><li>Since our stylesheet contains a template that exactly matches the <messageBoard> pattern, it is instantiated for each of the <messageBoard> elements in the source XML </li></ul>
  93. 93. <xsl:template match = “messageBoard”> <ul><li>This template gets invoked for each instance of the <messageBoard> element </li></ul><ul><li>< xsl:template match =&quot;messageBoard&quot;> </li></ul><ul><li>< li > </li></ul><ul><li>< a href =&quot;viewForum?id={@id}&quot;> </li></ul><ul><li>< xsl:value-of select =&quot;@name&quot;/> </li></ul><ul><li></ a > </li></ul><ul><li></ li > </li></ul><ul><li></ xsl:template > </li></ul><ul><li>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 </li></ul>
  94. 94. Summary <ul><li>Most transformation in XSLT is driven by two elements, <xsl:template> and <xsl:apply-templates>. </li></ul><ul><li>Processing begins at root, and then: </li></ul><ul><ul><li>For each X in the current node, processor searches for all <xsl:template match = “pattern”> elements in the stylesheet that potentially match the node. </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><ul><li>If the template contains <xsl:apply-templates select = “newPattern” />, a new current node is created and the process repeats recursively. </li></ul></ul>
  95. 95. Comparing <xsl:template> to <xsl:apply-template> <ul><li>Think about the former as similar to a Java method definition, and the later as invoking that method </li></ul>
  96. 96. Another Example <ul><li>college.xml </li></ul><ul><li><? xml version =&quot;1.0&quot; encoding =&quot;utf-8&quot;?> </li></ul><ul><li><? xml-stylesheet href=&quot;college.xsl&quot; type=&quot;text/xsl&quot; ?> </li></ul><ul><li>< college > </li></ul><ul><li>< name >SICSR</ name > </li></ul><ul><li>< city >Pune</ city > </li></ul><ul><li>< state >Maharashtra</ state > </li></ul><ul><li></ college > </li></ul><ul><li>college.xsl </li></ul><ul><li><? xml version =&quot;1.0&quot; encoding =&quot;utf-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot;1.0&quot; </li></ul><ul><li>xmlns:xsl =&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li>< xsl:template match =&quot;college&quot;> </li></ul><ul><li>< b > </li></ul><ul><li>< xsl:value-of select =&quot;name&quot;/> is located in < xsl:value-of select =&quot;city&quot;/>, < xsl:value-of select =&quot;state&quot;/>. </li></ul><ul><li></ b > </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  97. 97. Explanation <ul><li>Elements that do not start with <xsl:> are simply copied into the result tree </li></ul><ul><li><xsl:value of> copies the value of something in the XML source tree to the result tree </li></ul><ul><li>Here, current node is <college>, so <xsl:value-of select = “name”/> selects the text content of the <name> element inside <school> </li></ul>
  98. 98. An Example for Conceptual Clarity <ul><li><customer> </li></ul><ul><ul><li><customer> (Many instance) </li></ul></ul><ul><ul><ul><li><name> (Many instances) </li></ul></ul></ul>< 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. 99. Value-of select = ‘.’ <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;test.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><bold>Hello, world.</bold> </li></ul></ul><ul><ul><li><red>I am </red> </li></ul></ul><ul><ul><li><italic>fine.</italic> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> </li></ul></ul><ul><ul><li><xsl:template match=&quot;bold&quot;> </li></ul></ul><ul><ul><li><P><B><xsl:value-of select=&quot;.&quot;/></B></P> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li><xsl:template match=&quot;red&quot;> </li></ul></ul><ul><ul><li><P style=&quot;color:red&quot;><xsl:value-of select=&quot;.&quot;/></P> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li><xsl:template match=&quot;italic&quot;> </li></ul></ul><ul><ul><li><P><i><xsl:value-of select=&quot;.&quot;/></i></P> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  100. 100. Another Example – Can be Confusing! <ul><li>What would be the output? </li></ul><ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;test.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><employee> </li></ul></ul><ul><ul><li><firstName>Joe</firstName> </li></ul></ul><ul><ul><li><surName>Smith</surName> </li></ul></ul><ul><ul><li></employee> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL </li></ul><ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul></ul><ul><ul><li><xsl:template match=&quot;employee&quot;> </li></ul></ul><ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li><xsl:template match=&quot;surName&quot;> </li></ul></ul><ul><ul><li><i><xsl:value-of select=&quot;.&quot;/></i> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  101. 101. Explanation <ul><li>XSLT tries to process the first match (at the outermost possible level), i.e. on xslTutorial – There is none </li></ul><ul><li>So, it tries to find a match on the next level, which is employee; and executes code inside </li></ul><ul><li>Now, anything inside the employee tag has to be processed while inside the employee template itself </li></ul><ul><li>But surName is outside of employee, and hence will be ignored </li></ul>
  102. 102. Modified Example <ul><li>Is this correct now? </li></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;employee.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><employee> </li></ul></ul><ul><ul><li><firstName>Joe</firstName> </li></ul></ul><ul><ul><li><middleName>Angus</middleName> </li></ul></ul><ul><ul><li><surName>Smith</surName> </li></ul></ul><ul><ul><li></employee> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL </li></ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee&quot;> </li></ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul><ul><li><xsl:template match=&quot;surName&quot;> </li></ul><ul><li><i><xsl:value-of select=&quot;.&quot;/></i> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/middleName&quot;> </li></ul><ul><li><xsl:value-of select=&quot;middleName&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  103. 103. Analysis <ul><li>No! </li></ul><ul><li>Inside an <xsl:template-match>, another <xsl:template match> cannot exist </li></ul><ul><li>How to print the last name, then? </li></ul>
  104. 104. Modified Example <ul><li>XML </li></ul><ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;employee.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><xslTutorial> </li></ul></ul><ul><ul><li><employee> </li></ul></ul><ul><ul><li><firstName>Joe</firstName> </li></ul></ul><ul><ul><li><middleName>Angus</middleName> </li></ul></ul><ul><ul><li><surName>Smith</surName> </li></ul></ul><ul><ul><li></employee> </li></ul></ul><ul><ul><li></xslTutorial> </li></ul></ul><ul><li>XSL </li></ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee&quot;> </li></ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul><ul><li><xsl:apply-templates /> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/ sur Name&quot;> </li></ul><ul><li><xsl:value-of select=“ . &quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  105. 105. Another Version <ul><li>XSL </li></ul><ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee&quot;> </li></ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul><ul><li><xsl:apply-templates /> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/firstName&quot;> </li></ul><ul><li><h1> <xsl:value-of select=&quot;.&quot;/> </h1> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/middleName&quot;> </li></ul><ul><li><i> <xsl:value-of select=&quot;.&quot;/> </i> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/surName&quot;> </li></ul><ul><li><h2> <xsl:value-of select=&quot;.&quot;/> </h2> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  106. 106. Modified Further <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee&quot;> </li></ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul><ul><li><xsl:apply-templates /> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/firstName&quot;> </li></ul><ul><li><h1> <xsl:value-of select=&quot;.&quot;/> </h1> </li></ul><ul><li></xsl:template> </li></ul><ul><li><!-- <xsl:template match=&quot;/xslTutorial/employee/middleName&quot;> </li></ul><ul><li><i> <xsl:value-of select=&quot;.&quot;/> </i> </li></ul><ul><li></xsl:template> </li></ul><ul><li>--> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/surName&quot;> </li></ul><ul><li><h2> <xsl:value-of select=&quot;.&quot;/> </h2> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  107. 107. Modified Further <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee&quot;> </li></ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul><ul><li><xsl:apply-templates /> </li></ul><ul><li></xsl:template> </li></ul><ul><li><!-- </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/firstName&quot;> </li></ul><ul><li><h1> <xsl:value-of select=&quot;.&quot;/> </h1> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/middleName&quot;> </li></ul><ul><li><i> <xsl:value-of select=&quot;.&quot;/> </i> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/surName&quot;> </li></ul><ul><li><h2> <xsl:value-of select=&quot;.&quot;/> </h2> </li></ul><ul><li></xsl:template> </li></ul><ul><li>--> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  108. 108. Yet Another Version – Only middle name would get displayed after the complete name <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee&quot;> </li></ul><ul><li><B><xsl:value-of select=&quot;.&quot;/></B> </li></ul><ul><li><xsl:apply-templates select=“middleName” /> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/middleName&quot;> </li></ul><ul><li><i> <xsl:value-of select=&quot;.&quot;/> </i> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/xslTutorial/employee/surName&quot;> </li></ul><ul><li><h2> <xsl:value-of select=&quot;.&quot;/> </h2> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  109. 109. Controlling the Output Method <ul><li>Try adding </li></ul><ul><ul><li><xsl:output method=“html” /> </li></ul></ul><ul><ul><li><xsl:output method=“xml” /> </li></ul></ul><ul><ul><li><xsl:output method=“text” /> </li></ul></ul><ul><li>one after the other </li></ul><ul><li>Run the example from the command prompt as: </li></ul><ul><ul><li>set CLASSPATH=c:xalanbinxalan.jar </li></ul></ul><ul><ul><li>java org.apache.xalan.xslt.Process -INDENT 3 -IN employee.xml -XSL employee.xsl -OUT output.html </li></ul></ul>
  110. 110. More XSLT Examples
  111. 111. First XSL Example: Hello World! <ul><li>XML Document (HelloWorld.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;HelloWorld.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><msg>Hello World! </li></ul></ul><ul><ul><li></msg> </li></ul></ul><ul><li>XSL Document (HelloWorld.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul></ul><ul><ul><li><xsl:output method=&quot;text&quot;/> </li></ul></ul><ul><ul><li><xsl:template match=&quot;msg&quot;>Found it!</xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  112. 112. Second XSL example <ul><li>XML Document (second.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;second.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><message>We can easily output XML using XSLT! </li></ul></ul><ul><ul><li></message> </li></ul></ul><ul><li>XSL Document (second.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul></ul><ul><ul><li><xsl:output method=&quot;text&quot;/> </li></ul></ul><ul><ul><li><xsl:template match=&quot;/&quot;>Message in XML document is: <xsl:apply-templates/>!</xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  113. 113. Third XSL Example <ul><li>XML document (third.xml) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><?xml-stylesheet href=&quot;third.xsl&quot; type=&quot;text/xsl&quot;?> </li></ul></ul><ul><ul><li><name> </li></ul></ul><ul><ul><li><first>Sachin</first> </li></ul></ul><ul><ul><li><last> Tendukar</last> </li></ul></ul><ul><ul><li></name> </li></ul></ul><ul><li>XSL document (third.xsl) </li></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul></ul><ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul></ul><ul><ul><li><xsl:template match=&quot;name&quot;> </li></ul></ul><ul><ul><li><html> </li></ul></ul><ul><ul><li><head> </li></ul></ul><ul><ul><li><title>XSL Output Example</title> </li></ul></ul><ul><ul><li></head> </li></ul></ul><ul><ul><li><body> </li></ul></ul><ul><ul><li><p> </li></ul></ul><ul><ul><li><xsl:apply-templates select=&quot;first&quot;/> </li></ul></ul><ul><ul><li></p> </li></ul></ul><ul><ul><li><p> </li></ul></ul><ul><ul><li><xsl:apply-templates select=&quot;last&quot;/> </li></ul></ul><ul><ul><li></p> </li></ul></ul><ul><ul><li></body> </li></ul></ul><ul><ul><li></html> </li></ul></ul><ul><ul><li></xsl:template> </li></ul></ul><ul><ul><li></xsl:stylesheet> </li></ul></ul>
  114. 114. XPath Basics
  115. 115. Nodes <ul><li>XPath views a document as a tree </li></ul><ul><li>This tree consists of nodes </li></ul><ul><li>One node can contain other child nodes </li></ul><ul><li>There are 7 types of nodes </li></ul><ul><ul><li>root, element, attribute, text, comment, processing instruction, and namespace </li></ul></ul>
  116. 116. Sample XML Document <ul><li><?xml version = “1.0”?> </li></ul><ul><li><!-- This is a comment --> </li></ul><ul><li><BOOK author = “Andrew Tanenbaum” edition = “3”> </li></ul><ul><li><REVIEW> This is a wonderful book!</REVIEW> </li></ul><ul><li>Computer Networks </li></ul><ul><li></BOOK> </li></ul>
  117. 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. 118. XPath Basics <ul><li>XPath allows selection of nodes/attributes based on certain conditions </li></ul><ul><li>XSLT processes these nodes </li></ul><ul><li>XPath expressions </li></ul><ul><ul><li>Criteria for selection of nodes/attributes </li></ul></ul><ul><li>Location path </li></ul><ul><ul><li>Similar to street directions: Used for locating information in an XML document </li></ul></ul>
  119. 119. XPath Example <ul><li>child::chapter[child::title] </li></ul><ul><ul><li>Look for child nodes of the current node (called as the context node ) representing the <chapter> element </li></ul></ul><ul><ul><li>Within that, look only for child nodes that represent the <title> element </li></ul></ul><ul><ul><li><chapter> </li></ul></ul><ul><ul><ul><li><title> </li></ul></ul></ul>
  120. 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. 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. 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. 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. 124. Template Rules <ul><li>Describes how an XML element node is converted into an XSL element node for displaying </li></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li><xsl: template match=“/article/title”> </li></ul></ul><ul><ul><li><xsl: template match=“line”> </li></ul></ul><ul><ul><li><xsl: template match=“city/line”> </li></ul></ul><ul><ul><li><xsl: template match=“content//line”> </li></ul></ul><ul><ul><li><xsl: template match=“title | line”> </li></ul></ul><ul><ul><li><xsl:template match=“/article/*” </li></ul></ul><ul><ul><li><xsl:template match=“/article/section[2]/title/text()” </li></ul></ul>
  125. 125. <xsl:value-of> Element <ul><li>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 </li></ul><ul><li>The value of the required select attribute contains an XPath expression </li></ul><ul><li>It works like navigating a file system where a forward slash (/) selects subdirectories </li></ul>
  126. 126. XSLT Example <ul><li>Consider the following XML document </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;history.xsl&quot;?> </li></ul><ul><li><subject> </li></ul><ul><li><info>History of India</info> </li></ul><ul><li></subject> </li></ul><ul><li>We can write the following XSLT code to transform this into HTML </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;subject&quot;> </li></ul><ul><li><html> </li></ul><ul><li><head><title>Hello World</title></head> </li></ul><ul><li><body> </li></ul><ul><li><h2> <xsl:value-of select=&quot;info&quot;/> </h2> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  127. 127. Exercise <ul><li>Write an XSLT document for the following XML document to display it as HTML </li></ul><ul><li><?xml version = “1.0” ?> </li></ul><ul><li><?xml:stylesheet type = “text/xsl” href = “one.xsl”?> </li></ul><ul><li><myPerson> </li></ul><ul><li><personName>Sachin Tendulkar</personName> </li></ul><ul><li></myPerson> </li></ul>
  128. 128. Solution <ul><li><xsl:stylesheet version = “1.0” xmlns:xsl = “http://www/w3.org/1999/XSL/Transform”> </li></ul><ul><li><xsl:template match = “myPerson”> </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><b> <xsl:value-of select = “personName”/> </b> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  129. 129. Exercise <ul><li>Consider the following XML file: </li></ul><ul><li><?xml version=“1.0”?> </li></ul><ul><li><BOOK> </li></ul><ul><li><BOOK_TITLE>Computer Networks</BOOK_TITLE> </li></ul><ul><li><AUTHOR>Tanenbaum</AUTHOR> </li></ul><ul><li></BOOK> </li></ul><ul><li>Use XSL to display title and author as level 1 and level 2 headers, respectively </li></ul>
  130. 130. Solution <ul><li><xsl:stylesheet version=“1.0” xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> </li></ul><ul><li><xsl:template match=“BOOK”> </li></ul><ul><li><html> </li></ul><ul><li><head><title> Book Information </title></head> </li></ul><ul><li><body> </li></ul><ul><li> <h1><xsl:value-of select=“BOOK_TITLE”/></h1> </li></ul><ul><li> <h2>by <xsl:value-of select=“AUTHOR”/></h2> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  131. 131. Exercise <ul><li>Consider this XML and write an XSL to display only the book title and price </li></ul><ul><li><?xml version = “1.0” ?> </li></ul><ul><li><?xml version = &quot;1.0&quot; ?> </li></ul><ul><li><?xml:stylesheet type = &quot;text/xsl&quot; href = &quot;book2.xsl&quot;?> </li></ul><ul><li><CATALOG> </li></ul><ul><li><BOOK> </li></ul><ul><li><TITLE>Computer Networks</TITLE> </li></ul><ul><li><AUTHORS> </li></ul><ul><li><AUTHOR>Andrew Tanenbaum</AUTHOR> </li></ul><ul><li></AUTHORS> </li></ul><ul><li><PUBYEAR>2003</PUBYEAR> </li></ul><ul><li><PRICE>250</PRICE> </li></ul><ul><li></BOOK> </li></ul><ul><li><BOOK> </li></ul><ul><li><TITLE>Computer Fundamentals</TITLE > </li></ul><ul><li><AUTHORS> </li></ul><ul><li><AUTHOR>Rajaraman</AUTHOR> </li></ul><ul><li><AUTHOR>Ghosh</AUTHOR> </li></ul><ul><li></AUTHORS> </li></ul><ul><li><PUBYEAR>2002</PUBYEAR> </li></ul><ul><li><PRICE>250</PRICE> </li></ul><ul><li></BOOK> </li></ul><ul><li></CATALOG> </li></ul>
  132. 132. Solution <ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;BOOK&quot;> </li></ul><ul><li>Book Name: <xsl:value-of select=&quot;TITLE&quot;/> Price: <xsl:value-of select=&quot;PRICE&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  133. 133. Exercise <ul><li>Consider the following XML document, titled emp.xml: </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;emp.xsl&quot;?> </li></ul><ul><li><EMP_INFO> </li></ul><ul><li><EMPLOYEE> </li></ul><ul><li><EMP_NAME empID=&quot;9662&quot;> </li></ul><ul><li><FIRST>Sachin</FIRST> </li></ul><ul><li><LAST>Tendulkar</LAST> </li></ul><ul><li></EMP_NAME> </li></ul><ul><li></EMPLOYEE> </li></ul><ul><li></EMP_INFO> </li></ul><ul><li>Write emp.xsl file mentioned above, which would: </li></ul><ul><ul><li>Display a heading Emp Name: , followed by the employee’s name. </li></ul></ul><ul><ul><li>Display the employee id below this, in a smaller font. </li></ul></ul>
  134. 134. Solution <ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;EMP_INFO&quot;> </li></ul><ul><li><html> </li></ul><ul><li><head><title>Emp Info!</title></head> </li></ul><ul><li> <body> </li></ul><ul><li> <h1>Emp Name: <xsl:value-of select=&quot;EMPLOYEE/EMP_NAME/FIRST&quot;/> </li></ul><ul><li><xsl:value-of select=&quot;EMPLOYEE/EMP_NAME/LAST&quot;/> </h1> </li></ul><ul><li><h3> <xsl:value-of select=&quot;EMPLOYEE/EMP_NAME/@empID&quot;/></h3> </li></ul><ul><li> </body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  135. 135. Concept of apply-templates
  136. 136. apply-templates <ul><li>We know that <xsl:template match=“…”> allows us to search for a location path in our XML document </li></ul><ul><li>Another XSLT syntax, <xsl:apply-templates/> allows us to list the contents of the elements with reference to the previous <xsl:template match=“…”> declaration </li></ul><ul><li>Examples follow </li></ul>
  137. 137. Source XML (apply.xml) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;apply1.xsl&quot;?> </li></ul><ul><li><book> </li></ul><ul><li><title>Profesional XSL</title> </li></ul><ul><li><chapters> </li></ul><ul><li><chapter> </li></ul><ul><li><chapterNo>2</chapterNo> </li></ul><ul><li><chapterTopic>XPath</chapterTopic> </li></ul><ul><li><chapterAuthor>Andrew Watt</chapterAuthor> </li></ul><ul><li><chapterSections> </li></ul><ul><li><chapterSection>Section 1</chapterSection> </li></ul><ul><li><chapterSection>Section 2</chapterSection> </li></ul><ul><li><chapterSection>Section 3</chapterSection> </li></ul><ul><li><chapterSection>Section 4</chapterSection> </li></ul><ul><li><chapterSection>Section 5</chapterSection> </li></ul><ul><li><chapterSection>Section 6</chapterSection> </li></ul><ul><li></chapterSections> </li></ul><ul><li></chapter> </li></ul><ul><li><chapter> </li></ul><ul><li><chapterNo>3</chapterNo> </li></ul><ul><li><chapterTopic>XSLT Basics</chapterTopic> </li></ul><ul><li><chapterAuthor>Paul Spencer</chapterAuthor> </li></ul><ul><li><chapterSections> </li></ul><ul><li><chapterSection>Section 1</chapterSection> </li></ul><ul><li><chapterSection>Section 2</chapterSection> </li></ul><ul><li><chapterSection>Section 3</chapterSection> </li></ul><ul><li><chapterSection>Section 4</chapterSection> </li></ul><ul><li><chapterSection>Section 5</chapterSection> </li></ul><ul><li><chapterSection>Section 6</chapterSection> </li></ul><ul><li></chapterSections> </li></ul><ul><li></chapter> </li></ul><ul><li><chapter> </li></ul><ul><li><chapterNo>4</chapterNo> </li></ul><ul><li><chapterTopic>Modular XSLT</chapterTopic> </li></ul><ul><li><chapterAuthor>Kurt Cagle</chapterAuthor> </li></ul><ul><li><chapterSections> </li></ul><ul><li><chapterSection>Section 1</chapterSection> </li></ul><ul><li><chapterSection>Section 2</chapterSection> </li></ul><ul><li><chapterSection>Section 3</chapterSection> </li></ul><ul><li><chapterSection>Section 4</chapterSection> </li></ul><ul><li><chapterSection>Section 5</chapterSection> </li></ul><ul><li><chapterSection>Section 6</chapterSection> </li></ul><ul><li></chapterSections> </li></ul><ul><li></chapter> </li></ul><ul><li></chapters> </li></ul><ul><li></book> </li></ul>
  138. 138. Basic XSL Example (apply0.xml) <ul><li><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version=&quot;1.0&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul><ul><li>What would be the output? </li></ul>
  139. 139. Explanation <ul><li>We have not specified what action should be taken when root is applied </li></ul><ul><li>This causes the entire output to be suppressed! </li></ul>
  140. 140. XSL Example: 1 (apply1.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul><ul><li>Output: ? </li></ul>
  141. 141. Explanation <ul><li>Now there is an <apply-templates> inside the <template match> </li></ul><ul><li>This causes the default <apply-templates> to be applied, i.e. without any particular select inside <apply-templates> </li></ul><ul><li>In other words, <apply-templates> gets executed for all elements inside the root </li></ul>
  142. 142. XSL Example: 2 (apply2.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;*&quot;> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  143. 143. Explanation <ul><li>Empty output, since <apply-templates/> will bring search from the root level to all non-root elements. </li></ul><ul><li>But at that level, there is no <apply-templates/> to display anything, unlike in the previous example. </li></ul>
  144. 144. XSL Example: 3 (apply3.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;*&quot;> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;book&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;chapters&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;chapter&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;chapterTopic&quot;> </li></ul><ul><li><!-- <xsl:apply-templates/> --> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul><ul><li>Display the list of chapter topics </li></ul>
  145. 145. XSL Example: 4 (apply4.xsl) <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;*&quot;> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;book&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;chapters&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;chapter&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/book/chapters/chapter/chapterNo&quot;> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/book/chapters/chapter/chapterTopic&quot;> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/book/chapters/chapter/chapterAuthor&quot;> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul><ul><li>Display element values for all the selected elements </li></ul>
  146. 146. XSL Example: 6 (apply6.xsl) - Tricky <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; </li></ul><ul><li>xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  147. 147. XSL Example: 5 (apply5.xsl) – Tricky! <ul><li>What if we remove all search paths except the absolute ones? </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/book/chapters/chapter/chapterNo&quot;> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/book/chapters/chapter/chapterTopic&quot;> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;/book/chapters/chapter/chapterAuthor&quot;> </li></ul><ul><li><xsl:value-of select=&quot;.&quot;/> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  148. 148. Explanation <ul><li>Here, there is no <apply-templates> calling any of the defined <template match> tags </li></ul><ul><li>Hence, our <template match> tags would get ignored completely </li></ul><ul><li>Hence, it would produce the full XML contents as the output </li></ul>
  149. 149. Using the Mode Attribute
  150. 150. Using Mode <ul><li>If more than one template matches an identical pattern, a conflict arises </li></ul><ul><li>This can be solved by using template priority, but at times, that is also ambiguous </li></ul><ul><li>In such situations, we can use the template mode </li></ul><ul><li>Useful when a template needs to visit the same node, but with different results </li></ul>
  151. 151. Mode Example, also uses CSS <ul><li>XML (css-example-1) </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;css-example-1.xsl&quot;?> </li></ul><ul><li>< us > </li></ul><ul><li>< state name =&quot; Hawaii &quot;> </li></ul><ul><li>< county name =&quot; Hawaii &quot;> </li></ul><ul><li>< city class =&quot; largest &quot;> Hilo </ city > </li></ul><ul><li></ county > </li></ul><ul><li>< county name =&quot; Honolulu &quot;> </li></ul><ul><li>< city class =&quot; largest &quot;> Honolulu </ city > </li></ul><ul><li></ county > </li></ul><ul><li>< county name =&quot; Kauai &quot;> </li></ul><ul><li>< city class =&quot; largest &quot;> Kapaa </ city > </li></ul><ul><li></ county > </li></ul><ul><li>< county name =&quot; Maui &quot;> </li></ul><ul><li>< city class =&quot; largest &quot;> Kahului </ city > </li></ul><ul><li></ county > </li></ul><ul><li></ state > </li></ul><ul><li></ us > </li></ul><ul><li>XSL </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> </li></ul><ul><li>< xsl:stylesheet version =&quot; 1.0 &quot; xmlns:xsl =&quot; http://www.w3.org/1999/XSL/Transform &quot;> </li></ul><ul><li>< xsl:output method =&quot; html &quot;/> </li></ul><ul><li>< xsl:template match =&quot; us/state &quot;> </li></ul><ul><li>< html > </li></ul><ul><li>< head > </li></ul><ul><li>< title > State: < xsl:value-of select =&quot; @name &quot;/> </li></ul><ul><li></ title > </li></ul><ul><li>< style type =&quot; text/css &quot;> </li></ul><ul><li>h1, h2 {font-family: sans-serif, color: blue} </li></ul><ul><li>ul {font-size: 16pt} </li></ul><ul><li></ style > </li></ul><ul><li></ head > </li></ul><ul><li>< body > </li></ul><ul><li>< h1 > State: < xsl:value-of select =&quot; @name &quot;/> </li></ul><ul><li></ h1 > </li></ul><ul><li>< h2 > All Countries </ h2 > </li></ul><ul><li>< ul > </li></ul><ul><li>< xsl:apply-templates select =&quot; county &quot; mode =&quot; county &quot;/> </li></ul><ul><li></ ul > </li></ul><ul><li>< h2 > Largest Cities (by County) </ h2 > </li></ul><ul><li>< ul > </li></ul><ul><li>< xsl:apply-templates select =&quot; county &quot; mode =&quot; city &quot;/> </li></ul><ul><li></ ul > </li></ul><ul><li></ body > </li></ul><ul><li></ html > </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot; county &quot; mode =&quot; county &quot;> </li></ul><ul><li>< li > </li></ul><ul><li>< xsl:value-of select =&quot; @name &quot;/> </li></ul><ul><li></ li > </li></ul><ul><li></ xsl:template > </li></ul><ul><li>< xsl:template match =&quot; county &quot; mode =&quot; city &quot;> </li></ul><ul><li>< li > </li></ul><ul><li>< xsl:value-of select =&quot; city &quot;/> ( < xsl:value-of select =&quot; @name &quot;/> ) </li></ul><ul><li></ li > </li></ul><ul><li></ xsl:template > </li></ul><ul><li></ xsl:stylesheet > </li></ul>
  152. 152. Creating New Elements and Attributes
  153. 153. Creating Elements and Attributes <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><students> </li></ul><ul><li><student first_name=&quot;Raju&quot;> </li></ul><ul><li><id>101</id> </li></ul><ul><li><remarks> A student who is not at all sincere!</remarks> </li></ul><ul><li></student> </li></ul><ul><li><student first_name=&quot;Aarati&quot;> </li></ul><ul><li><id>102</id> </li></ul><ul><li><remarks> A student who is really quite dedicated to her studies!</remarks> </li></ul><ul><li></student> </li></ul><ul><li><student first_name=&quot;Rajani&quot;> </li></ul><ul><li><id>103</id> </li></ul><ul><li><remarks> A student who is awesome!</remarks> </li></ul><ul><li></student> </li></ul><ul><li></students> </li></ul>
  154. 154. Corresponding XSL <ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;students&quot;> </li></ul><ul><li><students> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></students> </li></ul><ul><li></xsl:template> </li></ul><ul><li><xsl:template match=&quot;student&quot;> </li></ul><ul><li><xsl:element name=&quot;{@first_name}&quot;> </li></ul><ul><li><xsl:attribute name=&quot;id&quot;><xsl:value-of select=&quot;id&quot;/></xsl:attribute> </li></ul><ul><li><notes> </li></ul><ul><li><xsl:value-of select=&quot;remarks&quot;/> </li></ul><ul><li></notes> </li></ul><ul><li></xsl:element> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  155. 155. Explanation - 1 <ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></xsl:template> </li></ul><ul><ul><li>match = “/” means select root </li></ul></ul><ul><ul><li><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;> </li></ul></ul>
  156. 156. Explanation – 2 <ul><li><xsl:template match=&quot;students&quot;> </li></ul><ul><li><students> </li></ul><ul><li><xsl:apply-templates/> </li></ul><ul><li></students> </li></ul><ul><li></xsl:template> </li></ul><ul><li>When the students element is found, output <students> </students> </li></ul><ul><li>Then look for a match inside < students> tag, i.e. for <student> tag now </li></ul>
  157. 157. Explanation – 3 <ul><li><xsl:template match=&quot;student&quot;> </li></ul><ul><li><xsl:element name=&quot;{@first_name}&quot;> </li></ul><ul><li><xsl:attribute name=&quot;id&quot;><xsl:value-of select=&quot;id&quot;/></xsl:attribute> </li></ul><ul><li><notes> </li></ul><ul><li><xsl:value-of select=&quot;remarks&quot;/> </li></ul><ul><li></notes> </li></ul><ul><li></xsl:element> </li></ul><ul><li></xsl:template> </li></ul><ul><li>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 </li></ul><ul><li>So: </li></ul><ul><ul><li><student first_name=&quot;Raju&quot;> </li></ul></ul><ul><ul><li><id>101</id> </li></ul></ul><ul><ul><li><remarks> A student who is not at all sincere!</remarks> </li></ul></ul><ul><ul><li></student> </li></ul></ul><ul><li>Will now become </li></ul><ul><ul><li><Raju> </Raju> </li></ul></ul>
  158. 158. Explanation – 4 <ul><li><xsl:attribute name=&quot;id&quot;> </li></ul><ul><li><xsl:value-of select=&quot;id&quot;/> </li></ul><ul><li></xsl:attribute> </li></ul><ul><li>Add an attribute named id to the output XML, which equals the value of the element id of the input XML </li></ul>
  159. 159. Explanation – 5 <ul><li><notes> </li></ul><ul><li><xsl:value-of select=&quot;remarks&quot;/> </li></ul><ul><li></notes> </li></ul><ul><li>Add a new element called as notes to the output XML, which should contain the value of the remarks element of the input XML </li></ul>
  160. 160. Explanation – 6 <ul><li>Input XML </li></ul><ul><ul><li><student first_name=&quot;Raju&quot;> </li></ul></ul><ul><ul><li><id>101</id> </li></ul></ul><ul><ul><li><remarks> A student who is not at all sincere!</remarks> </li></ul></ul><ul><ul><li></student> </li></ul></ul><ul><li>Output XML </li></ul><ul><li><Raju id = “101”> </li></ul><ul><li><comments> A student who is not at all sincere!</comments> </li></
  1. A particular slide catching your eye?

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

×