SDPL 2001 Notes 5.1: Additional XPath & XSLT 1 5.1 XPath & XSLT: Additional 5.1 XPath & XSLT: Additional features features XPath support for XPath support for – arithmetical operations arithmetical operations – processing ID/IDREF cross-references processing ID/IDREF cross-references – manipulation of strings manipulation of strings Generating text Generating text – for content for content – for attribute values for attribute values Repetition, sorting and conditional Repetition, sorting and conditional processing processing Generating numbers Generating numbers Computational power of XSLT Computational power of XSLT
5.1 XPath & XSLT: Additional features. XPath support for arithmetical operations processing ID/IDREF cross-references manipulation of strings Generating text for content for attribute values Repetition, sorting and conditional processing Generating numbers Computational power of XSLT. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
XPath support forXPath support for– arithmetical operationsarithmetical operations– processing ID/IDREF cross-referencesprocessing ID/IDREF cross-references– manipulation of stringsmanipulation of strings
Generating textGenerating text– for contentfor content– for attribute valuesfor attribute values
Repetition, sorting and conditional processingRepetition, sorting and conditional processing Generating numbersGenerating numbers Computational power of XSLTComputational power of XSLT
Function Function idid selects elements by their unique IDselects elements by their unique ID– NBNB: ID attributes need to be declared : ID attributes need to be declared
(in DTD or its internal subset; See an example later)(in DTD or its internal subset; See an example later) ExamplesExamples
– id('sect:intro')id('sect:intro') selects the element with unique IDselects the element with unique ID "sect:intro""sect:intro"
– id('sect:intro')/para[5]id('sect:intro')/para[5] selects the fifthselects the fifth parapara child of the above elementchild of the above element
SDPL 2001 Notes 5.1: Additional XPath & XSLT 4
String manipulationString manipulation
Equality and inequality of strings can be tested Equality and inequality of strings can be tested with operatorswith operators == and and !=!=– "foo" = 'foo'"foo" = 'foo'; ; "foo" != "Foo""foo" != "Foo"
Functions for concatenation, and for testing for Functions for concatenation, and for testing for substrings:substrings:– concat("dog", "bert") = "dogbert"concat("dog", "bert") = "dogbert"– starts-with("dogbert", "dog") = true()starts-with("dogbert", "dog") = true()– contains("docontains("dogbegbert", "rt", "gbegbe") = true()") = true()
SDPL 2001 Notes 5.1: Additional XPath & XSLT 5
XPath: more string functionsXPath: more string functions
The string-value of an expression can be inserted in the The string-value of an expression can be inserted in the result tree by instructionresult tree by instruction
<<xsl:value-of select="xsl:value-of select="ExprExpr" />" />– ifif the expression evaluates to a the expression evaluates to a node-setnode-set, the , the value of the value of the
first nodefirst node in document order is usedin document order is used
Consider transforming source elements likeConsider transforming source elements like<name alias="Bird"><name alias="Bird"><first>Charlie</first><last>Parker</last><first>Charlie</first><last>Parker</last></name></name>
to the formto the formCharlie ("Bird") ParkerCharlie ("Bird") Parker
SDPL 2001 Notes 5.1: Additional XPath & XSLT 7
Computing generated text (2)Computing generated text (2)
This can be specified by template ruleThis can be specified by template rule<xsl:template match="name"><xsl:template match="name"><xls:value-of select="first" /><xls:value-of select="first" />("<xsl:value-of select="@alias" />") ("<xsl:value-of select="@alias" />") <xls:value-of select="last" /> <xls:value-of select="last" /> <xsl:text><xsl:text></xsl:text></xsl:text></xsl:template></xsl:template>
Verbatim text (like the linefeed above) can be Verbatim text (like the linefeed above) can be inserted usinginserted using xsl:text xsl:text
SDPL 2001 Notes 5.1: Additional XPath & XSLT 8
Attribute value templatesAttribute value templates
The string-value of an expression can be inserted The string-value of an expression can be inserted in an attribute value by surrounding the in an attribute value by surrounding the expression by bracesexpression by braces { { and and }}
– the template is applied to each of the selected the template is applied to each of the selected nodes (0, 1 or more), each node in turn as the nodes (0, 1 or more), each node in turn as the current nodecurrent node» in document order, unless sorted using in document order, unless sorted using xsl:sortxsl:sort
instructions (see later)instructions (see later)
SDPL 2001 Notes 5.1: Additional XPath & XSLT 11
Example (Example (xsl:for-eachxsl:for-each))
Consider formatting the below document as HTML:Consider formatting the below document as HTML:<!DOCTYPE document [ <!ATTLIST section id ID #IMPLIED>]><!DOCTYPE document [ <!ATTLIST section id ID #IMPLIED>]><document> <title>The Joy of XML</title> <document> <title>The Joy of XML</title> <section id="Intro"><title>Getting Started</title> <section id="Intro"><title>Getting Started</title> <name><first>Helen</first> <last>Brown</last></name> <name><first>Helen</first> <last>Brown</last></name> says that processing XML documents is fun. says that processing XML documents is fun. <name><first>Dave</first> <last>Dobrik</last></name> agrees. <name><first>Dave</first> <last>Dobrik</last></name> agrees. </section> </section> <section><title>Family affairs</title> <section><title>Family affairs</title> <name><first>Bob</first> <last>Brown</last></name> is the <name><first>Bob</first> <last>Brown</last></name> is the husband of <name><first>Helen</first> husband of <name><first>Helen</first> <last>Brown</last></name>. </section> <last>Brown</last></name>. </section> <section><title>Finishing Up</title> <section><title>Finishing Up</title> As we discussed in <title-ref idref="Intro" />, processing XML As we discussed in <title-ref idref="Intro" />, processing XML documents is fun. </section></document>documents is fun. </section></document>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 12
Example: Table of contentsExample: Table of contents
A table of contents can be formed of section titles:A table of contents can be formed of section titles:<xsl:template match="/"><xsl:template match="/"><HTML><HEAD> <TITLE><xsl:value-of <HTML><HEAD> <TITLE><xsl:value-of select="//document/title"/></TITLE></HEAD> select="//document/title"/></TITLE></HEAD> <BODY> <BODY> <H2>Table of Contents</H2> <H2>Table of Contents</H2> <OL> <!-- Pull each section title: --> <OL> <!-- Pull each section title: --> <xsl:for-each select="//section/title"><xsl:for-each select="//section/title"> <LI><xsl:apply-templates /></LI> <LI><xsl:apply-templates /></LI> </xsl:for-each></xsl:for-each> </OL> <!-- then process the sections: --> </OL> <!-- then process the sections: --> <xsl:apply-templates select="//section"/> <xsl:apply-templates select="//section"/> </BODY> </HTML> </BODY> </HTML></xsl:template></xsl:template>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 13
Example (cont; Cross references)Example (cont; Cross references)
Cross references (to sections) can also be processed Cross references (to sections) can also be processed using using xsl:for-each: xsl:for-each: <xsl:template match="title-ref"> <xsl:template match="title-ref"> <xsl:for-each select="id(@idref)"><xsl:for-each select="id(@idref)"> Section (<xsl:value-of Section (<xsl:value-of
With this rule the source fragment With this rule the source fragment As we discussed in <title-ref idref="Intro"/>As we discussed in <title-ref idref="Intro"/>
becomesbecomesAs we discussed in Section (Getting …)As we discussed in Section (Getting …)
SDPL 2001 Notes 5.1: Additional XPath & XSLT 14
XSLT SortingXSLT Sorting
A sorted order for the processing of nodes with A sorted order for the processing of nodes with xsl:for-eachxsl:for-each and and xls:apply-templatesxls:apply-templates can can be specified by be specified by <xsl:sort/><xsl:sort/>
controlled by attributes of controlled by attributes of xsl:sortxsl:sort like like– selectselect: expression for the sort key: expression for the sort key– data-typedata-type: : "text" "text" (default) or(default) or "number" "number" – orderorder: : "ascending""ascending" (default) (default)
or or "descending""descending" The first The first xsl:sortxsl:sort specifies the primary sort key, specifies the primary sort key,
the second one the secondary sort key, and so on.the second one the secondary sort key, and so on.
SDPL 2001 Notes 5.1: Additional XPath & XSLT 15
Example (cont; Sorted index of names)Example (cont; Sorted index of names)
All names can be collected in a last-name-first-name order All names can be collected in a last-name-first-name order using the below templateusing the below template<H2>Index</H2> <UL><H2>Index</H2> <UL> <xsl:for-each select="//name"> <xsl:for-each select="//name"> <xsl:sort select="last" /> <xsl:sort select="last" /> <xsl:sort select="first" /> <xsl:sort select="first" /> <LI><xsl:value-of select="last" <LI><xsl:value-of select="last" />, <xsl:value-of select="first"/></LI> />, <xsl:value-of select="first"/></LI> </xsl:for-each> </xsl:for-each></UL></UL>
This creates an UL list with itemsThis creates an UL list with items<LI>Brown, Bob</LI> <LI>Brown, Bob</LI> <LI>Brown, Helen</LI> <LI>Brown, Helen</LI> <LI>Brown, Helen</LI> <LI>Brown, Helen</LI> <LI>Dobrik, Dave</LI><LI>Dobrik, Dave</LI>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 16
What about duplicates?What about duplicates?
Is it possible to eliminate duplicate values like Is it possible to eliminate duplicate values like <LI>Brown, Helen</LI> <LI>Brown, Helen</LI> <LI>Brown, Helen</LI> <LI>Brown, Helen</LI> ??
Yes (but not that straightforward)Yes (but not that straightforward) Using conditional instructionsUsing conditional instructions
– See nextSee next
SDPL 2001 Notes 5.1: Additional XPath & XSLT 17
Conditional processingConditional processing
A template can be instantiated or ignored based A template can be instantiated or ignored based on the value of a on the value of a testtest Boolean expression, usingBoolean expression, using
</xsl:if> </xsl:if> Example: a comma-separated list of names:Example: a comma-separated list of names:<xsl:template match="namelist/name"><xsl:template match="namelist/name"> <xsl:apply-templates/> <xsl:apply-templates/> <xsl:if test="not(position()=last())" <xsl:if test="not(position()=last())" >, </xsl:if> >, </xsl:if></xsl:template></xsl:template>
Also a case-like construct:Also a case-like construct: <xsl:choose><xsl:choose>
<!-- The first 'when' whose test=true() is <!-- The first 'when' whose test=true() is instantiated: -->instantiated: --><xsl:when test="<xsl:when test="Expr1Expr1"> … </xsl:when>"> … </xsl:when><xsl:when test="<xsl:when test="Expr2Expr2"> … </xsl:when>"> … </xsl:when>… … <!-- If no 'when' applies, optional 'otherwise' <!-- If no 'when' applies, optional 'otherwise' is instantiated: -->is instantiated: --><xsl:otherwise> … </xsl:otherwise ><xsl:otherwise> … </xsl:otherwise >
</xsl:choose></xsl:choose>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 19
Example (cont; Eliminating duplicate names)Example (cont; Eliminating duplicate names)
No access to other nodes (except No access to other nodes (except current()current()) in the list of ) in the list of xsl:for-each xsl:for-each (?)(?)– But can refer to other nodes in the source treeBut can refer to other nodes in the source tree– Process just the first one of duplicate Process just the first one of duplicate namenames: s: <xsl:for-each select="//name"><xsl:for-each select="//name"> <xsl:sort select="last"/><xsl:sort select="last"/><xsl:sort select="first" /> <xsl:sort select="first" /> <xsl:if test="not(<xsl:if test="not(preceding::name[first=current()/first preceding::name[first=current()/first and last=current()/last])">and last=current()/last])"> <LI><xsl:value-of select="last" <LI><xsl:value-of select="last" />, <xsl:value-of select="first"/></LI> />, <xsl:value-of select="first"/></LI></xsl:if></xsl:if></xsl:for-each></xsl:for-each>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 20
Generating numbersGenerating numbers
Formatted numbers can be inserted in the result tree Formatted numbers can be inserted in the result tree by element by element xsl:numberxsl:number – can be specified by attributecan be specified by attribute value="value="ExprExpr""– otherwise the number generated based on the position of otherwise the number generated based on the position of
the current node in the source treethe current node in the source tree Example 1, a numbered list:Example 1, a numbered list:
Example 2. Hierarchical numbering (1, 1.1, 1.1.1, Example 2. Hierarchical numbering (1, 1.1, 1.1.1, 1.1.2, …) for titles of chapters, titles of sections of 1.1.2, …) for titles of chapters, titles of sections of chapters, and titles of subsections of sectionschapters, and titles of subsections of sections<xsl:template match="title"><xsl:template match="title"> <xsl:number level="multiple" <xsl:number level="multiple" count="chapter|section|subsection" count="chapter|section|subsection" format="1.1 "/> format="1.1 "/> <xsl:apply-templates/> <xsl:apply-templates/></xsl:template></xsl:template>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 22
Generating numbers (3)Generating numbers (3)
Example 3. Example 3. Sequential numbering of Sequential numbering of notenotes s withinwithin chapterchapters:s:<xsl:template match="note"><xsl:template match="note"> <xsl:number level="any" <xsl:number level="any" from="chapter" from="chapter" format="(1) "/> format="(1) "/> <xsl:apply-templates/> <xsl:apply-templates/></xsl:template></xsl:template>
SDPL 2001 Notes 5.1: Additional XPath & XSLT 23
Computational power of XSLTComputational power of XSLT
Programming-like features, for example, Programming-like features, for example, – limitedlimited variablesvariables (names bound to values): (names bound to values): <xsl:for-each select="//name"><xsl:for-each select="//name"> <xsl:variable <xsl:variable name="name="LandFLandF" "
– named templatesnamed templates which can be explicitly called with which can be explicitly called with parametersparameters:: <xsl:call-template <xsl:call-template name="process-name"name="process-name">>
Computational power of XSLTComputational power of XSLT
Conditional instructions, string functions and named Conditional instructions, string functions and named templates sufficient to simulate Turing machinestemplates sufficient to simulate Turing machines
Implications:Implications:– XSLT has XSLT has full algorithmic programming powerfull algorithmic programming power
» Is this intentional?Is this intentional?» Inconvenient as a general-purpose programming language!Inconvenient as a general-purpose programming language!
– Impossible to recognise non-terminating Impossible to recognise non-terminating transformations automaticallytransformations automatically
» Malicious hacker could cause "denial-of-service" through non-Malicious hacker could cause "denial-of-service" through non-terminating style sheets terminating style sheets