8. XSL Transformations (XSLT) XSL = Extensible Stylesheet Language - - PowerPoint PPT Presentation

8 xsl transformations xslt
SMART_READER_LITE
LIVE PREVIEW

8. XSL Transformations (XSLT) XSL = Extensible Stylesheet Language - - PowerPoint PPT Presentation

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,


slide-1
SLIDE 1

XML-8 J. Teuhola 2013 121

  • 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)

slide-2
SLIDE 2

XML-8 J. Teuhola 2013 122

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.

slide-3
SLIDE 3

XML-8 J. Teuhola 2013 123

Illustration of the basic idea

XML source document XSL style- sheet XSLT processor Result of transform

slide-4
SLIDE 4

XML-8 J. Teuhola 2013 124

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)

slide-5
SLIDE 5

XML-8 J. Teuhola 2013 125

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=”...”?>

slide-6
SLIDE 6

XML-8 J. Teuhola 2013 126

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.

slide-7
SLIDE 7

XML-8 J. Teuhola 2013 127

Simple example (no explicit extraction)

Source document: <?xml version=”1.0”> <course> <name>Adv DB</name> <teacher>Timo</teacher> <audience> <student>Esa</student> <student>Anu</student> </audience> </course> Stylesheet: <?xml version=”1.0”> <xsl:stylesheet version= ”1.0” xmlns=”http://...”> <xsl:template match=”student”> Student name </xsl:template> </xsl:stylesheet>

slide-8
SLIDE 8

XML-8 J. Teuhola 2013 128

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.

slide-9
SLIDE 9

XML-8 J. Teuhola 2013 129

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

slide-10
SLIDE 10

XML-8 J. Teuhola 2013 130

Example of element selection

Source document: <?xml version=”1.0”> <course> <name>Adv DB</name> <teacher>Timo</teacher> <audience> <student>Esa</student> <student>Anu</student> </audience> </course> Template rule: <xsl:template match=”course”> <p> <xsl:value-of select=”audience”/> </p> </xsl:template> Result: <?xml version=”1.0” …?> <p>EsaAnu<p>

slide-11
SLIDE 11

XML-8 J. Teuhola 2013 131

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>

slide-12
SLIDE 12

XML-8 J. Teuhola 2013 132

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>

slide-13
SLIDE 13

XML-8 J. Teuhola 2013 133

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.

slide-14
SLIDE 14

XML-8 J. Teuhola 2013 134

Example of apply-templates

Source document: <?xml version=”1.0”> <course> <name>Adv DB</name> <teacher> <tname> <first>Timo</first> <last>Aho</last> </tname> </teacher> <audience> <student>Esa</student> <student>Anu</student> </audience> </course> Teacher’s last & first name: <xsl:template match=”tname”> <xsl:value-of select=”last”/> <xsl:value-of select=”first”/> </xsl template> <xsl:template match=”course”> <xsl:apply-templates select=”teacher”/> </xsl:template>

slide-15
SLIDE 15

XML-8 J. Teuhola 2013 135

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>

slide-16
SLIDE 16

XML-8 J. Teuhola 2013 136

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.

slide-17
SLIDE 17

XML-8 J. Teuhola 2013 137

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!)

slide-18
SLIDE 18

XML-8 J. Teuhola 2013 138

Built-in template rules (cont.)

  • 3. Comments and processing instructions:

<xsl:template match= ”processing-instruction( )|comment( )”/> This means that nothing is produced to the

  • utput.
  • 4. Namespaces:

XPath does not have a pattern for namespaces. Typical default applied in the XMLT processor: No output.

slide-19
SLIDE 19

XML-8 J. Teuhola 2013 139

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 ... >

slide-20
SLIDE 20

XML-8 J. Teuhola 2013 140

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’
  • nly the templates with the same mode.
slide-21
SLIDE 21

XML-8 J. Teuhola 2013 141

XSLT modes (cont.)

  • The XSLT processor adds default rules for the

root and elements in the specified mode:

<xsl:template match=”*|/” mode=”header”> <xsl:apply-templates mode=”header”> </xsl:template>

slide-22
SLIDE 22

XML-8 J. Teuhola 2013 142

Templates for attribute values

  • How to copy source element contents to result attribute

values?

  • Solutions in XMLT:

1) Attribute value template: <xsl:template match=”…”> <target-elem attr=”{source-elem}”/> <xsl:template> where the processor replaces {source-elem} by the value of that element in the source document.

slide-23
SLIDE 23

XML-8 J. Teuhola 2013 143

Templates for attribute values (cont.)

2) Explicit xsl:attribute declaration within the target element: <xsl:template match=“…“> <target-elem> <xsl:attribute name=“attr"> <xsl:value-of select=“source-elem"/> </xsl:attribute> </target-elem> </xsl-template>

slide-24
SLIDE 24

XML-8 J. Teuhola 2013 144

About namespaces

  • If namespaces are used, the namespace URI is used in

matching, not the prefix: <xsl:template match=”it:teacher”> matches with <cs:teacher …> if both ”it” and ”cs” are attached to the same URI.

  • Note. A default namespace does not match with ’no-

namespace’, although neither uses prefixes.

slide-25
SLIDE 25

XML-8 J. Teuhola 2013 145

Repeated application of rules

Source document:

<?xml version=”1.0”?> <course> <student> <firstName>Antti</firstName> <lastName>Aho</lastName> <startYear>2001</startYear> </student> <student> <firstName>Esko</firstName> <lastName>Elo</lastName> <startYear>2002</startYear> </student> …

slide-26
SLIDE 26

XML-8 J. Teuhola 2013 146

Repeated application of rules (cont.)

Transformation into an HTML table:

<xsl:template match=”/”> <html><body> <table border=”1”> <tr><th>First name</th> <th>Last name</th> <th>Starting year</th></tr> <xsl:for-each select=”course/student”> <tr><td><xsl:value-of select=”firstName”/></td> <td><xsl:value-of select=”lastName”/></td> <td><xsl:value-of select=”startYear”/></td></tr> </xsl:for-each> </table></body></html> </xsl:template>

slide-27
SLIDE 27

XML-8 J. Teuhola 2013 147

Conditional rules

  • One branch:

<xsl:if test=”…”> where ”…” is a boolean XPath expression

  • Two or more branches:

<xsl:choose> <xsl:when test=”…” … </xsl:when> <xsl:when test=”…” … </xsl:when> … <xsl:otherwise> … </xsl:otherwise> </xsl:choose>

slide-28
SLIDE 28

XML-8 J. Teuhola 2013 148

Example of a conditional rule

  • Addition to the previous example: Students have

an optional element: telephone number:

… <th>Telephone</th> … <td> <xsl:choose> <xsl:when test=”telNo”> <xsl:value-of select=”telNo”/> </xsl:when> <xsl:otherwise> - </xsl:otherwise> </xsl:choose> </td>

slide-29
SLIDE 29

XML-8 J. Teuhola 2013 149

About handling of whitespace

  • Hard to control; usually not relevant either.
  • Keeping whitespace of source elements (top-level decl.):

<xsl:preserve-space elements=”name1 name2 ...”/>

  • Keeping/dropping whitespace in the created element:

<new-elem xml:space=”preserve” ...> <new-elem xml:space=”collapse” ...>

  • Creating plain text, including whitespace:

<xsl:text> ... </xsl:text>

slide-30
SLIDE 30

XML-8 J. Teuhola 2013 150

Some other XSLT elements

  • <xsl:call-template name=”...”>

invokes a template by name (recursion possible)

  • <xsl:copy-of select=”expression”>

copies the content of the expression to output.

  • <xsl:element name=”...” namespace=”...”>

inserts the element into the result tree.

  • <xsl:import href=”URI” imports an xsl stylesheet to be

used together with the local stylesheet.

  • <xsl:number ...> inserts a formatted integer.
  • <xsl:output ...> output format (e.g. HTML)
  • ... and several others, see documentation