sdpl 2003notes 5: xslt1 5. document transformations n xslt (w3c rec. november 1999) –a language...

50
SDPL 2003 Notes 5: XSLT 1 5. Document Transformations 5. Document Transformations XSLT (W3C Rec. November 1999) XSLT (W3C Rec. November 1999) A language for transforming XML documents A language for transforming XML documents » representative of tree-based transformation representative of tree-based transformation languages (DSSSL, MetaMorphosis, TranSID, …) languages (DSSSL, MetaMorphosis, TranSID, …) designed to be used designed to be used » primarily as part of XSL formatting primarily as part of XSL formatting » also as an independent transformation language also as an independent transformation language Our goal: to understand the basic model Our goal: to understand the basic model and central features of the language and central features of the language Overview and an example Overview and an example Data model and processing model Data model and processing model

Post on 18-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 1

5. Document Transformations5. Document Transformations

XSLT (W3C Rec. November 1999)XSLT (W3C Rec. November 1999)– A language for transforming XML documentsA language for transforming XML documents

» representative of tree-based transformation languages representative of tree-based transformation languages (DSSSL, MetaMorphosis, TranSID, …) (DSSSL, MetaMorphosis, TranSID, …)

– designed to be used designed to be used » primarily as part of XSL formattingprimarily as part of XSL formatting» also as an independent transformation languagealso as an independent transformation language

Our goal: to understand the basic model and Our goal: to understand the basic model and central features of the languagecentral features of the language– Overview and an exampleOverview and an example– Data model and processing modelData model and processing model

Page 2: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 2

XSLT: OverviewXSLT: Overview

XSLT uses XML syntax for expressing XSLT uses XML syntax for expressing transformationstransformations– of a document of a document source treesource tree into a into a result treeresult tree

» result and source are result and source are separateseparate trees trees

– by by template rulestemplate rules Each (ordinary) template rules hasEach (ordinary) template rules has

– aa pattern pattern (matched against nodes of the source tree) (matched against nodes of the source tree)– a a template template as a bodyas a body

» instantiated to create fragments of the result treeinstantiated to create fragments of the result tree

Page 3: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 3

Trans form ation P rocess

O utput P ro cess

X M L

T ext

H T M L

S tyleS heet

SourceDocument

Sourc e TreeR esult T ree

Overview of XSLT TransformationOverview of XSLT Transformation

Page 4: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 4

Style Sheets and Template RulesStyle Sheets and Template Rules

AnAn xsl:stylesheetxsl:stylesheet (or(or xsl:transformxsl:transform) ) consists of template rules:consists of template rules:<xsl:template match="<xsl:template match="PatternPattern">">

TemplateTemplate <!-- <!-- NB: well-formed!NB: well-formed! --> --></xsl:template></xsl:template>

Rule applied to nodes of source tree matched by Rule applied to nodes of source tree matched by the the PatternPattern – expressed using XML Path Language (XPath)expressed using XML Path Language (XPath)

TemplateTemplate consists of consists of» literal result tree fragments (elements, text), and literal result tree fragments (elements, text), and » XSLT instructions for creating further result tree fragments XSLT instructions for creating further result tree fragments

conventionally used conventionally used as namespace prefixas namespace prefix

Page 5: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 5

XPath in ShortXPath in Short

W3C Recommendation (November 1999)W3C Recommendation (November 1999)– a compact non-XML syntax for a compact non-XML syntax for addressing parts addressing parts

of XML documentsof XML documents– used also in other W3C languagesused also in other W3C languages

» SpecsSpecs for hyperlinks in XML for hyperlinks in XML: : XLink (Rec.) and XPointer (DraftXLink (Rec.) and XPointer (Draft))

» XQuery (draft, 11/2002, for querying XML)XQuery (draft, 11/2002, for querying XML)

– provides also typical primitives for manipulating provides also typical primitives for manipulating strings, numbersstrings, numbers and and truth valuestruth values

Page 6: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 6

An XSL transformation exampleAn XSL transformation example

Transform below document to HTML:Transform below document to HTML:

<?xml-stylesheet type="text/xsl" href="walsh.xsl" ?><?xml-stylesheet type="text/xsl" href="walsh.xsl" ?>

<!-- Modified from an example by Norman Walsh --> <!-- Modified from an example by Norman Walsh -->

<doc><title>My Document</title><doc><title>My Document</title>

<para>This is a <em>short</em> document.</para><para>This is a <em>short</em> document.</para>

<para>It only exists to <em>demonstrate a <para>It only exists to <em>demonstrate a

<em>simple</em> XML document</em>.</para><em>simple</em> XML document</em>.</para>

<figure><title>My Figure</title> <figure><title>My Figure</title>

<graphic fileref="myfig.jpg"/></figure><graphic fileref="myfig.jpg"/></figure>

</doc></doc>

Page 7: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 7

Example style sheet beginsExample style sheet begins

<xsl:stylesheet version=<xsl:stylesheet version="1.0""1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="<xsl:template match="//"> <!-- rule for root -->"> <!-- rule for root --> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <HTML><HEAD><TITLE>A Document</TITLE></HEAD> <BODY><BODY> <!-- process root's children here: --><!-- process root's children here: -->

<xsl:apply-templates /> <xsl:apply-templates /> </BODY></BODY> </HTML></HTML> </xsl:template></xsl:template>

<xsl:template match="<xsl:template match="doc/titledoc/title"> "> <H1><H1><xsl:apply-templates /><xsl:apply-templates /></H1></H1>

</xsl:template></xsl:template>

Page 8: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 8

Example (Example (paraparass and and emphemphs)s)

<xsl:template match="<xsl:template match="parapara"> "> <P><P><xsl:apply-templates /><xsl:apply-templates /></P></P>

</xsl:template> </xsl:template>

<xsl:template match="<xsl:template match="emem"> "> <I><I><xsl:apply-templates /><xsl:apply-templates /></I></I>

</xsl:template> </xsl:template>

<xsl:template match="<xsl:template match="em/emem/em"> "> <B><B><xsl:apply-templates /><xsl:apply-templates /></B></B>

</xsl:template></xsl:template>

Page 9: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 9

Example (Example (figurefigures)s)

<xsl:template match="<xsl:template match="figurefigure"> "> <!-- Insert a bold caption of form '<!-- Insert a bold caption of form 'Figure Num.Figure Num. ' '

by counting all figures in the document: --> by counting all figures in the document: --><DIV><B>Figure <DIV><B>Figure <xsl:number level="any" <xsl:number level="any"

count="figure"/>count="figure"/>. </B> . </B> <BR /> <BR /> <!-- Process the children of figure, --><!-- Process the children of figure, --><!-- the 'graphic' child first: --><!-- the 'graphic' child first: --><xsl:apply-templates select="<xsl:apply-templates select="graphicgraphic" /> " />

<!-- then the 'title' child: --><!-- then the 'title' child: --><xsl:apply-templates select="<xsl:apply-templates select="titletitle" /> " />

</DIV></DIV>

</xsl:template> </xsl:template>

Page 10: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 10

Example (end of style sheet)Example (end of style sheet)

<xsl:template match="<xsl:template match="graphicgraphic"> "> <IMG src="<IMG src="{{@fileref@fileref}}" />" /> <!-- Assign the value of attribute <!-- Assign the value of attribute

'fileref' to attribute 'src' -->'fileref' to attribute 'src' --> </xsl:template> </xsl:template> <xsl:template match="<xsl:template match="figure/titlefigure/title"> ">

<B> <B> <xsl:apply-templates /><xsl:apply-templates /> </B> </B> </xsl:template></xsl:template>

</xsl:stylesheet></xsl:stylesheet>

Page 11: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 11

Result (edited for readability)Result (edited for readability)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">Transitional//EN">

<HTML><HEAD><TITLE>A Document</TITLE></HEAD><HTML><HEAD><TITLE>A Document</TITLE></HEAD>

<BODY> <H1><BODY> <H1>My DocumentMy Document</H1></H1>

<P><P>This is a This is a <I><I>shortshort</I></I> document. document.</P></P>

<P><P>It only exists to It only exists to <I><I>demonstrate a demonstrate a <B><B>simplesimple</B></B> XML XML documentdocument</I></I>..</P></P>

<DIV><DIV>

<B>Figure <B>Figure 11. </B> <BR>. </B> <BR>

<IMG src<IMG src="myfig.jpg"="myfig.jpg"><B>><B>My FigureMy Figure</B></B>

</DIV></DIV>

</BODY></BODY>

</HTML></HTML>

Page 12: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 12

What use of XSL or XSLT?What use of XSL or XSLT?

XSL can be used in different waysXSL can be used in different ways– for offline document formattingfor offline document formatting

» produce, say, PDF from XML by an XSL style sheet produce, say, PDF from XML by an XSL style sheet (using XSLT + XSL formatting objects)(using XSLT + XSL formatting objects)

– for offline document manipulationfor offline document manipulation» transform XML into other form (XML/HTML/text) transform XML into other form (XML/HTML/text)

using XSLTusing XSLT

– for online document deliveryfor online document delivery» in a Web serverin a Web server» in a Web browser (if the browser supports)in a Web browser (if the browser supports)

Page 13: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 13

XSLT in online document deliveryXSLT in online document delivery

XSLT in a browserXSLT in a browser– defines rendering of XML documentsdefines rendering of XML documents– approach of Microsoft Internet Explorer 5approach of Microsoft Internet Explorer 5

» transformation of XML to HTML on the fly in browsertransformation of XML to HTML on the fly in browser» NBNB: Microsoft's XSLT implementation differs from XSLT 1.0: Microsoft's XSLT implementation differs from XSLT 1.0

(a new one exists but has to be installed separately)(a new one exists but has to be installed separately)

XSLT in a Web serverXSLT in a Web server– an HTTP request for an XML document served by an HTTP request for an XML document served by

transforming XML on the fly to HTML (or other format) transforming XML on the fly to HTML (or other format) on the serveron the server

Page 14: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 14

Main Aspects of XSLTMain Aspects of XSLT

Data modelData model– How document data is viewed in XSLT?How document data is viewed in XSLT?

Selection mechanismSelection mechanism– How document parts are selected for processing?How document parts are selected for processing?

MatchingMatching– How are the template rules selected?How are the template rules selected?

Processing modelProcessing model– How XSLT execution proceeds?How XSLT execution proceeds?

Page 15: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 15

Data Model of XSLT and XPathData Model of XSLT and XPath

Documents viewed as abstract tree structuresDocuments viewed as abstract tree structures Seven types of tree nodesSeven types of tree nodes

– rootroot (additional parent of document element) (additional parent of document element)– elementelement nodes nodes– attributeattribute nodes nodes– texttext nodes nodes– commentscomments, , processing instructionsprocessing instructions and and

namespacesnamespaces NBNB: Entity references are expanded: Entity references are expanded

no entity nodes no entity nodes

Page 16: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 16

XSLT/XPath document treesXSLT/XPath document trees

Element nodes have elements, text nodes, Element nodes have elements, text nodes, comments and processing instructions of their comments and processing instructions of their (direct) (direct) contentcontent as their children as their children– NB: attribute nodes are not considered childrenNB: attribute nodes are not considered children– the the valuevalue of an element node is the concatenation of its of an element node is the concatenation of its

text-node descendantstext-node descendants Nodes have a complete Nodes have a complete document orderdocument order

– root node first, otherwise according to the order of the root node first, otherwise according to the order of the first character of the XML markup for each nodefirst character of the XML markup for each node

– > element node precedes it's attribute nodes, which > element node precedes it's attribute nodes, which precede any content nodes of the elementprecede any content nodes of the element

Page 17: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 17

XSLT/XPath TreesXSLT/XPath Trees

Similar to the DOM structure model, with slight Similar to the DOM structure model, with slight differencesdifferences– value of an element: its full textual content value of an element: its full textual content

(In DOM: (In DOM: nullnull))– no names for text nodes, comment nodes, etc.no names for text nodes, comment nodes, etc.

(In DOM: (In DOM: "#text""#text", , "#comment""#comment", etc. , etc.

Example document:Example document:<article>Written by <fig file="pekka.jpg" <article>Written by <fig file="pekka.jpg"

caption="The caption="The Lecturer"/> Lecturer"/> the lecturer.</article>the lecturer.</article>

Page 18: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 18

XSLT/XPath trees: ExampleXSLT/XPath trees: Example

"W ritte n b y the le c ture r."

"W ritte n b y the le c ture r."

root

""

elem ent

"artic le "

text

""

"W ritte n b y "

elem ent

"f ig "

""

text

""

" the le c ture r."

attribute attribute

"cap tio n" "f ile "

"The Lec turer" "p e kka.jp g "

Legend: type

value

nam e

<article>Written by <article>Written by <fig <fig file="pekka.jpg" file="pekka.jpg"

caption="The caption="The Lecturer” /> Lecturer” />

the lecturer.the lecturer.</article></article>

33rdrd

55thth or 6 or 6thth

11stst

55thth or 6 or 6thth

77thth44thth

22ndnd

Page 19: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 19

Main Aspects of XSLTMain Aspects of XSLT

Data modelData model Selection mechanismSelection mechanism

– How document parts are selected for How document parts are selected for processing?processing?

– A: With XPath expressionsA: With XPath expressions MatchingMatching Processing modelProcessing model

Page 20: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 20

XPath ExpressionsXPath Expressions

Used for selecting source tree nodes, conditional Used for selecting source tree nodes, conditional processing, and generating new text contentprocessing, and generating new text content– return return node-sets, truth values, numbers node-sets, truth values, numbers oror strings strings– can select any parts of source tree (can select any parts of source tree (node-setnode-set) for ) for

processing, using … processing, using … Location pathsLocation paths

– the most characteristic of XPath expressionsthe most characteristic of XPath expressions– evaluated with respect to a evaluated with respect to a context nodecontext node

» often the often the current nodecurrent node matched by the template pattern matched by the template pattern

– resultresult: set of nodes selected by the location path: set of nodes selected by the location path

Page 21: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 21

Location pathsLocation paths

Consist of Consist of location stepslocation steps separated by ' separated by '//''– each step produces a set of nodeseach step produces a set of nodes– steps evaluated left-to-right, steps evaluated left-to-right,

each node in turn acting as a context nodeeach node in turn acting as a context node» path begins with ‘path begins with ‘//’ ’ root is the first context node root is the first context node

Complete form of a location step: Complete form of a location step: AxisNameAxisName:::: NodeTestNodeTest ( ([[PredicateExprPredicateExpr]])*)*

– axisaxis specifies the tree relationship between the context specifies the tree relationship between the context node and the selected nodes node and the selected nodes

– node testnode test restricts the type and and name of nodes restricts the type and and name of nodes– further filtered by 0 or more further filtered by 0 or more predicatespredicates

Page 22: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 22

Location steps: AxesLocation steps: Axes

In total 13 axesIn total 13 axes– for staying at the context node:for staying at the context node:

» selfself

– for going downwards:for going downwards:» childchild, , descendantdescendant, , descendant-or-selfdescendant-or-self

– for going upwards:for going upwards:» parentparent, , ancestorancestor, , ancestor-or-selfancestor-or-self

– for moving towards beginning or end of the for moving towards beginning or end of the document:document:» preceding-siblingpreceding-sibling, , following-siblingfollowing-sibling, , precedingpreceding, , followingfollowing

– ““Special” axesSpecial” axes» attributeattribute, , namespacenamespace

Page 23: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 23

XPath Axes and Their OrientationXPath Axes and Their Orientation

Axes are oriented away from the context nodeAxes are oriented away from the context node(except (except attribute attribute and and namespace namespace axes, which are axes, which are unordered sets)unordered sets)– the the position() position() for the closest node for the closest node = 1 = 1– for the most remote node, for the most remote node, position() = last()position() = last()

1

C o nte xtno de :

The simplest axis,The simplest axis, self: self:

Page 24: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 24

XPath Axes and Their OrientationXPath Axes and Their Orientation

parent:parent:

1 C ontextnode :

Page 25: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 25

XPath Axes and Their OrientationXPath Axes and Their Orientation

ancestor:ancestor: 2

1

ancestor-or-self:ancestor-or-self:3

2

1

Page 26: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 26

XPath Axes and Their OrientationXPath Axes and Their Orientation

child:child:

431 2

C ontextnode :

Page 27: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 27

XPath Axes and Their OrientationXPath Axes and Their Orientation

descendant:descendant:

871 4

2 3 65 9

descendant-or-self:descendant-or-self: 1

982 5

3 4 76 1 0

Page 28: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 28

XPath Axes and Their OrientationXPath Axes and Their Orientation

preceding-sibling:preceding-sibling:

2 1

following-sibling:following-sibling:

21

Page 29: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 29

XPath Axes and Their OrientationXPath Axes and Their Orientation

following:following:

21

3

4

5

preceding:preceding:

3

2 1

Page 30: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 30

Location paths: Node testsLocation paths: Node tests

Node tests (slightly simplified)Node tests (slightly simplified)– NameName: any : any elementelement node with name node with name NameName

(on an attribute axis, any attribute node with name (on an attribute axis, any attribute node with name NameName))

– **: any element node : any element node (on an attribute axis, any attribute node)(on an attribute axis, any attribute node)

– text()text(): any text node: any text node» comment()comment(): any comment node: any comment node» processing-instruction()processing-instruction(): :

any processing instruction nodeany processing instruction node

– node()node(): any node of any type: any node of any type

Page 31: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 31

Location paths: AbbreviationsLocation paths: Abbreviations

Abbreviations in location stepsAbbreviations in location steps– 'child::''child::' can be omitted can be omitted– 'attribute::''attribute::' can be shortened to can be shortened to ''@@''– 'self::node()''self::node()' can be shortened to can be shortened to ''..'' (period)(period)– 'parent::node()''parent::node()' can be shortened to can be shortened to ''....''

– Predicate 'Predicate '[position()=[position()=nn]]' for testing occurrence ' for testing occurrence position position nn can be shortened to' can be shortened to' [ [nn]]''

– ''/descendant-or-self::node()/'/descendant-or-self::node()/' shortened to shortened to ''////''

Syntax resembles slightly Linux/Unix file path namesSyntax resembles slightly Linux/Unix file path names

Page 32: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 32

Semantics of Location Paths (example)Semantics of Location Paths (example)

{2, 5, 7}{2, 5, 7}

AA

BB

context nodecontext node

CC"txt""txt"AA BB

BBCC55

884433

22

11

66

77

*/node()/parent::B[child::A]*/node()/parent::B[child::A]

{3, 4, 6, 8}{3, 4, 6, 8} {2, 5, 7}{2, 5, 7}

final value: {2}final value: {2}

value after value after each step:each step:

Page 33: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 33

Location path examples (1)Location path examples (1)

chapchap children of current node:children of current node: ./chap./chap (or equivalently:(or equivalently: chapchap, or, or ./child::node()[name()='chap']./child::node()[name()='chap']))

The document element The document element (child element of root node):(child element of root node): /*/*

Elements Elements chapter chapter anywhere (below the root): anywhere (below the root): //chapter //chapter (( .//chapter .//chapter anywhere below anywhere below

the current node)the current node) All All chapterchapters with attributes with attribute type="intro"type="intro": :

//chapter[@type='intro']//chapter[@type='intro'] the previousthe previous chapter chapter siblingsibling: :

preceding-sibling::chapter[1]preceding-sibling::chapter[1]

Page 34: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 34

Location path examples (2)Location path examples (2)

All child elements having an attributeAll child elements having an attribute typetype: : *[@type]*[@type]

NBNB: use of node sets as truth values: : use of node sets as truth values: empty - empty - falsefalse; non-empty - ; non-empty - truetrue

All child elements of anyAll child elements of any author author child:child: author/*author/*

sectsections whoseions whose type type attribute equalsattribute equals style style attribute of the document element:attribute of the document element:

//sect[@type = /*/@style]//sect[@type = /*/@style] First First authorauthor child, and previous to the last: child, and previous to the last:

author[1], author[last()-1]author[1], author[last()-1]

Page 35: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 35

Location path examples (3)Location path examples (3)

Predicate expressions can be Boolean Predicate expressions can be Boolean combinations:combinations:– child elements child elements authorauthor with a with a publpubl child, but without child, but without degreedegree or or awardaward children:children:

author[publ and not(degree or award)]author[publ and not(degree or award)] Closest Closest chapchap ancestor (and highest):ancestor (and highest):

ancestor::chap[1] ancestor::chap[1] (ancestor::chap[last()]) (ancestor::chap[last()])

authorauthor of the highestof the highest sectsect ancestor that is ancestor that is contained in ancontained in an appappendix:endix: ancestor::sect[ancestor::app][last()]/authorancestor::sect[ancestor::app][last()]/author

Page 36: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 36

Main Aspects of XSLTMain Aspects of XSLT

Data modelData model Selection mechanismSelection mechanism MatchingMatching

– How are the rules selected?How are the rules selected?– A: With A: With PatternsPatterns

Processing modelProcessing model

Page 37: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 37

XSLT PatternsXSLT Patterns

Main use in match attributes of template rules:Main use in match attributes of template rules:<xsl:template match="<xsl:template match="PatternPattern">">

Restricted location path expressions:Restricted location path expressions:– with steps using onlywith steps using only childchild and and attributeattribute axes and axes and

separated byseparated by ' '////' ' or or ''//''» but arbitrary predicate expressions but arbitrary predicate expressions [[ExprExpr]]allowedallowed

– may begin withmay begin with id(id(''IdValIdVal''))(for selecting element nodes by ID attribute values)(for selecting element nodes by ID attribute values)

– alternative patterns separated by 'alternative patterns separated by '||' ' (node-set union (node-set union operator)operator)

Page 38: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 38

XSLT Patterns: SemanticsXSLT Patterns: Semantics

A location path pattern A location path pattern PP is of form is of form StepStep11 StepStep22 …… StepStepnn-1 -1 StepStepn n ,,

where each separator where each separator is either ` is either `//´ or `´ or `////´ ´ – may also begin with ‘may also begin with ‘//’’; ; Pattern Pattern ‘‘//’’ matchesmatches only the root only the root

ElseElse P P matches a nodematches a node vvnn iff there are nodes iff there are nodes vvnn,..., ,..., vv11 such such

that each that each vvii satisfies the node test and possible predicates satisfies the node test and possible predicates

of of StepStepi i , and which form a path towards the root:, and which form a path towards the root:

– If If P P begins with a single ‘begins with a single ‘//’, node ’, node vv11 must be child of the must be child of the

rootroot

– in case of in case of StepStepii-1-1//StepStepi i node node vvi-1 i-1 must be parent of must be parent of vvii

– in case of in case of StepStepii-1-1////StepStepi i node node vvi-1i-1 may be any ancestor of may be any ancestor of vvii

Page 39: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 39

XSLT Patterns: ExamplesXSLT Patterns: Examples

match="sect-head | section/head"match="sect-head | section/head"– matches any element with namematches any element with name sect-headsect-head, and any, and any headhead elements directly below aelements directly below a sectionsection

A node matches the patternA node matches the pattern /appendix//ulist/item[1] /appendix//ulist/item[1]

if it is the first if it is the first itemitem element in aelement in a ulistulist element which is element which is contained in ancontained in an appendixappendix, which is the document , which is the document elementelement

Page 40: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 40

Main Aspects of XSLTMain Aspects of XSLT

Data modelData model Selection mechanismSelection mechanism MatchingMatching Processing modelProcessing model

– How does the XSLT execution proceed?How does the XSLT execution proceed?

Page 41: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 41

XSLT Processing ModelXSLT Processing Model

0. Parse the document into a 0. Parse the document into a source treesource tree

1. Construct the 1. Construct the result treeresult tree by applying by applying template rulestemplate rules to the source tree to the source tree» may contain text and arbitrary elements (including may contain text and arbitrary elements (including

XSL FO formatting objects)XSL FO formatting objects)

2. Serialize the result tree (as XML, HTML or 2. Serialize the result tree (as XML, HTML or text), or interpret it to produce formatted output text), or interpret it to produce formatted output (in case of full XSL processing)(in case of full XSL processing)

Page 42: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 42

Result Tree Construction AlgorithmResult Tree Construction Algorithm

Start at the source tree root; Start at the source tree root;

whilewhile (unprocessed source nodes left) (unprocessed source nodes left) dodo

Find matching template rules; Find matching template rules; Choose one rule (the one with highest Choose one rule (the one with highest prioritypriority););

Instantiate rule template in the context of the current Instantiate rule template in the context of the current node; Add the result to the result tree;node; Add the result to the result tree;

Recursively process any source nodes selected by XSLT Recursively process any source nodes selected by XSLT instructions (instructions (apply-templates, for-eachapply-templates, for-each) in the ) in the template;template;

endwhileendwhile;;

Page 43: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 43

Selecting among multiple matching rulesSelecting among multiple matching rules

Priority of a rule can be specified explicitly: Priority of a rule can be specified explicitly: <xsl:template priority="2.0"><xsl:template priority="2.0"> … …

Default priorities based on the Default priorities based on the matchmatch pattern: pattern:– 0 for simple name tests 0 for simple name tests (like (like para, @hrefpara, @href))– negative for less specific patterns negative for less specific patterns

e.g., e.g., *, @*, node()*, @*, node()– 0.5 for more complex patterns0.5 for more complex patterns

Multiple matching rules with the same maximum Multiple matching rules with the same maximum priority is an error - Processor may recover by priority is an error - Processor may recover by (quietly!) choosing the (quietly!) choosing the last onelast one of those rules of those rules

Page 44: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 44

Application of template rulesApplication of template rules

Without a Without a selectselect attribute attribute <xsl:apply-templates /><xsl:apply-templates />

processes all children of current nodeprocesses all children of current node– > > Default behaviour: top-down traversal of source treeDefault behaviour: top-down traversal of source tree

Otherwise theOtherwise the selectselected nodes are processed ed nodes are processed – in in document order document order (if not sorted with (if not sorted with xsl:sortxsl:sort))

Built-in rulesBuilt-in rules allow recursive traversal to proceed allow recursive traversal to proceed gracefully in the absence of matching rules gracefully in the absence of matching rules

Page 45: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 45

Default rules for elements and contentDefault rules for elements and content

Built-in rule for the root and element nodes:Built-in rule for the root and element nodes: <xsl:template match="/ | *"><xsl:template match="/ | *">

<xsl:apply-templates /><xsl:apply-templates /> </xsl:template</xsl:template>>

Built-in rule for text and attribute nodes:Built-in rule for text and attribute nodes: <xsl:template match="text() | @*"><xsl:template match="text() | @*"><!-- Insert the string value <!-- Insert the string value

of current node: -->of current node: --> <xsl:<xsl:value-ofvalue-of select="." /> select="." />

</xsl:template</xsl:template>> Low priorityLow priority can be overriddencan be overridden

Page 46: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 46

A (Tricky) Processing Example A (Tricky) Processing Example

Consider transforming documentConsider transforming document<A><A><B>b1</B><C>cc<B>b2</B></C><D>dd</D><B>b3</B><B>b1</B><C>cc<B>b2</B></C><D>dd</D><B>b3</B></A></A>

with the below rules:with the below rules:<xsl:template match="/"> <!-- Rule 1 --><xsl:template match="/"> <!-- Rule 1 -->

<R><R><xsl:<xsl:apply-templatesapply-templates select="//C" /> select="//C" /></R></R></xsl:template></xsl:template>

<xsl:template match="C"> <!-- Rule 2 --><xsl:template match="C"> <!-- Rule 2 --><NewC>New:<NewC>New: <xsl: <xsl:apply-templatesapply-templates select="../B" /> select="../B" />

<xsl: <xsl:apply-templatesapply-templates /> /> </NewC></NewC>

</xsl:template></xsl:template>

Page 47: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 47

Processing example (2)Processing example (2)

The resultThe result<R><NewC><R><NewC>New:New: b1b3ccb2</NewC></R> b1b3ccb2</NewC></R>

is obtained as follows: is obtained as follows: 1. 1. Rule 1 matches the root node Rule 1 matches the root node Element node Element node RR is added is added

to the result; Instruction to the result; Instruction <xsl:apply-templates <xsl:apply-templates

select="//C" />select="//C" /> selects the (only)selects the (only) CC element for element for processing (which will produce the contents of node processing (which will produce the contents of node RR). ).

2. 2. Rule 2 with patternRule 2 with pattern "C" "C" creates into result tree acreates into result tree a NewC NewC element node with text node element node with text node ""New:New: " " as its first child.as its first child.

Page 48: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 48

Processing example (3)Processing example (3)

3. Instruction 3. Instruction <xsl:apply-templates select="../B"/><xsl:apply-templates select="../B"/> selects element selects element BB siblings of current node (siblings of current node (CC). The built-in ). The built-in element rule applies to these, and the built-in text rule to element rule applies to these, and the built-in text rule to their children. their children. Result: text nodesResult: text nodes " "b1b1" " and and ""b3b3" " become the next become the next children ofchildren of NewBNewB..

4. Instruction 4. Instruction <xsl:apply-templates /><xsl:apply-templates /> in the context of in the context of element node element node CC selects its children, selects its children, ""cc"cc" and and <B>b2</B><B>b2</B>, for processing. The built-in text rule inserts , for processing. The built-in text rule inserts value value ""cc"cc" to the result tree, and the to the result tree, and the BB element element node node becomesbecomes "b2""b2" in the result (similarly to step 3). in the result (similarly to step 3).

Page 49: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 49

Processing example (4)Processing example (4)

AA

BB BB

BB

CC DD

b1b1

SourceSource ResultResult

cccc

b2b2

dddd

RR

NewCNewC

New: New: b3b3

1.1.2.2.

3.3.

4.4.

b3b3b1b1 cccc b2b2

Page 50: SDPL 2003Notes 5: XSLT1 5. Document Transformations n XSLT (W3C Rec. November 1999) –A language for transforming XML documents »representative of tree-based

SDPL 2003 Notes 5: XSLT 50

Is it Really So Tricky?Is it Really So Tricky?

In practice: SeldomIn practice: Seldom– A computer scientist wants to understand how a model A computer scientist wants to understand how a model

really works … really works …

XSLT is high-level declarative language for XSLT is high-level declarative language for describing transformationsdescribing transformations– Normally no need to think so carefully about execution; Normally no need to think so carefully about execution;

Often sufficient just to specify declarative rules to Often sufficient just to specify declarative rules to handle different cases, likehandle different cases, like

<xsl:template match="<xsl:template match="parapara"> "> <P><P><xsl:apply-templates /><xsl:apply-templates /></P></P>

</xsl:template> </xsl:template>