+ All Categories
Home > Documents > SDPL 2003Notes 5.1: Additional XPath & XSLT1 5.1 Additional features of XPath & XSLT n XPath support...

SDPL 2003Notes 5.1: Additional XPath & XSLT1 5.1 Additional features of XPath & XSLT n XPath support...

Date post: 15-Dec-2015
Category:
Upload: victor-packard
View: 236 times
Download: 0 times
Share this document with a friend
26
SDPL 2003 Notes 5.1: Additional XPath & XSLT 1 5.1 Additional features of 5.1 Additional features of XPath & XSLT XPath & XSLT 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
Transcript

SDPL 2003 Notes 5.1: Additional XPath & XSLT 1

5.1 Additional features of XPath & XSLT5.1 Additional features of XPath & XSLT

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

SDPL 2003 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

SDPL 2003 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)

Examples:Examples:– 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– id('sect1 sect2 sect3')id('sect1 sect2 sect3')selects 3 sections selects 3 sections

(if they have the corresponding ID values)(if they have the corresponding ID values)

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

Testing for substrings:Testing for substrings:– starts-with("starts-with("dogdogbert", "bert", "dogdog") = true()") = true()– contains("docontains("dogbegbert", "rt", "gbegbe") = true()") = true()

Concatenation (of two or more strings), Concatenation (of two or more strings), – concat("dog", "bert") = "dogbert"concat("dog", "bert") = "dogbert"

SDPL 2003 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("translate("dogdogbert", "bert", "dgodgo", "Dli") ", "Dli") = "Dilbert"= "Dilbert"

SDPL 2003 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 value of , the value of the 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 form

Charlie ("Bird") ParkerCharlie ("Bird") Parker

SDPL 2003 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 white space above) can be Verbatim text (like the white space above) can be inserted usinginserted using xsl:text xsl:text

SDPL 2003 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><file>Mary.jpgMary.jpg</file></file> <size width="300"/><size width="300"/>

</photo></photo>into forminto form

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

SDPL 2003 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)

SDPL 2003 Notes 5.1: Additional XPath & XSLT 10

XSLT: RepetitionXSLT: Repetition

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

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

</xsl:</xsl:for-eachfor-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)

SDPL 2003 Notes 5.1: Additional XPath & XSLT 11

Example (of Example (of xsl:xsl:for-eachfor-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 2003 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 2003 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 …)

SDPL 2003 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 be specified by can 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 2003 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 2003 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 2003 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>

SDPL 2003 Notes 5.1: Additional XPath & XSLT 18

Conditional processing (2)Conditional processing (2)

Also a case-like construct (Also a case-like construct ( switchswitch in Java): in Java): <xsl:choose><xsl:choose>

<!-- The first 'when' whose test=true() is <!-- The first 'when' whose test=true() is instantiated: -->instantiated: --><<xsl:whenxsl:when test=" test="Expr1Expr1"> … </"> … </xsl:whenxsl:when>><<xsl:whenxsl:when test=" test="Expr2Expr2"> … </"> … </xsl:whenxsl:when>>

… … <!-- If no 'when' applies, optional 'otherwise' <!-- If no 'when' applies, optional 'otherwise' is instantiated: -->is instantiated: --><<xsl:otherwisexsl:otherwise> … </> … </xsl:otherwisexsl:otherwise>>

</xsl:choose></xsl:choose>

SDPL 2003 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-eachxsl: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 2003 Notes 5.1: Additional XPath & XSLT 20

Generating NumbersGenerating Numbers

Formatted numbers can be inserted in the result Formatted numbers can be inserted in the result tree by element tree by element xsl:numberxsl:number – number can be specified by attributenumber can 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:numberxsl:number format="1. "/> format="1. "/> <xsl:apply-templates/> <xsl:apply-templates/><xsl:template><xsl:template>

SDPL 2003 Notes 5.1: Additional XPath & XSLT 21

Generating numbers: Example 1Generating numbers: Example 1

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

itemitem itemitem itemitem

olol

apricotapricot bananabanana coconutcoconut

1.1. 2.2. 3.3.

apricotapricotbananabananacoconutcoconut

SDPL 2003 Notes 5.1: Additional XPath & XSLT 22

Generating numbers: Example 2Generating numbers: Example 2

Hierarchical numbering (1, 1.1, 1.1.1, 1.1.2, …) Hierarchical numbering (1, 1.1, 1.1.1, 1.1.2, …) for titles of chapters, titles of their sections, and for titles of chapters, titles of their sections, and titles of subsections of sections:titles of subsections of sections:<xsl:template match="title"><xsl:template match="title"> < <xsl:numberxsl:number level="multiple" level="multiple" count="chap|sect|subsect" count="chap|sect|subsect" format="1.1 "/> format="1.1 "/> <xsl:apply-templates/> <xsl:apply-templates/></xsl:template></xsl:template>

SDPL 2003 Notes 5.1: Additional XPath & XSLT 23

Generating numbers: Example 2Generating numbers: Example 2

11 1.11.1 1.1.11.1.1 22

chapchap

SweetsSweets

titletitle

titletitle

BerriesBerries

sectsect

subsectsubsect

CherryCherry

titletitle

chapchap

titletitle

VegetablesVegetables

. . .. . .

. . .. . .

SweetsSweets BerriesBerries CherryCherry VegetablesVegetables

SDPL 2003 Notes 5.1: Additional XPath & XSLT 24

Example 2: VariationExample 2: Variation

As above, but numbering titles within appendices As above, but numbering titles within appendices with A, A.1, A.1.1, B.1 etc:with A, A.1, A.1.1, B.1 etc:

<xsl:template match="appendix//title"<xsl:template match="appendix//title" priority="1"> priority="1">

< <xsl:numberxsl:number level="multiple" level="multiple" count="appendix|sect|subsect" count="appendix|sect|subsect" format="A.1 "/> format="A.1 "/> <xsl:apply-templates/> <xsl:apply-templates/></xsl:template></xsl:template>

SDPL 2003 Notes 5.1: Additional XPath & XSLT 25

Example 2: VariationExample 2: Variation

AA A.1A.1 A.1.1A.1.1 BB

appendixappendix

SweetsSweets

titletitle

titletitle

BerriesBerries

sectsect

subsectsubsect

CherryCherry

titletitle

titletitle

VegetablesVegetables

. . .. . .

. . .. . .

SweetsSweets BerriesBerries CherryCherry VegetablesVegetables

appendixappendix

SDPL 2003 Notes 5.1: Additional XPath & XSLT 26

Generating numbers: Example 3Generating numbers: Example 3

Sequential numbering of Sequential numbering of notenotes s withinwithin chapchaptersters::(more precisely: after the start of the preceding chapter)(more precisely: after the start of the preceding chapter)

<xsl:template match="note"><xsl:template match="note"> <xsl:number level="any" <xsl:number level="any" from="chap" from="chap" format="(1) "/> format="(1) "/> <xsl:apply-templates/> <xsl:apply-templates/></xsl:template></xsl:template>


Recommended