53
SDPL 2011 4: Overview of XSLT 1 4 Document Transformations 4 Document Transformations XSLT (1.0 W3C Rec, Nov 1999; XSLT (1.0 W3C Rec, Nov 1999; XSLT 2.0 Rec, Jan 2007) XSLT 2.0 Rec, Jan 2007) A language for transforming XML documents A language for transforming XML documents initial main purpose to support XSL initial main purpose to support XSL formatting formatting used mainly for independent transformations used mainly for independent transformations (esp. XML (esp. XML HTML) HTML) Our goal: to understand the basic model Our goal: to understand the basic model and central features of XSLT and central features of XSLT Overview and an example Overview and an example Data model and processing model Data model and processing model

4 Document Transformations

  • Upload
    ozzy

  • View
    48

  • Download
    0

Embed Size (px)

DESCRIPTION

4 Document Transformations. XSLT (1.0 W3C Rec , Nov 1999; XSLT 2.0 Rec , Jan 2007) A language for transforming XML documents initial main purpose to support XSL formatting used mainly for independent transformations (esp. XML → HTML) - PowerPoint PPT Presentation

Citation preview

Page 1: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 1

4 Document Transformations4 Document Transformations

XSLT (1.0 W3C Rec, Nov 1999; XSLT (1.0 W3C Rec, Nov 1999; XSLT 2.0 Rec, Jan 2007) XSLT 2.0 Rec, Jan 2007)

– A language for transforming XML documentsA language for transforming XML documents– initial main purpose to support XSL formattinginitial main purpose to support XSL formatting– used mainly for independent transformationsused mainly for independent transformations

(esp. XML (esp. XML →→ HTML) HTML) Our goal: to understand the basic model and Our goal: to understand the basic model and

central features of XSLTcentral features of XSLT– Overview and an exampleOverview and an example– Data model and processing modelData model and processing model

Page 2: 4 Document Transformations

SDPL 2011 4: Overview of 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 template rule hasEach template rule has

– aa patternpattern (matched against nodes of the source tree) (matched against nodes of the source tree)– a a templatetemplate as a bodyas a body

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

Page 3: 4 Document Transformations

SDPL 2011 4: Overview of 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: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 4

Style Sheets and Template RulesStyle Sheets and Template Rules

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

TemplateTemplate <!--<!-- well-formed XML!well-formed XML! -->--></xsl:</xsl:templatetemplate>>

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

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

conventional XSLTconventional XSLTnamespace prefixnamespace prefix

Page 5: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 5

XPath in a NutshellXPath in a Nutshell

XPath 1.0 W3C Rec. 11/99 (2.0 Rec. 1/07)XPath 1.0 W3C Rec. 11/99 (2.0 Rec. 1/07)– a compact non-XML syntax for a compact non-XML syntax for addressing parts of addressing parts of

XML documents XML documents as as node-setsnode-sets (( sequencessequences in 2.0) in 2.0)– used also in other W3C languagesused also in other W3C languages

» Specs for hyperlinks in XML: Specs for hyperlinks in XML: XLink (Rec. '01) and XPointer (Rec. '03)XLink (Rec. '01) and XPointer (Rec. '03)

» XQuery (Rec. 1/07; extends XPath 2.0)XQuery (Rec. 1/07; extends XPath 2.0)

– also typical operations on also typical operations on strings, numbersstrings, numbers and and truth truth valuesvalues

Page 6: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 6

Early impressions of XSLT 2.0Early impressions of XSLT 2.0

Somewhat more advanced & flexible than 1.0Somewhat more advanced & flexible than 1.0 Complexity of 1.0 vs. 2.0 specs (as pages):Complexity of 1.0 vs. 2.0 specs (as pages):

Data ModelData Model ~ 80 ~ 80

XPath 2.0XPath 2.0 ~100~100

Funcs & opersFuncs & opers ~160~160

XSLT 2.0XSLT 2.0 ~280~280--------------------------------------------------------------------

~620~620

XPath 1.0 ~ 30XPath 1.0 ~ 30

XSLT 1.0 ~ 90XSLT 1.0 ~ 90------------------------------------------------TotalTotal ~ 120 ~ 120

Page 7: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 7

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 8: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 8

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 9: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 9

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="/"> <!-- rule for root --><xsl:template match="/"> <!-- 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="doc/title"> <xsl:template match="doc/title"> <H1><H1><xsl:apply-templates /><xsl:apply-templates /></H1></H1>

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

Page 10: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 10

Example (Example (paraparass and and emphemphs)s)

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

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

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

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

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

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

Page 11: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 11

Example (Example (figurefigures)s)

<xsl:template match="figure"> <xsl:template match="figure"> <!-- Insert a bold caption of form 'Figure Num. '<!-- Insert a bold caption of form '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="graphic" /> <xsl:apply-templates select="graphic" />

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

</DIV></DIV>

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

Page 12: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 12

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

<xsl:template match="graphic"> <xsl:template match="graphic"> <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="figure/title"> <xsl:template match="figure/title">

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

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

Page 13: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 13

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 14: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 14

What use of XSL(T)?What use of XSL(T)?

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 + (using XSLT + XSL formatting objectsXSL 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» on a Web serveron a Web server» in a Web browser (if the browser supports)in a Web browser (if the browser supports)

Page 15: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 15

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– supported by current major browserssupported by current major browsers

» transformation of XML to HTML on the fly in browsertransformation of XML to HTML on the fly in browser

XSLT on a Web serverXSLT on a Web server– an HTTP request served by transforming XML on the an HTTP request served by transforming XML on the

fly to HTML (or other format) on the serverfly to HTML (or other format) on the server

Page 16: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 16

Main Aspects of XSLTMain Aspects of XSLT

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

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

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

Processing modelProcessing model– How does the XSLT execution proceed?How does the XSLT execution proceed?

Page 17: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 17

Data Model of XSLT and XPathData Model of XSLT and XPath

Documents are viewed as treesDocuments are viewed as treesmade of seven types of nodes:made of seven types of 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 18: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 18

XSLT/XPath document treesXSLT/XPath document trees

Defined in Sect. 5 of XPath 1.0 specDefined in Sect. 5 of XPath 1.0 spec– for XSLT/XPath 2.0 & XQuery in their joint Data Model for XSLT/XPath 2.0 & XQuery in their joint Data Model

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– NBNB: attribute nodes are : attribute nodes are notnot children (but have a parent) children (but have a parent)– the the valuevalue of an element node is the concatenation of of an element node is the concatenation of

its text-node descendantsits text-node descendants

Page 19: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 19

XSLT/XPath TreesXSLT/XPath Trees

Similar to the DOM, with slight differences: Similar to the DOM, with slight differences: – 7 vs 12 node types7 vs 12 node types

– value of an element: its full textual content value of an element: its full textual content (In DOM: (In DOM: nullnull))

– names for text nodes, comment nodes, etc are names for text nodes, comment nodes, etc are emptyempty(In DOM: (In DOM: "#text""#text", , "#comment""#comment", etc.) , etc.)

Document orderDocument order of nodes: of nodes:– root node first, otherwise according to the order of the first root node first, otherwise according to the order of the first

character of the XML markup for each nodecharacter of the XML markup for each node

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

Page 20: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 20

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

77thth

44thth

22ndnd

nullnull in DOM in DOM

Page 21: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 21

Main Aspects of XSLTMain Aspects of XSLT

Data modelData model Selection mechanismSelection mechanism

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

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

Page 22: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 22

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 23: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 23

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 as context nodeeach node in turn as context node» path begins with ‘path begins with ‘//’ ’ root is the first context node root is the first context node

Complete location step: Complete 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– filtered further by 0 or more filtered further by 0 or more predicatespredicates

Page 24: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 24

Location steps: AxesLocation steps: Axes

In total 13 axes (~ directions in tree)In total 13 axes (~ directions in tree)– 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 start/end of the document:for moving towards start/end of the document:» preceding-siblingpreceding-sibling, , following-siblingfollowing-sibling, , precedingpreceding, , followingfollowing

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

Page 25: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 25

XPath Axes and Their OrientationXPath Axes and Their Orientation

Ordinary axes oriented away from context nodeOrdinary axes oriented away from context node((attributeattribute and and namespacenamespace axes are unordered) axes are unordered)– 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 26: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 26

XPath Axes and Their OrientationXPath Axes and Their Orientation

parent::parent:: (exists for every node except the root)(exists for every node except the root)

1

Context node:

Page 27: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 27

XPath Axes and Their OrientationXPath Axes and Their Orientation

ancestor::ancestor:: 2

1

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

2

1

Page 28: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 28

XPath Axes and Their OrientationXPath Axes and Their Orientation

child::child::

431 2

C ontextnode :

Page 29: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 29

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 30: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 30

XPath Axes and Their OrientationXPath Axes and Their Orientation

preceding-sibling::preceding-sibling::

2 1

following-sibling::following-sibling::

21

Page 31: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 31

XPath Axes and Their OrientationXPath Axes and Their Orientation

following::following::

21

3

4

5

preceding::preceding::

3

2 1

Page 32: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 32

Location paths: Node testsLocation paths: Node tests

Location paths: [/]Step/.../Step, where Location paths: [/]Step/.../Step, where – Step = [axis::] NodeTest ('['Pred']')*Step = [axis::] NodeTest ('['Pred']')*

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

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

– **: any : any elementelement (any (any attributeattribute node on an attribute axis) node on an attribute axis)

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

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

Page 33: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 33

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 34: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 34

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 35: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 35

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

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

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

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

the context node)the context node) All All chapterchapters ofs of typetype AA oror BB: :

//chapter[@type='A' or @type='B']//chapter[@type='A' or @type='B'] the previousthe previous chapterchapter siblingsibling: :

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

Page 36: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 36

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

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

NBNB: Node sets as truth values: empty - : Node sets as truth values: empty - falsefalse; non-empty - ; non-empty - truetrue

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

sectsections whoseions whose typetype attribute equals attribute equals stylestyle 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 37: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 37

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 38: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 38

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

also used for numbering (Which parts are counted?)also used for numbering (Which parts are counted?)

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

separated byseparated by ' '////' ' or or ''//''» but arbitrary predicates in but arbitrary predicates in [[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)

Page 39: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 39

XSLT Patterns: SemanticsXSLT Patterns: Semantics

A A node node nn matches a pattern matches a pattern PP if if nn would be would be selected by selected by PP in some context in some context– > the context has to be an ancestor of > the context has to be an ancestor of nn– can be tested by considering Steps of can be tested by considering Steps of PP right-to-left and right-to-left and

checking that they can be satisfied along a path from checking that they can be satisfied along a path from nn towards roottowards root

Page 40: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 40

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

PatternPattern

/appendix//ulist/item[1]/appendix//ulist/item[1]

matches the first matches the first itemitem element in aelement in a ulistulist which is contained in anwhich is contained in an appendixappendix, which is the document element, which is the document element

Page 41: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 41

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 42: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 42

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

2. Serialize the result tree 2. Serialize the result tree (as XML, HTML or text)(as XML, HTML or text)

Page 43: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 43

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 44: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 44

XSLT Result Construction Algorithm (sketch)XSLT Result Construction Algorithm (sketch)

ResultTree:= ApplTempls([root of the source tree]);ResultTree:= ApplTempls([root of the source tree]);

procproc ApplTempls(CNL: ApplTempls(CNL: list of list of Nodes) Nodes) returns list of returns list of Nodes:Nodes:

ResList:= emptyNodeList();ResList:= emptyNodeList();

for eachfor each Node cn Node cn inin CNL CNL do do // // current node current node in in current node listcurrent node list

Find matching template rule (of highest Find matching template rule (of highest prioritypriority; See next); See next)

Instantiate its template T in context (cn, CNL), and add to ResList; Instantiate its template T in context (cn, CNL), and add to ResList; Replace each Replace each <apply-templates select="<apply-templates select="EE"/>"/> in T by in T by ApplTempls(L), where L = value of expr E in context (cn, CNL);ApplTempls(L), where L = value of expr E in context (cn, CNL);

end forend for;;

returnreturn ResList; ResList;Example: see nextExample: see next

Page 45: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 45

Rules:Rules:

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

select="A/B" />select="A/B" /> </X> </X></xsl:template></xsl:template>

<xsl:template match="B"><xsl:template match="B"> <Y pos="{position()}"> <Y pos="{position()}"> <xsl:apply-templates /> <xsl:apply-templates /> </Y> </Y></xsl:template> </xsl:template>

<xsl:template match="C"><xsl:template match="C"> <Z pos="{position()}"> <Z pos="{position()}"> <xsl:apply-templates /> <xsl:apply-templates /> </Z> </Z></xsl:template></xsl:template>

Source: Source: <A><B><C /></B><B><C /></B></A><A><B><C /></B><B><C /></B></A>

Result: Result:

Page 46: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 46

Selecting Selecting oneone of matching rules of 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 parapara, , @href@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 (quietly!) priority is an error - Processor may (quietly!) choose the choose the last onelast one of them of them

Page 47: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 47

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 ~ ~ select="child::node()"select="child::node()"))

“ “default traversaldefault traversal””: top-down : top-down

Nodes are processed in Nodes are processed in document order document order (if not sorted (if not sorted with with xsl:sortxsl:sort))

Built-in rulesBuilt-in rules support the top-down traversal if no support the top-down traversal if no matching rules are foundmatching rules are found

Page 48: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 48

Built-In Default RulesBuilt-In Default Rules

For the root and element nodes:For the root and element nodes: <xsl:template match="/ | *"> <xsl:template match="/ | *">

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

For text and attribute nodes: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 49: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 49

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:apply-templates select="//C" /><xsl:apply-templates 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:apply-templates select="../B" /> <xsl:apply-templates select="../B" />

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

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

Page 50: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 50

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 NewCNewC element node with text node element node with text node "New: ""New: " as its first child.as its first child.

Page 51: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 51

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 "b1""b1" and and "b3""b3" become the next become the next children ofchildren of NewCNewC..

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 52: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 52

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 53: 4 Document Transformations

SDPL 2011 4: Overview of XSLT 53

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

Seldom, fortunatelySeldom, fortunately– but a computer scientist wants to understand how a but a computer scientist wants to understand how a

model worksmodel works Often sufficient to give simple declarative rules for Often sufficient to give simple declarative rules for

different cases, likedifferent cases, like<xsl:template match="<xsl:template match="parapara"> ">

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