SDPL 2005 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 – arithmetics arithmetics – 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
5.1 Additional features of XPath & XSLT. XPath support for arithmetics processing ID/IDREF cross-references manipulation of strings Generating text for content for attribute values Repetition, sorting and conditional processing Generating numbers. XPath: Arithmetical Operations. - 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.
Transcript
SDPL 2005 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– arithmeticsarithmetics– 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
– and summing them as numbers and summing them as numbers » sum(sum(node-setnode-set))
Example: Example: – Grade average for the first student in document:Grade average for the first student in document:sum(sum((//student)[1]/course/@grade(//student)[1]/course/@grade)) div div count(count((//student)[1]/course(//student)[1]/course))
SDPL 2005 Notes 5.1: Additional XPath & XSLT 4
Cross-referencingCross-referencing
Function Function idid selects elements by their unique IDselects elements by their unique ID– NBNB: ID attributes need to be declared in DTD: ID attributes need to be declared in DTD
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 2005 Notes 5.1: Additional XPath & XSLT 5
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 2005 Notes 5.1: Additional XPath & XSLT 6
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-ofxsl:value-of select=" select="ExprExpr" />" />– if if ExprExpr evaluates to a node-set, evaluates to a node-set, value of the first nodevalue of the first node in in
document order is useddocument order is used
Consider transforming source elements likeConsider transforming source elements like
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 2005 Notes 5.1: Additional XPath & XSLT 9
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()current() node node» in document order, unless sorted using in document order, unless sorted using xsl:sortxsl:sort
instructions (see later)instructions (see later)
SDPL 2005 Notes 5.1: Additional XPath & XSLT 12
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 2005 Notes 5.1: Additional XPath & XSLT 13
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=“document/section"/> <xsl:apply-templates select=“document/section"/> </BODY> </HTML> </BODY> </HTML></xsl:template></xsl:template>
SDPL 2005 Notes 5.1: Additional XPath & XSLT 14
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 2005 Notes 5.1: Additional XPath & XSLT 15
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:sortxsl: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 2005 Notes 5.1: Additional XPath & XSLT 16
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:sortxsl:sort select="last" /> select="last" /> < <xsl:sortxsl:sort select="first" /> 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 2005 Notes 5.1: Additional XPath & XSLT 17
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 2005 Notes 5.1: Additional XPath & XSLT 18
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:ifxsl: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:ifxsl:if test="position() < last()" test="position() < last()" > >, , </</xsl:ifxsl:if>></xsl:template></xsl:template>
<!-- If no '<!-- If no 'whenwhen' applies, an optional ' applies, an optional ''otherwiseotherwise' is instantiated: -->' is instantiated: --><xsl:otherwise><xsl:otherwise> … … </xsl:otherwise></xsl:otherwise>
</xsl:choose></xsl:choose>
SDPL 2005 Notes 5.1: Additional XPath & XSLT 20
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] )">
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 by the position of the otherwise the number generated by the position of the
current node in the source treecurrent node in the source tree
We’ll consider typical cases through examplesWe’ll consider typical cases through examples– The complete rules of the Spec are rather complexThe complete rules of the Spec are rather complex
Example 1: Numbering list itemsExample 1: Numbering list items
SDPL 2005 Notes 5.1: Additional XPath & XSLT 22
Generating numbers: Example 1Generating numbers: Example 1
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:titles of subsections:<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 2005 Notes 5.1: Additional XPath & XSLT 24
Generating numbers: Example 2Generating numbers: Example 2
Generating numbers: Example 3Generating numbers: Example 3
Sequential numbering of Sequential numbering of notenotes s withinwithin chapchaptersters::(more precisely: starting anew at the start of any chapter)(more precisely: starting anew at the start of any chapter)