24
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

  • Upload
    lev

  • View
    58

  • Download
    0

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 1

5.1 XPath & XSLT: Additional features5.1 XPath & XSLT: Additional features

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

Page 2: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 2

XPath: Arithmetical OperationsXPath: Arithmetical Operations

Operations for double-precision (64 bit) Operations for double-precision (64 bit) floating-point numbersfloating-point numbers

+, -, *, div, mod+, -, *, div, mod (same as (same as %% in Java)in Java)– functions to map numbers to integers:functions to map numbers to integers:

» floor(-1.1) = -2, floor(1.1)=floor(1.5)=1floor(-1.1) = -2, floor(1.1)=floor(1.5)=1» ceiling(-1.1) = -1, ceiling(-1.1) = -1, ceiling(1.1)=ceiling(1.5)=2ceiling(1.1)=ceiling(1.5)=2

» round(-1.1) = -1, round(1.1)= 1, round(-1.1) = -1, round(1.1)= 1, round(-1.5) = -1, round(1.5) = 2round(-1.5) = -1, round(1.5) = 2

Page 3: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 3

Cross-referencingCross-referencing

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

Page 4: 5.1 XPath & XSLT: Additional features

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

Page 5: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 5

XPath: more string functionsXPath: more string functions

– substring-before("dogsubstring-before("dogbertbert", "", "bertbert") = ") = substring-before("dogsubstring-before("dogbbert", "ert", "bb") = "dog"") = "dog"

– substring-after("dosubstring-after("doggbert", "bert", "gg")= "bert"")= "bert"– substring(substring(string, startpos, length?string, startpos, length?):):

» substring("dogbert", 1, 3) = "dog"substring("dogbert", 1, 3) = "dog"» substring("dogbert", 3) = "gbert"substring("dogbert", 3) = "gbert"

– string-length("dogbert")=7string-length("dogbert")=7– translate(translate(Str, ReplacedChars, ReplacingCharsStr, ReplacedChars, ReplacingChars):):

» translate("dogbert", "dgo", "Dli") = "Dilbert"translate("dogbert", "dgo", "Dli") = "Dilbert"

Page 6: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 6

Computing generated textComputing generated text

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

Page 7: 5.1 XPath & XSLT: Additional features

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

Page 8: 5.1 XPath & XSLT: Additional features

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

Consider transforming source elementConsider transforming source element<photo><photo>

<file>Mary.jpg</file><file>Mary.jpg</file> <size width="300"/><size width="300"/>

</photo></photo>into forminto form

<img src="/images/Mary.jpg" <img src="/images/Mary.jpg" width="300" />width="300" />

Page 9: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 9

Attribute value templates (2)Attribute value templates (2)

This can be specified by template ruleThis can be specified by template rule<xsl:template match="photo"><xsl:template match="photo">

<img src="/images/{file}"<img src="/images/{file}"width="{size/@width}" />width="{size/@width}" />

</xsl:template></xsl:template> ExpressionsExpressions {file} {file} and and {size/@width}{size/@width} are are

evaluated in the context of the current node (the evaluated in the context of the current node (the photophoto element)element)

Page 10: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 10

XSLT: RepetitionXSLT: Repetition

Nodes can be "pulled" from source for processing Nodes can be "pulled" from source for processing using instructionusing instruction

<xsl:for-each select="<xsl:for-each select="ExprExpr">">TemplateTemplate</xsl:for-each></xsl:for-each>

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

Page 11: 5.1 XPath & XSLT: Additional features

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>

Page 12: 5.1 XPath & XSLT: Additional features

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>

Page 13: 5.1 XPath & XSLT: Additional features

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

select="substring(title, 1, 8)" />...)select="substring(title, 1, 8)" />...) </xsl:for-each></xsl:for-each></xsl:template></xsl:template>

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

Page 14: 5.1 XPath & XSLT: Additional features

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.

Page 15: 5.1 XPath & XSLT: Additional features

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>

Page 16: 5.1 XPath & XSLT: Additional features

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

Page 17: 5.1 XPath & XSLT: Additional features

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 test="<xsl:if test="ExpressionExpression">">TemplateTemplate

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

Page 18: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 18

Conditional processing (2)Conditional processing (2)

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>

Page 19: 5.1 XPath & XSLT: Additional features

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>

Page 20: 5.1 XPath & XSLT: Additional features

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:

<xsl:template match="ol/item"><xsl:template match="ol/item"> <!-- default: count like siblings (items) --> <!-- default: count like siblings (items) --> <xsl:number format="1. "/> <xsl:number format="1. "/> <xsl:apply-templates/> <xsl:apply-templates/><xsl:template><xsl:template>

Page 21: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 21

Generating numbers (2)Generating numbers (2)

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>

Page 22: 5.1 XPath & XSLT: Additional features

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>

Page 23: 5.1 XPath & XSLT: Additional features

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

select="select="concat(last, ', ', first)concat(last, ', ', first)"" />/>… …

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

<xsl:with-param <xsl:with-param name="pname"name="pname" select="select="$LandF$LandF"" /> /> </xsl:call-template> </xsl:call-template>

… …</xsl:for-each></xsl:for-each>

Page 24: 5.1 XPath & XSLT: Additional features

SDPL 2001 Notes 5.1: Additional XPath & XSLT 24

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