XSL XML Stylesheet Language 1 XSL XSL XML
- Slides: 84
XSL – XML Stylesheet Language 1
XSL • XSL = XML Stylesheet Language • XSL cosists of – XPath (navigation in documents) – XSLT (T for transformations) – XSLFO (FO formatting objects) • This is a rather complex language for typesetting (i. e. , preparing text for printing) • It will not be taught 2
XPath A Language for Locating Nodes in XML Documents 3
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) • W 3 Schools has an XPath tutorial • XPath includes – Axis navigation – Conditions – Functions 4
<? 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> An XML document <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> 5 </catalog>
catalog. xml The XML document as a DOM tree country UK cd catalog country title artist price UK cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie 9. 90 Aretha Franklin 9. 90 6
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 7
catalog. xml catalog cd country UK country title artist price UK /catalog cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie Aretha Franklin 9. 90 Getting the root element of the document 9. 90 8
catalog. xml catalog country UK cd country title artist price UK /catalog/cd cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie 9. 90 Finding child nodes Aretha Franklin 9. 90 9
catalog. xml catalog country UK cd country title artist price UK /catalog/cd/price cd cd USA title artist price Space Oddity Dark Side of the Moon Aretha: Lady Soul David Bowie Pink Floyd 10. 90 country Aretha Franklin 9. 90 Finding descendent nodes 9. 90 10
catalog. xml catalog country UK cd country title artist price UK /catalog/cd[price<10] cd cd USA title artist price Space Oddity Dark Side of the Moon Aretha: Lady Soul David Bowie Pink Floyd 10. 90 country Aretha Franklin 9. 90 Condition on elements 9. 90 11
catalog. xml country UK cd country title artist price UK catalog //title cd cd Pink Floyd 10. 90 country USA title artist price Space Oddity Dark Side of the Moon /catalog//title Aretha: Lady Soul David Bowie Aretha Franklin 9. 90 // represents any directed path in the document 9. 90 12
catalog. xml catalog country UK cd country title artist price UK /catalog/cd/* cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie Aretha Franklin 9. 90 * represents any element name in the document 9. 90 13
/*/* catalog. xml What will the following expressions return? //* catalog //*[price=9. 90]/* country UK cd country title artist price UK cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie Aretha Franklin 9. 90 * represents any element name in the document 9. 90 14
catalog. xml /catalog/cd[1] catalog country UK cd country title artist price UK /catalog/cd[last()] cd cd USA title artist price Space Oddity Dark Side of the Moon Aretha: Lady Soul David Bowie Pink Floyd 10. 90 country Aretha Franklin 9. 90 Position based condition 9. 90 15
catalog. xml /catalog/cd[@country=“UK”] catalog country UK cd country title artist price UK cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie 9. 90 @ marks attributes Aretha Franklin 9. 90 16
catalog. xml catalog /catalog/cd/@country UK cd country title artist price UK cd cd USA title artist price Space Oddity Dark Side of the Moon Pink Floyd 10. 90 country Aretha: Lady Soul David Bowie 9. 90 @ marks attributes Aretha Franklin 9. 90 17
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 also other axes, e. g. , child, descendent, ancestor, parent, followingsibling, etc. 18
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 19
Additional Examples of XPath Expressions These examples use element names that are not necessarily from the XML document that was shown previously 20
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 21
More Examples of XPath 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 22
More Examples of XPath 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 23
More Examples of XPath 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 24
More Examples of XPath 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 25
More Examples of XPath 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 26
More Examples of XPath 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 27
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 28
Location Paths • The previous examples are abbreviations of location paths – See XPath tutorial in W 3 Schools 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 29
XSLT Transforming XML documents into other XML documents 30
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) 31
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 32
Applying XSLT Stylesheets to XML 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 33
Using an XSL Processor XSL stylesheet XML document XSL Processor Result is either an XML, HTML or text document java org. apache. xalan. xslt. Process -IN my. Xml. File. xml -XSL my. Xsl. File. xsl -OUT my. Output. File. html Directly applying the Xalan XSL processor 34
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> A link to the stylesheet 35
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. w 3. org/1999/XSL/Transform"> <xsl: transform version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> The namespace allows the XSL processor to distinguish between XSL tags and tags of the result document 36
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 match attribute that 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 (/) 37
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 38
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: template match="/"> <html> <body> <h 1>Hello World</h 1> </body> </html> </xsl: template> </xsl: stylesheet> 39
<html> <body> <h 1>Hello World</h 1> </body> </html> Applying a browser to catalog. xml (catalog. xml has a link to catalog. xsl) 40
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 41
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/"> <html> <body> <xsl: apply-templates select="catalog/cd"/> </body> <html> </html> <body> </xsl: template> <xsl: template match="cd"> <h 2>A CD!</h 2> </xsl: template> <h 2>A CD!</h 2> </body> </xsl: stylesheet> 42 </html>
Default Templates • XSL provides implicit built-in templates that match every element and text nodes <xsl: template match=“/ | *”> <xsl: apply-templates/> </xsl: template> <xsl: template match=“text()”> <xsl: value-of select=“. ”/> </xsl: template> • Templates we write always override these built-in templates (when they match) 43
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: template match=“cd[title=‘Space Oddity’]"> <html> <body> <h 1>Hello World</h 1> </body> What is generated when </html> this xsl is applied to </xsl: template> catalog. xml? </xsl: stylesheet> 44
The default templates print the text in the leaves of the first and third cd’s Dark Side of the Moon Pink Floyd 10. 90 <html><body><h 1>Hello World</h 1></body></html> Aretha: Lady Soul Aretha Franklin 9. 90 This is printed by the template from the previous slide 45
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 46
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 47
Selected values 48
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: template match="/"> <html> <body> <h 2>A CD Catalog</h 2> <table border="1"> <tr bgcolor=“yellow"> <th>Title</th> <th>Artist</th> </tr> 49
<tr> <td><xsl: value-of select="catalog/cd/title"/> </td> <td><xsl: value-of select="catalog/cd/artist"/> </td> </tr> </table> Note that only the first matched </body> element is retrieved for each </html> </xsl: template> <xsl: value of> </xsl: stylesheet> 50
The <xsl: for-each> Element <xsl: for-each select=“xpath-expression”/> • The <xsl: for-each> element loops over all the nodes in the nodelist of the XPath expression that appears as the value of the select attribute • The value of each node can be extracted by an <xsl: value-of> element 51
All the values are selected 52
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: template match="/"> <html> <body> <h 2>A CD Catalog</h 2> <table border="1"> <tr bgcolor=“yellow"> <th>Title</th> <th>Artist</th> </tr> As in the previous example 53
<xsl: for-each select="catalog/cd"> <tr> <td><xsl: value-of select="title"/> </td> <td><xsl: value-of select="artist"/> </td> </tr> </xsl: for-each> </table> </body> </html> </xsl: template> </xsl: stylesheet> Note that all the /catalog/cd elements are retrieved 54
Consider the following change in the select attribute: <xsl: for-each select="catalog/cd[price< 10]"> <tr> <td><xsl: value-of select="title"/> </td> <td><xsl: value-of select="artist"/> </td> </tr> </xsl: for-each> </table> </body> </html> Only elements that satisfy </xsl: template> </xsl: stylesheet> /catalog/cd[price<10] are retrieved 55
56
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 57
Sorted by the name of the artist 58
<xsl: for-each select="catalog/cd"> <xsl: sort select="artist"/> <tr> <td><xsl: value-of select="title"/> </td> <td><xsl: value-of select="artist"/> </td> </tr> </xsl: for-each> </table> </body> </html> The /catalog/cd elements </xsl: template> </xsl: stylesheet> are sorted according to the value of the artist element 59
The <xsl: if> Element • The <xsl: if> element is used for conditional processing • The condition appears as the value of the test attribute, for example: <xsl: if test="price > 10"> some output. . . </xsl: if> • The elements inside the <xsl: if> element are processed if the condition is true 60
Note • Processing the inside elements means – Copying them into the output stream if they are not XSL elements, and – Evaluating them if they are XSL elements • If the value of the test attribute is just an XPath expression (i. e. , without any comparison), then the test is satisfied if the nodelist of this XPath expression is not empty 61
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: template match="/"> <html> <body> <h 2>A CD Catalog</h 2> <table border="1"> <tr bgcolor=“yellow"> <th>Title</th> <th>Artist</th> </tr> As in the previous examples 62
<xsl: for-each select="catalog/cd"> <xsl: if test="price > 10"> <tr> <td><xsl: value-of select="title"/> </td> <td><xsl: value-of select="artist"/> </td> </tr> </xsl: if> </xsl: for-each> </table> </body> </html> Only /catalog/cd with </xsl: template> price>10 are retrieved </xsl: stylesheet> 63
64
Question • In the previous example, what should be the condition if we want to extract the information for all cd’s that have a price (i. e. , the information about the price is not missing)? 65
The <xsl: choose> Element • The <xsl: choose> element is used in conjunction with <xsl: when> and <xsl: otherwise> to express test with multiple conditions • There can be many <xsl: when> inside an <xsl: choose> element, but there should be a single <xsl: otherwise> inside an <xsl: choose> element 66
Using <xsl: choose> • To insert a conditional choose against the content of the XML file, simply add the <xsl: choose>, <xsl: when>, and <xsl: otherwise> elements to your XSL document like this: <xsl: choose> <xsl: when test="price > 10"> . . . some code. . . </xsl: when> <xsl: otherwise> . . . some code. . </xsl: otherwise> </xsl: choose> 67
<xsl: for-each select="catalog/cd">>tr< > td><xsl: value-of select="title"/></td< <xsl: choose> <xsl: when test="price > 10"> <td bgcolor="red"> <xsl: value-of select="artist"/></td> </xsl: when> <xsl: when test="price> 9 and price< =10<" <td bgcolor="gray"> <xsl: value-of select="artist"/></td> </xsl: when> <xsl: otherwise> <td><xsl: value-of select="artist"/></td> </xsl: otherwise> />xsl: choose></tr< />xsl: for-each < 68
69
Applying Templates Recursively • The following example shows how to apply templates recursively • Generally, it is possible (but not in this example) that more than one template matches the current source node • The specification (www. w 3. org/TR/xslt) describes (Section 5. 5) which template should be chosen for application 70
<? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: template match="/"> >html< > body< > h 2>A CD Catalog</h 2 < > xsl: apply-templates </ /> body< /> html< />xsl: template< 71
<xsl: template match="cd"> >p< > xsl: apply-templates select="title </" > xsl: apply-templates select="artist</" /> p< />xsl: template< >xsl: template match="title<" Title: <span style="color: red<" > xsl: value-of select=". "/></span< > br</ />xsl: template< 72
<xsl: template match="artist"> Artist: <span style="color: green<" > xsl: value-of select=". "/></span< > br</ />xsl: template< />xsl: stylesheet< 73
74
Is Recursive Application of Templates Really Needed? • The output of the previous example can also be generated by an XSL stylesheet that uses only one template that matches the root (and does not use the element <xsl: apply-templates>) • However, some tasks can only be done by applying templates recursively – This typically happens when the structure of the source XML document is not known 75
For example • Suppose that we want to write an XSL stylesheet that generates an exact copy of the source XML 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) 76
<? xml version="1. 0"? > <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform" version="1. 0"> <xsl: output method="xml"/> <xsl: template match="*"> <xsl: element name="{name(. )}"> <xsl: for-each select="@*"> <xsl: attribute name="{name(. )}"> <xsl: value-of select=". "/> </xsl: attribute> </xsl: for-each> <xsl: apply-templates/> </xsl: element> </xsl: template> Identity Transformation </xsl: stylesheet> Stylesheet 77
The <xsl: output> Element <? xml version="1. 0" encoding="ISO-8859 -1"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/ Transform"> <xsl: output method="xml" version="1. 0" encoding="iso-8859 -1" indent="yes"/>. . . </xsl: stylesheet> Tells in what format the output should be: xml/html/text 78
Some Other XSL Elements • The <xsl: text> element allows to insert free text in the output • The <xsl: copy-of> element creates a copy of the current node • The <xsl: comment> element is used to create a comment node in the result tree • There are more elements and functions: look in the specification! (www. w 3. org/TR/xslt) 79
Costello’s Tutorial (See DBI Lecture Schedule for a reference) • Costello has an excellent and detailed tutorial – First part (xsl 01. ppt) has many examples that do not use recursion – Second part (xsl 02. ppt) explains how to use recursion and why it is needed (the identity transformation shown earlier is from Costello’s tutorial) 80
W 3 Schools Tutorial on XSLT • The W 3 Schools XSLT Tutorial has (among other things) tables that list all the elements and functions of XSLT • It also has some details about implementations – Some browsers may not implement all features or may implement some features differently from the specifications 81
How to Use the Examples • Since XSL is a high-level language, XSLT stylesheets are relatively short and easy to understand • It is easy to modify an existing stylesheet into another stylesheet with a similar functionality – The many examples of Costello and W 3 Schools are very useful as a starting point for writing your own stylesheets 82
Summary • XSLT is a high-level transformation language • Create core output once in XML format (using Servlets, JSP, etc. ) • Use XSLT to transform the core output as needed 83
For Example • Suppose that you have produced an XML catalog of products • Now you decide to lower all prices by 10% • It is easy to do it in XSLT – Modify the identity transformation to do that 84
- Stylesheet design
- User stylesheet
- Path relative stylesheet
- Xml
- What does xsl stand for
- Xslt stands for _____.
- Xsl formatting objects
- Xsl processor
- Language definition
- Xsl
- Esslii
- Pacman max level
- Xml user interface language
- Xml extensible markup language
- Zig web framework
- Microsoft xml parser
- Xray xml editor
- Dublin core xml
- Java soap xml 파싱
- Java xml
- Xml meaning
- Xml stands
- Oracle xml gateway white paper
- Syntax xml
- Xml dom sax
- Textml server reviews
- Extracting data from xml
- Specifications and constraints
- Xml meaning
- Xml vs xbrl
- Xml web service
- Ximpleware
- Sas read xml
- Vi format xml
- Kml to xml
- Single source publishing software
- Soa xml
- Reading basics
- Slidetodoc
- Open xml sdk productivity tool
- Oodb and xml database
- Klavarscript
- Bnf json
- Jeus domain.xml
- Xml.aimsweb
- Element in xml
- Internal and external dtd
- Parse xml in power automate
- Sgml
- Syntax xml
- Xml basics
- Xml ivr
- Xml dom
- Xml 1
- Xml
- Microsoft schemas xml
- Php xml viewer
- Xml reporting tools
- Xml dom defines a standard way for
- Splunk datetime.xml
- The text is intended for
- Xml format
- Excel xml maps are not exportable
- Xml logistics
- Schema l
- Linq2 アプリ
- Cisoa
- Exi xml
- Mismo xml
- Asynchronous javascript and xml
- Zenetr
- Xml file reader
- L xml
- Xml file reader
- Xml ecommerce
- Xml sax dom
- Extract xml oracle ejemplos
- Cobol xml generate example
- Xml 101
- Sas read xml file
- What is ajax
- Xml in java
- Dicom xml
- Tia portal compatibility
- Xml composer