SDPL 2006 XSLT: Additional Features a nd Computing 1 5.1 Additional Features 5.1 Additional Features 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
38
Embed
SDPL 2006XSLT: Additional Features and Computing1 5.1 Additional Features n XPath support for –arithmetics –processing ID/IDREF cross-references –manipulation.
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 2006 XSLT: Additional Features and Computing
1
5.1 Additional Features 5.1 Additional Features
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
Operators for double-precision (64 bit) floating-Operators for double-precision (64 bit) floating-point numberspoint numbers
++, , --, , **, , divdiv, , modmod (same as (same as %% in Java)in Java) Rounding numbers up, down, and to the closest integer:Rounding numbers up, down, and to the closest integer:
floor(x)floor(x), , ceiling(x)ceiling(x),,round(x)round(x) Formatting numbers as strings (e.g.):Formatting numbers as strings (e.g.):
– and summing them as numbers and summing them as numbers » sum(sum(node-setnode-set))
Example: Example: – Average of course grades below current node:Average of course grades below current node:sum(sum(.//course/@grade.//course/@grade)) div div count(count(.//course.//course))
SDPL 2006 XSLT: Additional Features and Computing
4
Cross-referencingCross-referencing
Function Function idid selects elements by their unique IDselects elements by their unique ID– NBNB: ID attributes must be declared in DTD: ID attributes must be declared in DTD
selects the element with unique IDselects the element with unique ID ""sect:introsect:intro""– id('sect:intro')/auth[3]id('sect:intro')/auth[3]
selects the thirdselects the third authauth of the above elementof the above element– id('sect1 sect2 sect3')id('sect1 sect2 sect3')selects 3 sections selects 3 sections
(with corresponding ID values)(with corresponding ID values)
SDPL 2006 XSLT: Additional Features and Computing
5
String manipulationString manipulation
Equality and inequality of strings can be tested with Equality and inequality of strings can be tested with operatorsoperators == andand !=!=– "foo" = 'foo'"foo" = 'foo'; ; (NB alternative quotes)(NB alternative quotes)– "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 2006 XSLT: Additional Features and Computing
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 used (XSLT 2.0: of all, space-separated)document order is used (XSLT 2.0: of all, space-separated)
Example: Transform elements likeExample: Transform 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"><xsl:value-of select="first" /><xsl:value-of select="first" />("<xsl:value-of select="@alias" />") ("<xsl:value-of select="@alias" />") <xsl:value-of select="last" /> <xsl: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:textxsl:text
SDPL 2006 XSLT: Additional Features and Computing
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 {{ andand }}
– TemplateTemplate is applied to the selected nodelist, is applied to the selected nodelist, each node in turn as the 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 2006 XSLT: Additional Features and Computing
12
Example (of Example (of xsl:xsl:for-eachfor-each))
Format the below document as HTML:Format the below document as HTML:<!DOCTYPE document [ <!ATTLIST section id ID #IMPLIED> ]><!DOCTYPE document [ <!ATTLIST section id ID #IMPLIED> ]><document> <title><document> <title>The Joy of XMLThe Joy of XML</title> </title> <section id="Intro"><title><section id="Intro"><title>Getting StartedGetting Started</title> </title> <name><first> <name><first>HelenHelen</first> <last></first> <last>BrownBrown</last></name></last></name> says that XML processing is fun. says that XML processing is fun. <name><first><name><first>DaveDave</first> <last></first> <last>DobrikDobrik</last></name></last></name> agrees. agrees. </section> </section> <section><title><section><title>Family affairsFamily affairs</title> </title> <name><first> <name><first>BobBob</first> <last></first> <last>BrownBrown</last></name></last></name> is is the husband of the husband of <name><first><name><first>HelenHelen</first> </first> <last> <last>BrownBrown</last></name></last></name>. . </section> </section> <section><title><section><title>Finishing UpFinishing Up</title></title> As we discussed in As we discussed in <title-ref idref="Intro" /><title-ref idref="Intro" />, XML , XML processing is fun. processing is fun. </section></section></document></document>
SDPL 2006 XSLT: Additional Features and Computing
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"/>
Example (cont; Cross references)Example (cont; Cross references)
Cross references (to sections) can also be processed Cross references (to sections) can also be processed usingusing for-eachfor-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 2006 XSLT: Additional Features and Computing
15
XSLT SortingXSLT Sorting
A sorted order for the processing of nodes with A sorted order for the processing of nodes with xsl:for-xsl:for-eacheach andand xls:apply-templatesxls:apply-templates can be specified bycan be specified by <<xsl:sortxsl:sort/>/>
controlled by attributes ofcontrolled by attributes of xsl:sortxsl:sort likelike– selectselect: expression for the sort key (default: : expression for the sort key (default: ".""."))– 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 2006 XSLT: Additional Features and Computing
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>
Possible to eliminate duplicates?Possible to eliminate duplicates?Yes, but a bit tricky. See nextYes, but a bit tricky. See next
SDPL 2006 XSLT: Additional Features and Computing
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 Boolean expression:on the value of a Boolean expression:
</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:ifxsl:if test="test="position() < last()position() < last()"" > >,, </</xsl:ifxsl:if>></xsl:template></xsl:template>
SDPL 2006 XSLT: Additional Features and Computing
18
An aside: Meaning of An aside: Meaning of position()position()
Evaluation wrt the current node list. Above rule applied to Evaluation wrt the current node list. Above rule applied to a source witha source with
<!-- 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 2006 XSLT: Additional Features and Computing
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 current ) in the current node listnode list– But can refer to nodes in the source treeBut can refer to 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:number xsl:number />/> – by the position of the current node in the source treeby the position of the current node in the source tree– nodes to be counted specified by a nodes to be counted specified by a countcount pattern pattern– common numbering schemes supported: single-level, common numbering schemes supported: single-level,
hierarchical, and sequential ignoring levelshierarchical, and sequential ignoring levels
Typical cases in following examplesTypical cases in following examples» (Complete specification rather complex)(Complete specification rather complex)
Example 1: Numbering list itemsExample 1: Numbering list items
SDPL 2006 XSLT: Additional Features and Computing
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:number level="multiple"<xsl:number 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 2006 XSLT: Additional Features and Computing
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)
Ex 3: Sequential numbering Ex 3: Sequential numbering fromfrom chaps chaps
chapchap
Yes!Yes!
notenote
notenote
No!No!
sectsectPerhaps?Perhaps?
notenotenotenote
OKOK
. . .. . .
. . .. . .(1)(1) (2)(2) (3)(3) (1)(1)
Yes!Yes! No!No! Perhaps?Perhaps? OKOK
chapchap
SDPL 2006 XSLT: Additional Features and Computing
29
5.2 Computing with XSLT5.2 Computing with XSLT
XSLT is a declarative rule-based languageXSLT is a declarative rule-based language– for a special purpose: XML transformationsfor a special purpose: XML transformations– Could we use XSLT for procedural computing?Could we use XSLT for procedural computing?– What is the exact computational power of XSLT?What is the exact computational power of XSLT?
We've seen some programming-like features:We've seen some programming-like features:– iteration over source nodes (iteration over source nodes (xsl:for-eachxsl:for-each))– conditional evaluation (conditional evaluation (xsl:ifxsl:if andand xsl:choosexsl:choose))
SDPL 2006 XSLT: Additional Features and Computing
30
Computing with XSLTComputing with XSLT
Further programming-like features:Further programming-like features:– variablesvariables (names bound to (names bound to non-updatablenon-updatable values): values): <xsl:for-each select="//name"><xsl:for-each select="//name"> <xsl:variable <xsl:variable name="name="LAndFLAndF" "
Visibility of Variable BindingsVisibility of Variable Bindings
The binding is The binding is visiblevisible in following siblings of in following siblings of xsl:variable,xsl:variable, and in their descendants:and in their descendants:
We used LaTeX to format an XML article. For this, we We used LaTeX to format an XML article. For this, we needed to map source table structuresneeded to map source table structures
More General Solution (1/2)More General Solution (1/2)
Pass the column-count to a named template which Pass the column-count to a named template which generates the requested number of ‘generates the requested number of ‘ll’s:’s:
Computational power of XSLTComputational power of XSLT
XSLT seems quite powerful, but how powerful is it?XSLT seems quite powerful, but how powerful is it?– Implementations provide extension mechanisms, e.g., to Implementations provide extension mechanisms, e.g., to
call arbitrary Java methodscall arbitrary Java methods– Are there limits to XSLT processing that we can do Are there limits to XSLT processing that we can do
withoutwithout extensions? extensions?
AnyAny algorithm can be shown computable with plain algorithm can be shown computable with plain XSLTXSLT– by simulating Turing machines, by a recursive named by simulating Turing machines, by a recursive named
template with string parameters template with string parameters (see 2005 lecture notes, or References)(see 2005 lecture notes, or References)
SDPL 2006 XSLT: Additional Features and Computing
38
What does this mean?What does this mean?
XSLT has XSLT has full algorithmic powerfull algorithmic power– (It is "Turing-complete")(It is "Turing-complete")– 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 automatically transformations automatically (( the "halting problem" has no algorithmic solution) the "halting problem" has no algorithmic solution)
» could attempt "denial-of-service" attacks with non-terminating could attempt "denial-of-service" attacks with non-terminating style sheets style sheets