1 / 56

Extensible Stylesheet Language (XSL)

Extensible Stylesheet Language (XSL). XSL. XSL is a standard that consists of three parts: XPath (navigation in documents) XPath was taught in the DB course, so it will not be taught XSLT (transformation of documents) XSLFO (FO for formatting objects)

malana
Download Presentation

Extensible Stylesheet Language (XSL)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Extensible Stylesheet Language (XSL) cs336607

  2. XSL XSL is a standard that consists of three parts: • XPath (navigation in documents) • XPath was taught in the DB course, so it will not be taught • XSLT (transformation of documents) • XSLFO (FO for formatting objects) • This is a rather complex language for typesetting (i.e., preparing text for printing) • It will not be taught cs336607

  3. XSL Transformations (XSLT) cs336607

  4. XSLT • XSLT is a language for transforming XML documents into other XML documents • For example, XHTML, RSS, KML, GML, MathML, WML • Can also transform XML to text documents, e.g., SQL programs • An XSLT program is itself an XML document (called an XSL stylesheet) that describes the transformation process for input documents cs336607

  5. XSLT Processors XSLT Processor cs336607

  6. Web Pages – The Whole Picture Web Page Data Layout Knowledge Doc. Structure XSL XHTML Style CSS XML cs336607

  7. <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cdcountry="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cdcountry="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cdcountry="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog> catalog.xml cs336607

  8. Valid XML! Commands are XML elements with the namespace xsl Includes XHTML elements <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template> </xsl:stylesheet> catalog.xsl cs336607

  9. Applying XSL Stylesheets to XML There are several ways of applying an XSL stylesheet to an XML document: • Directly applying anXSLT processor to the XML document and the XSL stylesheet • Calling an XSLT processor from within a program • Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation • The resulting XHTML document is shown instead of the original XML cs336607

  10. Processing XSL in Java You can use the XALAN package of Apache in order to process XSL transformations javaorg.apache.xalan.xslt.Process -INmyXmlFile.xml -XSLmyXslFile.xsl -OUTmyOutputFile.html cs336607

  11. How Does XSLT Work? • An XSL stylesheet is a collection of templates that are applied to source nodes (i.e., nodes of the given XML) • Each template has a match attributethat specifies to which source nodes the template can be applied • Each source node has at a template that matches it • The current source node is processed by applying a template that matches this node • When processing a node, it is possible (but not necessary) to recursively process other nodes, e.g., the children of the processed node • The XSLT processor processes the document root (/) cs336607

  12. Templates • A template has the form <xsl:templatematch="pattern"> ... </xsl:template> • The content of a template consists of • XML elements (e.g., XHTML) and text that are copied to the result • XSL elements (<xsl:…>) that are actually instructions • The pattern syntax is a subset of XPath cs336607

  13. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template> </xsl:stylesheet> catalog1.xsl cs336607

  14. <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheettype="text/xsl"href="catalog1.xsl"?> <catalog> <cdcountry="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cdcountry="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cdcountry="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog> catalog1.xml cs336607

  15. The Result <html> <head> <METAhttp-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body> </html> In XALAN, automatically added to <head> cs336607

  16. cs336607

  17. Examples of Match Attributes • match="cd", • All elements with tag name cd • match="//cd", match="/catalog/cd/artist" • All matches of the absolute XPath • match="cd/artist" • All artist nodes that have a cd parent • match="catalog//artist" • All artist nodes that have a catalog ancestor • match="cd[@country='UK']/artist" cs336607

  18. <xsl:apply-templates> • Processing starts by applying a template to the root • If no specified template matches the root, then one is inserted by default (see the next slide) • The XSL stylesheet must specify explicitly whether templates should be applied to descendants of a node • It is done by putting inside a template the instruction: <xsl:apply-templates select="xpath"/> • In xpath, the current processed node is used as the context node • Without the select attribute, this instruction processes all the children of the current node (including text nodes) cs336607

  19. Default Templates • XSL provides implicit built-in templates that match every element and text nodes • Templates we write always override these built-in templates (when they match) <xsl:templatematch="/|*"> <xsl:apply-templates/> </xsl:template> <xsl:templatematch="text()|@*"> <xsl:value-ofselect="."/> </xsl:template> cs336607

  20. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> </xsl:stylesheet> <?xml version="1.0" encoding="UTF-8"?> Dark Side of the Moon Pink Floyd 10.90 Space Oddity David Bowie 9.90 Aretha: Lady Soul Aretha Franklin 9.90 In XALAN and in IE (it yields an empty page in Firefox) cs336607

  21. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="cd"> <h2>A cd!</h2> </xsl:template> </xsl:stylesheet> <h2>A cd!</h2> <h2>A cd!</h2> <h2>A cd!</h2> cs336607

  22. <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="cd[@country='UK']"> <h2>A cd!</h2></xsl:template> </xsl:stylesheet> <h2>A cd!</h2> <h2>A cd!</h2> Aretha: Lady Soul Aretha Franklin 9.90 cs336607

  23. <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <xsl:apply-templates select="catalog/cd[@country='UK']/artist"/> </xsl:template> <xsl:templatematch="artist"> <h2>An artist!</h2> </xsl:template> </xsl:stylesheet> <h2>An artist!</h2> <h2>An artist!</h2> cs336607

  24. <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <xsl:apply-templates select="cd[@country='UK']/artist"/> </xsl:template> <xsl:templatematch="artist"> <h2>An artist!</h2> </xsl:template> </xsl:stylesheet> Does not match the context node cs336607

  25. <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <xsl:apply-templatesmode=“first” select="catalog/cd[@country='UK']/artist"/> <xsl:apply-templatesmode=“second” select="catalog/cd[@country='UK']/artist"/> </xsl:template> <xsl:templatemode=“first”match="artist"> <h2>An artist!</h2> </xsl:template> <xsl:templatemode=“second”match="artist"> <h2>The artist is coming back!</h2> </xsl:template> </xsl:stylesheet> cs336607

  26. cs336607

  27. The Most Frequently Used Elements of XSL • <xsl:value-ofselect="xpath"/> • This element extracts the value of a node from the nodelist located by xpath • <xsl:for-eachselect="xpath"/> • This element loops over all the nodes in the node list located by xpath • <xsl:iftest="cond"/>, <xsl:iftest="xpath"/>, etc. • This element is for conditional processing cs336607

  28. Example 1 <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><head><title>cd catalog</title></head> <body> <h1>CD catalog</h1> <ul> <xsl:for-eachselect="catalog/cd"> <li><xsl:value-ofselect="title"/> [<xsl:value-ofselect="artist"/>]</li> </xsl:for-each> </ul></body></html> </xsl:template> </xsl:stylesheet> Currently selected element is the context (current) node catalog2.xsl cs336607

  29. cs336607

  30. Example 2 <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><head><title>cd catalog</title></head> <body> <h1>CD catalog</h1> <ul> <xsl:for-eachselect="catalog/cd"> <li><xsl:apply-templates select="."/></li> </xsl:for-each> </ul></body></html> </xsl:template> catalog3.xsl cs336607

  31. Entities replace characters price<10 → price&lt;10 Example 2 (cont.) <xsl:templatematch="cd"> <b><xsl:value-ofselect="artist"/></b>: <xsl:value-ofselect="title"/> <xsl:iftest="price&lt;10"> (<em>now on sale: $<xsl:value-ofselect="price"/> </em>) </xsl:if> </xsl:template> </xsl:stylesheet> cs336607

  32. <xsl:choose>: Switch syntax for conditions Example 3 <xsl:choose> <xsl:whentest="price &lt; 9"> (<em>Special price!</em>) </xsl:when> <xsl:whentest="price&gt;9 and price&lt;=10"> (<i>Good price!</i>) </xsl:when> <xsl:otherwise> (Normal price.) </xsl:otherwise> </xsl:choose> cs336607

  33. The <xsl:sort> Element • The <xsl:sort> element is used to sort the list of nodes that are looped over by the <xsl:for-each> element • Thus, the <xsl:sort> must appear inside the <xsl:for-each> element • The looping is done in sorted order cs336607

  34. CDs are iterated in ascending order of the titles <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html><head><title>cd catalog</title></head> <body> <h1>CD catalog</h1> <ul> <xsl:for-eachselect="catalog/cd"> <xsl:sortselect="title"/> <li><xsl:value-ofselect="title"/></li> </xsl:for-each> </ul></body></html> </xsl:template> </xsl:stylesheet> catalog4.xsl cs336607

  35. Setting Values in Attributes • The<xsl:value-of> element cannot be used within attribute value • However, we can insert expressions into attribute values, by putting the expression inside curly braces ({}) • Alternatively, we can use <xsl:element> in order to construct XML elements cs336607

  36. An Example • In the following example, we add to each CD entitled t a link to the URL /showcd.jsp?title=t <xsl:template match="cd"> <b><xsl:value-of select="artist"/></b>: <ahref="/showcd.jsp?title={./title}"> <xsl:value-of select="title"/> </a> </xsl:template> cs336607

  37. cs336607

  38. Using <xsl:element> <xsl:template match="cd"> <b><xsl:value-of select="artist"/></b>: <xsl:elementname="a"> <xsl:attributename="href"> /showcd.jsp?title=<xsl:value-of select="title"/> </xsl:attribute> <xsl:value-of select="title"/> </xsl:element> </xsl:template> cs336607

  39. On XSL Code • Typically, an XSLT program can be written in several, very different ways • Templates can sometime replace loops and vice versa • Conditions can sometimes be replaced with XPath predicates (e.g., in the select attribute) and vice versa • A matter of convenience and elegancy cs336607

  40. On Recursive Templates • It is not always possible to avoid recursive templates • That is, use only the template of the root • Suppose that we want to write an XSL stylesheet that generates a copy of the source document • It is rather easy to do it when the structure of the source XML document is known • Can we write an XSL stylesheet that does it for every possible XML document? • Yes! (see next slide) cs336607

  41. <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:outputmethod="xml"/> <xsl:template match="*"> <xsl:element name="{name()}"> <xsl:for-eachselect="@*"> <xsl:attributename="{name()}"> <xsl:value-ofselect="."/> </xsl:attribute> </xsl:for-each> <xsl:apply-templates/> </xsl:element> </xsl:template> </xsl:stylesheet> Identity Transformation Stylesheet cs336607

  42. Generating Valid XHTML • By default, the documents that XSL stylesheets generate are not valid XHTML • Next, we will show how XSL stylesheet can be changed in order to generate valid XHTML cs336607

  43. The Original XSL Example <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheetversion="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:templatematch="/"> <html> <head><title>cd catalog</title></head> <body><h1>This is a cd catalog!</h1></body> </html> </xsl:template> </xsl:stylesheet> cs336607

  44. The Original Transformation Result <html> <head> <METAhttp-equiv="Content-Type"content="text/html; charset=UTF-8"> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body> </html> No DOCTYPE Uppercase tag name, unclosed element cs336607

  45. Modifying the XSL Example <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system= "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/> <xsl:template match="/"> … cs336607

  46. <xsl:output/> <xsl:output method="xml|html|text|name" version="string" encoding="string" omit-xml-declaration="yes|no" standalone="yes|no" doctype-public="string" doctype-system="string" cdata-section-elements="namelist" indent="yes|no" media-type="string"/> cs336607

  47. The Transformation Result <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>cd catalog</title> </head> <body> <h1>This is a cd catalog!</h1> </body> </html> META is not inserted cs336607

  48. Some Other XSL Elements • The <xsl:text> element inserts free text in the output • The <xsl:copy-ofselect="xpath"> creates a copy of the specified nodes (deep copy, i.e., copies the entire subtree) • The <xsl:copyselect="xpath"> creates a copy of the specified nodes (does not copy children or attributes) • The <xsl:comment> element creates a comment node in the result tree • The <xsl:variable> element defines a variable (local or global) that can be used within the program cs336607

  49. Example • Transform this list of number to be • Sorted • Alternatingly red and blue cs336607

  50. cs336607

More Related