8. XSL Transformations (XSLT) • XSL = Extensible Stylesheet Language • Task: Transform the XML document from one vocabulary to another (source tree � result tree) • The result syntax can be also non-XML (second conversion often needed, e.g. to PS or PDF ) • Purposes: – Communicating in a heterogeneous network – Formatting for output • Parts of XSL specification (2001) – XSLT : general transformation language (1999) – XSL-FO : XSL Formatting Objects (cf. CSS ) XML-8 J. Teuhola 2013 121
Basic idea of XSLT • The XSL stylesheet is a well-formed XML document consisting of template rules . • Template rule = pattern + template • An XSLT processor does the following: – Compares the elements and attributes of the source document with the patterns. – In case of match, writes the template to the result. – Some items are usually extracted from the source to specified ’slots’ in the template. • XSLT can be used together with DTDs and namespaces. XML-8 J. Teuhola 2013 122
Illustration of the basic idea XML source document XSLT Result of processor transform XSL style- sheet XML-8 J. Teuhola 2013 123
XSLT stylesheet document • Starts with an XML declaration. • May have a DTD (usually not). • Namespace: – Typical prefix: xsl – Version 1.0 URI: http://www.w3.org/TR/xslt Version 2.0 URI: http://www.w3.org/TR/xslt20/ • Alternative root elements (synonyms): xsl:transform or xsl:stylesheet • The root must declare the version=”1.0” or “2.0” • Simplest form: An empty root defines a default transformation extracting the plain text content (gathered from leaf nodes; result not well-formed XML) XML-8 J. Teuhola 2013 124
XSLT processors • Implementation alternatives: – Part of a web browser (esp. if the output format is HTML). Example: MSXML processor in IE 6. – Part of a web/application server, e.g. Apache’s Cocoon (http://xml.apache.org/cocoon) – Standalone programs (called from command line), e.g. • xalan (http://xml.apache.org/xalan-j) • saxon (http://saxon.sourceforge.net) • A processing instruction in the source document tells the location (href) of the stylesheet file: <?xml-stylesheet type=”application/xml” href=”...”?> XML-8 J. Teuhola 2013 125
Template rules • Describe the conversion from source to target • The rule is an xsl:template element, with – the pattern as the match attribute (written in XPath language, see Chapter 9), – the template to be output as content. • The template may contain – plain text to be output (’literal data characters’), – well-formed markup to be copied from the template (’literal result elements’), – parts extracted from the input document. XML-8 J. Teuhola 2013 126
Simple example (no explicit extraction) Source document : Stylesheet : <?xml version=”1.0”> <xsl:stylesheet version= <?xml version=”1.0”> ”1.0” xmlns=”http://...”> <course> <xsl:template <name>Adv DB</name> match=”student”> <teacher>Timo</teacher> Student name <audience> </xsl:template> <student>Esa</student> </xsl:stylesheet> <student>Anu</student> </audience> </course> XML-8 J. Teuhola 2013 127
Simple example (cont.) Result : <?xml version=1.0 …?>Adv DBTimo Student name Student name Notes about the example : • The default template rule is also applied, producing texts ‘Adv DB’ and ‘Timo’. • Whitespace characters (e.g. linefeed) in the pattern are preserved. XML-8 J. Teuhola 2013 128
Copying values from source to target • Within the template, xsl:value-of extracts the content of an element/attribute: <xsl:value-of select=”…”> where ”…” is an XPath expression specifying the item to be selected from the current context (i.e. within the matched element). • Result from the selected element/attribute: textual content with – markup removed – entity references resolved XML-8 J. Teuhola 2013 129
Example of element selection Source document : Template rule : <?xml version=”1.0”> <xsl:template match=”course”> <p> <course> <xsl:value-of <name>Adv DB</name> select=”audience”/> <teacher>Timo</teacher> </p> <audience> <student>Esa</student> </xsl:template> <student>Anu</student> </audience> Result : </course> <?xml version=”1.0” …?> <p>EsaAnu<p> XML-8 J. Teuhola 2013 130
Example of attribute handling Source document : <?xml version=”1.0”> <example type=”small” name=”attribute extraction example”> <greeting>Hello world</greeting> </example> Wanted result : <html> <head> <title>small example</title> </head> <body id=”attribute extraction example”> <h1>Hello world</h1> </body> </html> XML-8 J. Teuhola 2013 131
Solution template <xsl:template match=”example”> <html> <head> <title> <xsl:value-of select=”@type”/> example </title> </head> <body> <xsl:attribute name=”id”> <xsl:value-of select=”@name”/> </xsl:attribute> <h1><xsl:value-of select=”greeting”/></h1> </body> </html> </xsl:template> XML-8 J. Teuhola 2013 132
Processing order for matching • Default: from start to end in document order • Matching with parent is done before children; matching with children may be prevented. • Changing the order of traversal: <xsl:apply-templates select=”…”> The select attribute (optional XPath expression) tells which nodes should be processed here. • The order of declaring rules in the stylesheet does not matter; except if several rules apply, the last one is used. XML-8 J. Teuhola 2013 133
Example of apply-templates Source document : Teacher’s last & first name : <?xml version=”1.0”> <course> <xsl:template match=”tname”> <name>Adv DB</name> <xsl:value-of select=”last”/> <teacher> <xsl:value-of select=”first”/> <tname> </xsl template> <first>Timo</first> <last>Aho</last> <xsl:template match=”course”> </tname> <xsl:apply-templates </teacher> select=”teacher”/> <audience> </xsl:template> <student>Esa</student> <student>Anu</student> </audience> </course> XML-8 J. Teuhola 2013 134
Example of multiple levels <xsl:template match=”course”> <html> <head><title>Course teacher</title></head> <body> <xsl:apply-templates select=”teacher”/> </body> </html> </xsl:template> <xsl:template match=”teacher”> <xsl:apply-templates select=”tname”/> </xsl:template> <xsl:template match=”tname”> <p><xsl:value-of select=”first”> <xsl:value-of select=”last”></p> </xsl:template> XML-8 J. Teuhola 2013 135
Built-in template rules 1. For the root and other elements : <xsl:template match=”*|/”> <xsl:apply-templates/> </xsl:template> – Here ”*” is a wildcard matching any element, ”/” represents the root. – This is the first rule to be applied, unless an explicit pattern also matches. – If an explicit pattern matches, then the subtree is not processed by default – explicit rules must be written also for children, if needed. XML-8 J. Teuhola 2013 136
Built-in template rules (cont.) 2. Text and attributes : The built-in rule copies the content to the result: <xsl:template match=”text( )|@*”> <xsl:value-of select=”.”/> </xsl:template> where text() refers to the text content itself, ”@*” refers to an attribute, and ”.” to the current node. Attribute nodes must be explicitly selected for output. (Attributes not considered children of their elements!) XML-8 J. Teuhola 2013 137
Built-in template rules (cont.) 3. Comments and processing instructions : <xsl:template match= ”processing-instruction( )|comment( )”/> This means that nothing is produced to the output. 4. Namespaces : XPath does not have a pattern for namespaces. Typical default applied in the XMLT processor: No output. XML-8 J. Teuhola 2013 138
Some guidelines • It is typical to override built-in rules by starting with explicit <xsl:template match=”/”> ..... </xsl:template> • Built-in rules can be ’recovered’ by calling: <xsl:apply-templates ... > XML-8 J. Teuhola 2013 139
XSLT modes • If different rules should be applied to the same pattern in different contexts, the mode attribute can be used to discriminate the cases. • Making a connection: <xsl:template match=”teacher” mode=”header”> … <xsl:apply-templates select=”…” mode=”header”> • The apply-templates with a given mode ’calls’ only the templates with the same mode. XML-8 J. Teuhola 2013 140
Recommend
More recommend