XSL (XML StylesheetLanguage) Two parts XSLT (XSL Transformation) XSL-FO (XSL Formatting Objects) XSLFO is similar to CSS, quite complex We will discuss XSLT in detail, XSL-FO in brief
4.
XPath Allows searchingand navigation of XML documents Can specify which parts of an XML document we want to transform Used heavily in XSLT for searching of information
5.
XSLT Usage StylingAdd elements specific to viewing (e.g. logo) Create new content from existing one (e.g. TOC) Present information with the right level of details (e.g. overview for managers, details for staff) Transform XML documents into HTML
6.
XSLT Stylesheets AnXSLT stylesheet consists of a series of templates , together with instructions based on XPath Tell an XSLT processor how to match the template against the nodes in an XML input document For each template, the processor reads the input document for all matching patterns and produces an output document See next slide
7.
XSLT Processing ConceptInput XML document XSLT Processor Output XML document XSLT Template XSLT Stylesheet
XSLT Example –1 Consider the following XML document <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="pune.xsl"?> <content> <about> PUNE </about> <city> <line1> Pune is a lovely city </line1> <line2> The education facilities are as best as you can get </line2> <line3> And the weather is great, too. </line3> </city> </content>
10.
XSLT Example –2 Here is the corresponding XSLT document <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="content"> <html> <head><title>Welcome to Pune!</title></head> <body> <h1><xsl:value-of select="about"/></h1> <h2><xsl:value-of select="city/line1"/></h2> <h3><xsl:value-of select="city/line2"/></h3> <h4><xsl:value-of select="city/line3"/></h4> </body> </html> </xsl:template> </xsl:stylesheet>
11.
Be Careful! Whenwe say <xsl:template-match>, we not only attempt to locate the specified element/position, but actually move the XSLT cursor there Example <xslt:template-match = “content”> Takes cursor to the content element in XML Hence, later when we say <xsl:value-of select="about"/>, XSLT completes the Xpath as /content/about OR Hence, later when we say <xsl:value-of select=“city/line1"/>, XSLT completes the Xpath as /content/city/line1
12.
Now try these<xsl:template match="/"> and remaining things unchanged Now the path for searching is /about, /city/line1, etc; which is incorrect <xsl:template match="/content"> and remaining things unchanged <xsl:template match="/content">, <h1><xsl:value-of select="/about"/></h1> and remaining things unchanged <xsl:template match="/city"> and remaining things unchanged
Second XSL exampleXML Document (second.xml) <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="second.xsl" type="text/xsl"?> <message>We can easily output XML using XSLT! </message> XSL Document (second.xsl) <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text"/> <xsl:template match="/">Message in XML document is: <xsl:apply-templates/>!</xsl:template> </xsl:stylesheet>
15.
Exercise Write anXSLT document for the following XML document to display it as HTML <?xml version = “1.0” ?> <?xml:stylesheet type = “text/xsl” href = “one.xsl”?> <myPerson> <personName>Sachin Tendulkar</personName> </myPerson>
Exercise Consider thefollowing XML file: <?xml version=“1.0”?> <BOOK> <BOOK_TITLE>Computer Networks</BOOK_TITLE> <AUTHOR>Tanenbaum</AUTHOR> </BOOK> Use XSL to display title and author as level 1 and level 2 headers, respectively
18.
Solution <xsl:stylesheet version=“1.0”xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“BOOK”> <html> <head><title> Book Information </title></head> <body> <h1><xsl:value-of select=“BOOK_TITLE”/></h1> <h2>by <xsl:value-of select=“AUTHOR”/></h2> </body> </html> </xsl:template> </xsl:stylesheet>
19.
Exercise Consider thisXML and write an XSL to display only the book title and price <?xml version = "1.0" ?> <?xml:stylesheet type = "text/xsl" href = "booksmultiple.xsl"?> <CATALOG> <BOOK> <TITLE>Computer Networks</TITLE> <AUTHORS> <AUTHOR>Andrew Tanenbaum</AUTHOR> </AUTHORS> <PUBYEAR>2003</PUBYEAR> <PRICE>250</PRICE> </BOOK> <BOOK> <TITLE>Computer Fundamentals</TITLE > <AUTHORS> <AUTHOR>Rajaraman</AUTHOR> <AUTHOR>Ghosh</AUTHOR> </AUTHORS> <PUBYEAR>2002</PUBYEAR> <PRICE>250</PRICE> </BOOK> </CATALOG>
Exercise Consider thefollowing XML document, titled emp.xml: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="emp.xsl"?> <EMP_INFO> <EMPLOYEE> <EMP_NAME empID="9662"> <FIRST>Sachin</FIRST> <LAST>Tendulkar</LAST> </EMP_NAME> </EMPLOYEE> </EMP_INFO> Write emp.xsl file mentioned above, which would: Display a heading Emp Name: , followed by the employee’s name. Display the employee id below this, in a smaller font.
To understand better… Make changes to the XSL as follows: <h1><xsl:value-of select="/title"/></h1> <h2><xsl:value-of select="/author"/></h2> Then as <h1><xsl:value-of select="title"/></h1> <h2><xsl:value-of select="author"/></h2> Then as <xsl:template match="//"> <h1><xsl:value-of select="title"/></h1> <h2><xsl:value-of select="author"/></h2> Then as <xsl:template match="/"> <h1><xsl:value-of select="/xslTutorial/title"/></h1> <h2><xsl:value-of select="/xslTutorial/author"/></h2> Then as <xsl:template match="/"> <h1><xsl:value-of select="xslTutorial/title"/></h1> <h2><xsl:value-of select="xslTutorial/author"/></h2>
25.
Change to theXML 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
26.
Now look atthe XSL The XSLT stylesheet is also a well-formed XML document The <xsl:stylesheet> element has two attributes Version specifies the XSLT specifications version Declares the namespace
27.
Question What ifour XML document has multiple occurrences of the title and author tags? We would still see only the first occurrence, since we have not yet seen the recursion part of XSLT
Interesting Tricks –1 XML (trick-1.xml) <?xml version="1.0"?> <?xml-stylesheet href=“trick-1.xsl" type="text/xsl"?> <xslTutorial> <title>XSL</title> <author>John Smith</author> </xslTutorial> XSL (trick-1.xsl) <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> </xsl:stylesheet> What would be the output?
30.
Answer The fullXML contents Why? If no template is specified, XSLT produces the complete XML as output!
31.
Interesting Tricks –2 XML (trick-2.xml) <?xml version="1.0"?> <?xml-stylesheet href=“trick-2.xsl" type="text/xsl"?> <xslTutorial> <title>XSL</title> <author>John Smith</author> </xslTutorial> XSL (trick-2.xsl) <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match=“/”> </xsl:template> </xsl:stylesheet> What would be the output?
32.
Answer Now wehave said, match root, but once root is matched, we say do nothing (since there is nothing between <xsl:template match = “/” and </xsl:template> tags Hence, output is empty
33.
Interesting Tricks –3 XML (trick-3.xml) <?xml version="1.0"?> <?xml-stylesheet href=“trick-3.xsl" type="text/xsl"?> <xslTutorial> <title>XSL</title> <author>John Smith</author> </xslTutorial> XSL (trick-3.xsl) <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match=“/”> <xsl:value-of select = “title” /> </xsl:template> </xsl:stylesheet> What would be the output?
34.
Answer Now, wetry to find a match on the title element inside /. But the path for title should be /xslTutorial/title Hence, output would be empty
35.
Interesting Tricks –4 XML (trick-4.xml) <?xml version="1.0"?> <?xml-stylesheet href=“trick-4.xsl" type="text/xsl"?> <xslTutorial> <title>XSL</title> <author>John Smith</author> </xslTutorial> XSL (trick-4.xsl) <?xml version="1.0" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match=“/”> <xsl:value-of select = “/xslTutorial/title” /> </xsl:template> </xsl:stylesheet> What would be the output?
36.
Answer It wouldproduce the contents of the title element, as expected, now
Usage of Templates<xsl:template match = “…”> We know that this clause is used to match a particular tag from an XML file and to do processing, accordingly <xsl:template name = “…”> Allows us to define a template Once such a template is defined, we can use <xsl:call-template name = “…”> to call that defined template
39.
Understanding <apply-templates> class.xml<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> <class> <student>Jack</student> <student>Harry</student> <student>Rebecca</student> <teacher>Mr. Bean</teacher> </class> Class.xsl <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match=“student"> Found a learner! </xsl:template> </xsl:stylesheet> Output Found a learner! Found a learner! Found a learner! Mr. Bean
40.
How it Works?<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> <class> <student>Jack</student> <student>Harry</student> <student>Rebecca</student> <teacher>Mr. Bean</teacher> </class> <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="student"> 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!
41.
Plain English VersionFor all the elements in the given XML If the current element = “student” Display “Found a learner” Else Display the actual contents of the element End-if End-For
42.
Explanation The waythis works is: Use a template if one defined Blindly output the contents of the elements wherever there is no template defined For each <template match = “student”>, we display the text Found a learner! instead of the student name itself. However, for the teacher tag, there is no <template match>. Therefore, its contents are displayed as they are.
43.
Notes on theResult – 1 Note that we would see display for all the student tags However, in the first example, we would have seen the output only for the first instance of title and author tags Why? In the earlier example, there was no template , i.e. no recursion – the syntax used was value-of select Now, we use a template , which introduces recursion
44.
Notes on theResult – 2 This style of coding is ambiguous! It can lead to completely unexpected results Let us modify our XML and XSL now
45.
Modified XML andXSL class1.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class1.xsl"?> < class > < dept > Bye </ dept > < salary > 10000 </ salary > </ class > class1.xsl <?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:template match =" hello "> Found a learner! </ xsl:template > </ xsl:stylesheet > What would be the output?
46.
Output Bye10000 Why?The attempt is to find a match for the tag or element hello in our XML document, which is not found For other tags in the XML document (i.e. dept and salary), there is no template defined; so no special processing is needed for them, except blindly outputting their contents, as before!
47.
Further Modifications ModifiedXML <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class1.xsl"?> < class > < dept > Bye </ dept > < salary > 10000 </ salary > < name > < first > test </ first > < last > test </ last > </ name > </ class > Resulting Output Bye10000testtest This is based on same logic as earlier
48.
Still More Changesclass3.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class1.xsl"?> < class > < dept > Bye </ dept > < salary > 10000 </ salary ></ class > class3.xsl <?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:template match =" dept "> Found a learner! </ xsl:template > < xsl:template match =" salary "> </ xsl:template > </ xsl:stylesheet >
49.
Output Found alearner! This is because we have suppressed the output for the salary tag now
Output Testone10000two20000three30000 Reason:The logic works as: If there is at least one dept tag <apply-templates> (which means, display the default output as it is, which means everything)
XSL Changed –1 Now changes the XSL to this: <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="class"> <xsl:apply-templates select="student"/> </xsl:template> <xsl:template match="student"> Found a learner! </xsl:template> </xsl:stylesheet> What would be the output? See next slide.
57.
Plain English VersionFor each element in the given XML If the current element is class If the child element of the current element is student Output Found a learner End-if End-if End-for Note: There is no Else now!
58.
Output of thesecond XSL Found a learner! Found a learner! Found a learner! Explanation <xsl:template match="class"> The XSLT processor begins at the root element when looking for template matches. It finds a match for the root element class . <xsl:apply-templates select="student"/> In our template that matched class we use xsl:apply-templates which will check for template matches on all the children of class . The children of class in our XML document are student and teacher . To have the teacher element "Mr. Bean" ignored, we use the select attribute of xsl:apply-templates to specify only student children. The XSLT processor then goes searching templates that only match student elements. <xsl:template match="student“> The processor finds the only other template in our XSLT, which prints out "Found a learner!" for each student element in the XML document. XSLT finds three students, so "Found a learner!" is displayed three times. XSLT then finishes its processing and we are left with XSLT output that has eliminated the unwanted text, "Mr. Bean!"
59.
Beware of thisProblem! Suppose the XSL was like this: <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="class"> <xsl:apply-templates /> </xsl:template> <xsl:template match="student"> Found a learner! </xsl:template> </xsl:stylesheet> What would be the output? See next slide.
60.
Analysis The onlychange we have made is to remove the select attribute from apply-templates This would translate to something different! See next slide
61.
Plain English VersionFor each element in the given XML If the current element is class If the child element of the current element is student Output Found a learner Else Output the contents of the current element as they are End-if End-if End-for Note: There is an Else now!
62.
What about this?XML <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> < college > < class > < institution > SICSR </ institution > < student > Jack </ student > < student > Harry </ student > < student > Rebecca </ student > < teacher > Mr. Bean </ teacher > </ class > </ college > XSL <?xml version="1.0" ?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:template match =" class "> < xsl:apply-templates /> </ xsl:template > < xsl:template match =" student "> Found a learner! </ xsl:template > </ xsl:stylesheet >
63.
Output SICSR Founda learner! Found a learner! Found a learner! Mr. Bean Explanation The same logic as earlier applies If a match is found, do something; else display contents of current element blindly Remember, we do not have a select attribute in the template If we have it, what would happen?
64.
Another Variation XML<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="class.xsl"?> < college > < class > < institution > SICSR </ institution > < student > Jack </ student > < student > Harry </ student > < student > Rebecca </ student > < teacher > Mr. Bean </ teacher > </ class > </ college > XSL <?xml version="1.0" ?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:template match =" class "> < xsl:apply-templates select =" student "/> </ xsl:template > < xsl:template match =" student "> Found a learner! </ xsl:template > </ xsl:stylesheet > Output Found a learner! Found a learner! Found a learner!
65.
Summary Do notleave our code in an ambiguous state This happens if we specify <apply-templates> without any specific select attribute It can also happen if we do not specify exact selection criteria inside <template match> Output may not be as expected!
Using Parameters inTemplates <xsl:template name = "print" > <xsl:param name = "A" /> <xsl:param name = "B" >111</xsl:param> <xsl:value-of select = "$A" /> <xsl:text > + </xsl:text> <xsl:value-of select = "$B" /> <xsl:text > = </xsl:text> <xsl:value-of select = "$A+$B" /> </xsl:template> This defines a template (like a method) called as print This template can receive two parameters, named A and B. B has a default value of 111. The template prints the following as the output: Value of A + Value of B = Sum of A and B For example, if a caller calls this template with A = 10 and B = 20, the output will be: 10 + 20 = 30
68.
Calling Templates <xsl:templatematch = "/" > <xsl:call-template name = "print" > <xsl:with-param name = "A" >11</xsl:with-param> <xsl:with-param name = "B" >33</xsl:with-param> </xsl:call-template> <xsl:call-template name = "print" > <xsl:with-param name = "A" >55</xsl:with-param> </xsl:call-template> </xsl:template> Now, we are calling the template defined earlier ( print ) twice in succession: first time passing A = 11 and B = 33; and second time passing A = 55 and not passing any value for B Output (Full code on next slide): 11 + 33 = 44 55 + 111 = 166 Note : 111 was B’s default value in the print template on the previous slide
69.
Code XML <?xmlversion="1.0"?> <?xml-stylesheet type="text/xsl" href="1.xsl" ?> <AAA > <BBB>bbb </BBB> <CCC>ccc </CCC> </AAA> XSL <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method = "text" /> <xsl:template match = "/" > <xsl:call-template name = "print" > <xsl:with-param name = "A" >11</xsl:with-param> <xsl:with-param name = "B" >33</xsl:with-param> </xsl:call-template> <xsl:call-template name = "print" > <xsl:with-param name = "A" >55</xsl:with-param> </xsl:call-template> </xsl:template> <xsl:template name = "print" > <xsl:param name = "A" /> <xsl:param name = "B" >111</xsl:param> <xsl:text > </xsl:text> <xsl:value-of select = "$A" /> <xsl:text > + </xsl:text> <xsl:value-of select = "$B" /> <xsl:text > = </xsl:text> <xsl:value-of select = "$A+$B" /> </xsl:template> </xsl:stylesheet>
discussionForumHome.xml <? xml version ="1.0" encoding ="utf-8"?> <? xml-stylesheet href="discussionForumHome.xsl" type="text/xsl" ?> < disussionForumHome > < messageBoard id ="1" name ="Java Programming"/> < messageBoard id ="2" name ="XML Programming"/> < messageBoard id ="3" name ="XSLT Programming"/> </ disussionForumHome >
75.
discussionForumHome.xsl <? xml version ="1.0" encoding ="utf-8"?> < xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"> < xsl:template match ="/"> < html > < head > < title >Discussion Forum Home Page</ title > </ head > < body > < h1 >Discussion Forum Home Page</ h1 > < h3 >Please select a message board to view:</ h3 > < ul > < xsl:apply-templates select ="disussionForumHome/messageBoard"/> </ ul > </ body > </ html > </ xsl:template > < xsl:template match ="messageBoard"> < li > < a href ="viewForum?id={@id}"> < xsl:value-of select ="@name"/> </ a > </ li > </ xsl:template > </ xsl:stylesheet >
76.
<xsl:template match =“/”> Read this as: Start processing the XML document at the root of the document / indicates root There are some details underneath, as explained next
77.
Understanding <xsl:template-match> - 1 < xsl:template match ="/"> < html > < head > < title >Discussion Forum Home Page</ title > </ head > < body > < h1 >Discussion Forum Home Page</ h1 > < h3 >Please select a message board to view:</ h3 > < ul > < xsl:apply-templates select ="disussionForumHome/messageBoard"/> </ ul > </ body > </ html > </ xsl:template > Once root is located, start outputting HTML tags as shown, until the < xsl:apply-templates select ="disussionForumHome/messageBoard"/> is encountered
78.
Understanding <xsl:template-match> - 2 <xsl:template match> Has four optional attributes match – Almost always necessary, takes an Xpath expression as an argument. When the current node matches the node set defined in this expression, the template is executed. name – Allows us to refer to the template from elsewhere in the document priority – NA mode – NA
79.
Understanding <xsl:template-match> - 3 But the XSLT also has one more <xsl:template> element as follows: < xsl:template match ="messageBoard"> Why does this not get the processor’s attention? This is because by default, only the root is loaded; all other templates must be explicitly called by other parts of the XSLT code, as we shall discuss shortly
80.
<xsl:apply-templates> - 1Tells the XSLT processor to Begin a new search for elements in the source XML document that match the search pattern And to look for a matching <xsl:template> element in the XSLT for the same element Here, we have: < xsl:apply-templates select ="disussionForumHome/messageBoard"/> Hence, the XSLT processor searches for A pattern disussionForumHome/messageBoard in the XML document A <template-match> element for the same in the XSLT document
81.
<xsl:apply-templates> - 2The <xsl:apply-templates> element works recursively Here, it tells the XSLT processor to first select the <discussionForumHome> elements of the current node Current node here is the root element of the XML document Hence, it selects all the <discussionForumHome> elements at the root level, which means just one element However, if we had more elements with the same name deeper in the hierarchy, they would have been ignored
82.
<xsl:apply-templates> - 3Assuming that the XSLT processor locates the <discussionForumHome> element, it searches for all of its <messageBoard> children For each <messageBoard> child, the processor looks for the template in our stylesheet that provides the best match Since our stylesheet contains a template that exactly matches the <messageBoard> pattern, it is instantiated for each of the <messageBoard> elements in the source XML
83.
<xsl:template match =“messageBoard”> This template gets invoked for each instance of the <messageBoard> element < xsl:template match ="messageBoard"> < li > < a href ="viewForum?id={@id}"> < xsl:value-of select ="@name"/> </ a > </ li > </ xsl:template > In each case, it produces a line item, consisting of a hyperlink, displaying the name attribute of the <messageBoard> element, and embedding the id attribute in the URL
84.
Summary Most transformationin XSLT is driven by two elements, <xsl:template> and <xsl:apply-templates>. Processing begins at root, and then: For each X in the current node, processor searches for all <xsl:template match = “pattern”> elements in the stylesheet that potentially match the node. The selected <xsl:template match = “pattern”> is instantiated using node X as its current node. This templates typically copies data from the source XML, or produces brand new content in combination with the source data. If the template contains <xsl:apply-templates select = “newPattern” />, a new current node is created and the process repeats recursively.
85.
Comparing <xsl:template> to<xsl:apply-template> Think about the former as similar to a Java method definition, and the later as invoking that method
86.
Controlling the OutputMethod Try adding <xsl:output method=“html” /> <xsl:output method=“xml” /> <xsl:output method=“text” /> one after the other Run the example from the command prompt as: set CLASSPATH=d:\xalan\bin\xalan.jar java org.apache.xalan.xslt.Process -INDENT 3 -IN employee.xml -XSL employee.xsl -OUT output.html
Basic XSL Example(apply0.xml) <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version="1.0"> <xsl:template match="/"> </xsl:template> </xsl:stylesheet> What would be the output?
90.
Explanation We havenot specified what action should be taken when root is applied This causes the entire output to be suppressed!
Explanation Now thereis an <apply-templates> inside the <template match> This causes the default <apply-templates> to be applied, i.e. without any particular select inside <apply-templates> In other words, <apply-templates> gets executed for all elements inside the root
Explanation Empty output,since <apply-templates/> will bring search from the root level to all non-root elements. But at that level, there is no <apply-templates/> to display anything, unlike in the previous example.
XSL Example: 5(apply5.xsl) – Tricky! What if we remove all search paths except the absolute ones? <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/book/chapters/chapter/chapterNo"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="/book/chapters/chapter/chapterTopic"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="/book/chapters/chapter/chapterAuthor"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet>
99.
Explanation Here, thereis no <apply-templates> calling any of the defined <template match> tags Hence, our <template match> tags would get ignored completely Hence, it would produce the full XML contents as the output
Using Mode Ifmore than one template matches an identical pattern, a conflict arises This can be solved by using template priority, but at times, that is also ambiguous In such situations, we can use the template mode Useful when a template needs to visit the same node, but with different results
102.
Mode Example, alsouses CSS XML (css-example-1) <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="css-example-1.xsl"?> < us > < state name =" Hawaii "> < county name =" Hawaii "> < city class =" largest "> Hilo </ city > </ county > < county name =" Honolulu "> < city class =" largest "> Honolulu </ city > </ county > < county name =" Kauai "> < city class =" largest "> Kapaa </ city > </ county > < county name =" Maui "> < city class =" largest "> Kahului </ city > </ county > </ state > </ us > XSL <?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:output method =" html "/> < xsl:template match =" us/state "> < html > < head > < title > State: < xsl:value-of select =" @name "/> </ title > < style type =" text/css "> h1, h2 {font-family: sans-serif, color: blue} ul {font-size: 16pt} </ style > </ head > < body > < h1 > State: < xsl:value-of select =" @name "/> </ h1 > < h2 > All Countries </ h2 > < ul > < xsl:apply-templates select =" county " mode =" county "/> </ ul > < h2 > Largest Cities (by County) </ h2 > < ul > < xsl:apply-templates select =" county " mode =" city "/> </ ul > </ body > </ html > </ xsl:template > < xsl:template match =" county " mode =" county "> < li > < xsl:value-of select =" @name "/> </ li > </ xsl:template > < xsl:template match =" county " mode =" city "> < li > < xsl:value-of select =" city "/> ( < xsl:value-of select =" @name "/> ) </ li > </ xsl:template > </ xsl:stylesheet >
Creating Elements andAttributes <?xml version="1.0"?> <students> <student first_name="Raju"> <id>101</id> <remarks> A student who is not at all sincere!</remarks> </student> <student first_name="Aarati"> <id>102</id> <remarks> A student who is really quite dedicated to her studies!</remarks> </student> <student first_name="Rajani"> <id>103</id> <remarks> A student who is awesome!</remarks> </student> </students>
Explanation - 1<xsl:template match="/"> <xsl:apply-templates/> </xsl:template> match = “/” means select root <xsl:apply-templates/> means look for a “xsl:template match” at the root level; i.e. look for a definition that says <xsl:template match=“students">
107.
Explanation – 2<xsl:template match="students"> <students> <xsl:apply-templates/> </students> </xsl:template> When the students element is found, output <students> </students> Then look for a match inside < students> tag, i.e. for <student> tag now
108.
Explanation – 3<xsl:template match="student"> <xsl:element name="{@first_name}"> <xsl:attribute name="id"><xsl:value-of select="id"/></xsl:attribute> <notes> <xsl:value-of select="remarks"/> </notes> </xsl:element> </xsl:template> When the < student> element is found, create a new element in the output XML whose element name equals the value of the attribute first_name in the input XML So: <student first_name="Raju"> <id>101</id> <remarks> A student who is not at all sincere!</remarks> </student> Will now become <Raju> </Raju>
109.
Explanation – 4<xsl:attribute name="id"> <xsl:value-of select="id"/> </xsl:attribute> Add an attribute named id to the output XML, which equals the value of the element id of the input XML
110.
Explanation – 5<notes> <xsl:value-of select="remarks"/> </notes> Add a new element called as notes to the output XML, which should contain the value of the remarks element of the input XML
111.
Explanation – 6Input XML <student first_name="Raju"> <id>101</id> <remarks> A student who is not at all sincere!</remarks> </student> Output XML <Raju id = “101”> <comments> A student who is not at all sincere!</comments> </Raju>
Sorting on MultipleKeys Specify multiple xsl:sort elements, one after the other Example <xsl:sort select=“SURNAME”/> <xsl:sort select=“FIRSTNAME”/> <xsl:sort select=“BIRTH_DATE” order=“ascending”/>
132.
Another Sort Examplesort-1.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="sort-1.xsl"?> < europe > < state > Belgium </ state > < state > Germany </ state > < state > United Kingdom </ state > < state > France </ state > < state > Spain </ state > < state > Italy </ state > < state > Turkey </ state > < state > Sweden </ state > < state > Ireland </ state > < state > Greece </ state > < state > Malta </ state > < state > Vatican City </ state > < state > Portugal </ state > </ europe > sort-1.xsl <?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:template match =" europe "> < xsl:text > Alphabetical List of European States </ xsl:text > < xsl:text > Total Number of States: </ xsl:text > < xsl:value-of select =" count(state) "/> < xsl:text > </ xsl:text > < xsl:apply-templates select =" state "> < xsl:sort /> </ xsl:apply-templates > </ xsl:template > < xsl:template match =" state "> < xsl:text > - </ xsl:text > < xsl:apply-templates /> < xsl:text > </ xsl:text > </ xsl:template > </ xsl:stylesheet >
133.
Producing HTML Output<?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:output method =" html "/> < xsl:template match =" europe "> < html > < head > < title > European States </ title > </ head > < style type =" text/css "> body {font-family: sans-serif} </ style > < body > < h3 > Alphabetical List of European States </ h3 > < p > < b > Total Number of States: </ b > < xsl:value-of select =" count(state) "/> </ p > < ul > < xsl:apply-templates select =" state "> < xsl:sort /> </ xsl:apply-templates > </ ul > </ body > </ html > </ xsl:template > < xsl:template match =" state "> < li > < xsl:apply-templates /> </ li > </ xsl:template > </ xsl:stylesheet >
Explanation The firsttemplate matches the list element and sorts on the names (using name () ) of the element children (using *) of the list This is the first sort The second template matches only on the element children of list , again using * After inserting some text (such as Section: ), and the name of the element (again using name() ), the template sorts the text node content of item children This is the second sort
Exercise Consider thefollowing XML document <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="books3.xsl"?> <BOOKS> <BOOK pubyear="1929"> <BOOK_TITLE>Look Homeward, Angel</BOOK_TITLE> <AUTHOR>Wolfe, Thomas</AUTHOR> </BOOK> <BOOK pubyear="1973"> <BOOK_TITLE>Gravity's Rainbow</BOOK_TITLE> <AUTHOR>Pynchon, Thomas</AUTHOR> </BOOK> <BOOK pubyear="1977"> <BOOK_TITLE>Cards as Weapons</BOOK_TITLE> <AUTHOR>Jay, Ricky</AUTHOR> </BOOK> <BOOK pubyear="2001"> <BOOK_TITLE>Computer Networks</BOOK_TITLE> <AUTHOR>Tanenbaum, Andrew</AUTHOR> </BOOK> </BOOKS> Do the following: Display all the books published in the 1970s. Display the same information in a tabular form with an asterisk against the book title. Display the same information as in (2), sorted on author name.
if Example –1 Consider an XML file (names.xml) as follows. Display all the names comma-separated in the output HTML file. <?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="ifcomma2.xsl" ?> <namelist> <name>Albert</name> <name>Terrance</name> <name>Will</name> <name>Sylvia</name> <name>Timothy</name> <name>Gordon</name> <name>James</name> <name>Robert</name> <name>Dan</name> <name>Sasha</name> </namelist>
148.
if Example –1 <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="namelist/name"> <xsl:apply-templates/> <xsl:if test="position()!=last()">, </xsl:if> </xsl:template> </xsl:stylesheet>
149.
if Example –2 Another way to achieve the same objective <?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="namelist/name"> <xsl:if test="position()!=1">, </xsl:if> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet>
150.
if Example –3 Consider the following XML file (items.xml) and display alternate rows in yellow background. <?xml version='1.0'?> <?xml-stylesheet type="text/xsl" href="ifyellow.xsl" ?> <items> <item>Car</item> <item>Pen</item> <item>LP Record</item> <item>Wisdom</item> <item>Cell phone</item> <item>Film projector</item> <item>Hole</item> <item>Canopy</item> <item>Widget</item> <item>Concept</item> <item>Null character</item> </items>
choice Example Considerthe following XML file (order.xml). If total number of items selected is <10, display small, if between 10 and 19, display medium, else large. <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="refchoose.xsl" ?> <orders> <order> <lineitem/> <lineitem/> <total>9</total> </order> <order> <lineitem/> <lineitem/> <total>19</total> </order> <order> <lineitem/> <lineitem/> <total>29</total> </order> </orders>
What is theImpact? We have changed variable to parameter Now, we can pass the value of discount from the command prompt! java org.apache.xalan.xslt.Process -INDENT 3 -param discount 0 .20 -IN Variable-example.xml -XSL Variable-example-1.xsl -OUT output.xml
<xsl:number> ContinuedChange the <xsl:number> tag to the following <xsl:number level="multiple"/>
163.
Using position ()XML (functions-example-2) <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="functions-example-2.xsl"?> < europe > < state > Belgium </ state > < state > Germany </ state > < state > United Kingdom </ state > < state > France </ state > < state > Spain </ state > < state > Italy </ state > < state > Turkey </ state > < state > Sweden </ state > < state > Ireland </ state > < state > Greece </ state > < state > Malta </ state > < state > Vatican City </ state > < state > Portugal </ state > </ europe > XSL <?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:output method =" text " indent =" yes "/> < xsl:template match =" europe "> < xsl:apply-templates select =" state "/> </ xsl:template > < xsl:template match =" europe/state "> < xsl:value-of select =" position() "/> < xsl:text > . </ xsl:text > < xsl:value-of select =" . "/> < xsl:text > </ xsl:text > </ xsl:template > </ xsl:stylesheet >
164.
Now Using number Modified XSL <?xml version="1.0" encoding="UTF-8"?> < xsl:stylesheet version =" 1.0 " xmlns:xsl =" http://www.w3.org/1999/XSL/Transform "> < xsl:output method =" text " indent =" yes "/> < xsl:template match =" europe "> < xsl:apply-templates select =" state "/> </ xsl:template > < xsl:template match =" europe/state "> < xsl:number format =" 1 "/> < xsl:text > . </ xsl:text > < xsl:value-of select =" . "/> < xsl:text > </ xsl:text > </ xsl:template > </ xsl:stylesheet >
Requirements A bankallows its account holders to perform money transfer online. Write an application that will: Allow the user to log in to the bank’s site (have a screen that has user id and password, and check if they are valid). If the user is authenticated successfully, show a screen to the user, where the user can select from and to accounts (also show their current balances) and can type the amount to be transferred. If balance is sufficient, execute the money transfer instruction and if successful, show the new balances to the user. Allow the user to log off. Use these technologies: JSP-Servlets, XML-XSL, JAXP, Any RDBMS, HTML-CSS.