860 likes | 1.16k Views
XSL – XML Stylesheet Language. XSL. XSL = XML Stylesheet Language XSL cosists of XPath (navigation in documents) XSLT (T for transformations) XSLFO (FO for formatting objects) This is a rather complex language for typesetting (i.e., preparing text for printing) It will not be taught.
E N D
XSL • XSL = XML Stylesheet Language • XSL cosists of • XPath (navigation in documents) • XSLT (T for transformations) • XSLFO (FO for formatting objects) • This is a rather complex language for typesetting (i.e., preparing text for printing) • It will not be taught
XPath A Language for Locating Nodes in XML Documents
XPath • XPath expressions are written in a syntax that resembles paths in file systems • The list of nodes located by an XPath expression is called a Nodelist • XPath is used in XSL and in XQuery (a query language for XML) • W3Schools has an XPath tutorial • XPath includes • Axis navigation • Conditions • Functions
<?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> <cd country="UK"> <title>Space Oddity</title> <artist>David Bowie</artist> <price>9.90</price> </cd> <cd country="USA"> <title>Aretha: Lady Soul</title> <artist>Aretha Franklin</artist> <price>9.90</price> </cd> </catalog> An XML document
The XML document as a DOM tree catalog.xml catalog cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90
The Main Idea in the Syntax of XPath Expressoins • “/” at the beginning of an XPath expression represents the root of the document • “/” between element names represents a parent-child relationship • “//” represents an ancestor-descendent relationship • “@” marks an attribute • “[condition]” specifies a condition
catalog.xml catalog /catalog cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 Getting the root element of the document
catalog.xml catalog /catalog/cd cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 Finding child nodes
catalog.xml catalog /catalog/cd/price cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 Finding descendent nodes
catalog.xml catalog /catalog/cd[price<10] cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 Condition on elements
catalog.xml /catalog//title catalog //title cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 // represents any directed path in the document
catalog.xml catalog /catalog/cd/* cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 * represents any element name in the document
/*/* catalog.xml What will the following expressions return? //* catalog //*[price=9.90] //*[price=9.90]/* cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 * represents any element name in the document
catalog.xml /catalog/cd[1] catalog /catalog/cd[last()] cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 Position based condition
catalog.xml /catalog/cd[@country=“UK”] catalog cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 @ marks attributes
catalog.xml catalog /catalog/cd/@country cd cd country cd country country USA UK UK title artist price title artist price title artist price Space Oddity Aretha: Lady Soul Dark Side of the Moon David Bowie Aretha Franklin Pink Floyd 9.90 9.90 10.90 @ marks attributes
Relative Navigation Using Axes • Starts with the current node and not with the root (/) • A . marks the current node (e.g., ./title) • A .. marks the parent node (e.g., title/../*) • There are also other axes, e.g., child, descendent, ancestor, parent, following-sibling, etc.
Functions • Many functions that are included in XPath • Some examples: • count() – returns the number of nodes in a nodelist • last() – returns the last node in a nodelist • name() – returns the name of a node • position() – returns the position of the node in the nodelist
Additional Examples ofXPath Expressions These examples use element names that are not necessarily from the XML document that was shown previously
Examples of XPath Expressions • para • Selects the para children elements of the context node • * • Selects all element children of the context node • text() • Selects all text node children of the context node • @name • Selects the name attribute of the context node
More Examples ofXPath Expressions • @* • Selects all the attributes of the context node • para[1] • Selects the first para child of the context node • para[last()] • Selects the last para child of the context node • */para • Selects all para grandchilren of the context node
More Examples ofXPath Expressions • /doc/chapter[5]/section[2] • Selects the second section of the fifth chapter of the doc • chapter//para • Selects the para element descendants of the chapter element children of the context node • //para • Selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
More Examples ofXPath Expressions • //olist/item • Selects all the item elements that have an olist parent and are in the same document as the context node • . • Selects the context node • .//para • Selects the para descendants of the context node • .. • Selects the parent of the context node
More Examples ofXPath Expressions • ../@lang • Selects the lang attribute of the parent of the context node • para[@type=“warning”] • Selects the para children of the context node that have a type attribute with value warning • chapter[title] • Selects the chapter children of the context node that have one or more title children
More Examples ofXPath Expressions • para[@type=“warning”][5] • Selects the fifth para child among the children of the context node that have a type attribute with value warning • para[5][@type=“warning”] • Selects the fifth para child of the context node if that child has a type attribute with value warning
More Examples ofXPath Expressions • chapter[title=“Introduction”] • Selects the chapter children of the context node that have one or more title children with string-value equal to Introduction • employee[@secretary and @assistant] • Selects employee children of the context node that have both a secretary attribute and an assistant attribute
More Examples of Xpath Expressions • /university/department/course • This Xpath expression matches any path that starts at the root, which is a university element, passes through a department element and ends in a course element • ./department/course[@year=2002] • This Xpath expression matches any path that starts at the current element, continues to a child which is a department element and ends at a course element with a year attribute that is equal to 2002
Location Paths • The previous examples are abbreviations of location paths • See XPath tutorial in W3Schools or Costello’s slides on long names • For example, // is short for /descendant-or-self::node()/. • //para is short for /descendant-or-self::node()/child::para
XSLT Transforming XML documents into other XML documents
XSLT Stylesheet • An XSLT stylesheet is a program that transforms an XML document into another XML document • For example: • Transforming XML to XHTML (HTML that conforms to XML syntax) • Transforming an XML document to WML (a format of XML that cellular phones can display)
A Few Things About XSL • XSL is a high-level, functional language • The syntax is a bit peculiar and possibly confusing • An XSL style sheet is a valid XML document • Valid with respect to the XSL namespace • Therefore, commands in XSL are XSL elements
Applying XSLT Stylesheets toXML Documents • There are three ways of applying an XSLT stylesheet to an XML document • Directly applying an XSLT processor to the XML document and the XSLT stylesheet • Calling an XSLT processor from within a (Java) program • Adding to the XML document a link to the XSL stylesheet and letting the browser do the transformation
java org.apache.xalan.xslt.Process -IN myXmlFile.xml -XSL myXslFile.xsl -OUT myOutputFile.html Directly applying the Xalan XSL processor Using an XSL Processor XSL stylesheet XML document Result is either an XML, HTML or text document XSL Processor
A link to the stylesheet Letting a Browser Perform the Transformation <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href=“catalog.xsl"?> <catalog> <cd country="UK"> <title>Dark Side of the Moon</title> <artist>Pink Floyd</artist> <price>10.90</price> </cd> … </catalog>
The Root of the XSL Document • The Root of the XSL document should be one of the following lines: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> The namespace allows the XSL processor to distinguish between XSL tags and tags of the result document
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 document) • Each template has a matchattributethat specifies to which source nodes the template can be applied • The current source node is processed by applying a template that matches this node • Processing always starts at the root (/)
Templates • A template has the form <xsl:template match="pattern"> ... </xsl:template> • The content of a template consists of • XML elements and text that are copied to the result • XSL elements that are actually instructions • The pattern syntax is a subset of XPath
<?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> <body> <h1>Hello World</h1> </body> </html> </xsl:template> </xsl:stylesheet>
<html> <body> <h1>Hello World</h1> </body> </html> Applying a browser to catalog.xml (catalog.xml has a link to catalog.xsl)
The Element<xsl:apply-templates> • Processing starts by applying a template that matches the root (/) • If the given XSL stylesheet does not have a template that matches the root, then one is inserted by default (see the slide on “Default Templates”) • The XSL stylesheet must specify explicitly whether templates should be applied to descendants of the root • It is done by putting inside a template the instruction: <xsl:apply-templates select="xpath"/> • Without the select attribute, this instruction processes all the children of the current node
<?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> <body> <xsl:apply-templates select="catalog/cd"/> </body> </html> </xsl:template> <xsl:template match="cd"> <h2>A CD!</h2> </xsl:template> </xsl:stylesheet> <html> <body> <h2>A CD!</h2> <h2>A CD!</h2> <h2>A CD!</h2> </body> </html>
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:template match=“/ | *”> <xsl:apply-templates/> </xsl:template> <xsl:template match=“text()”> <xsl:value-of select=“.”/> </xsl:template>
<?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=“cd[title=‘Space Oddity’]"> <html> <body> <h1>Hello World</h1> </body> </html> </xsl:template> </xsl:stylesheet> What is generated when this xsl is applied to catalog.xml?
The default templates print the text in the leaves of the first and third cd’s This is printed by the template from the previous slide Dark Side of the Moon Pink Floyd 10.90 <html><body><h1>Hello World</h1></body></html> Aretha: Lady Soul Aretha Franklin 9.90
The Most Frequently Used Elements of XSL • <xsl:value-of select=“xpath-expression”/> • This element extracts the value of a node from the nodelist located by xpath-expression • <xsl:for-each select=“xpath-expression”/> • This element loops over all the nodes in the nodelist located by xpath-expression • <xsl:if test=“xpath-expression”/>, <xsl:if test=“xpath-expression=value”/>, etc. • This element is for conditional processing
The <xsl:value-of> Element <xsl:value-of select=“xpath-expression”/> • The XSL element <xsl:value-of> can be used to extract the value of an element that is selected from the source XML document • The extracted value is added to the output stream • The selected element is located by an XPath expression that appears as the value of the select attribute
<?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> <body> <h2>A CD Catalog</h2> <table border="1"> <tr bgcolor=“yellow"> <th>Title</th> <th>Artist</th> </tr>
<tr> <td><xsl:value-of select="catalog/cd/title"/> </td> <td><xsl:value-of select="catalog/cd/artist"/> </td> </tr> </table> </body> </html> </xsl:template> </xsl:stylesheet> Note that only the first matched element is retrieved for each <xsl:value of>