SDPL 2007 XSLT: Additional Features a nd Computing 1 5.1 Additional Features 5.1 Additional Features XPath for arithmetics, cross- XPath for arithmetics, cross- references, and string manipulation references, and string manipulation Generating text Generating text – for content for content – for attribute values for attribute values Repetition, sorting and conditional Repetition, sorting and conditional processing processing Numbering document contents Numbering document contents 5.2 Computing with XSLT 5.2 Computing with XSLT
39
Embed
SDPL 2007XSLT: Additional Features and Computing1 5.1 Additional Features n XPath for arithmetics, cross-references, and string manipulation n Generating.
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 2007 XSLT: Additional Features and Computing
1
5.1 Additional Features 5.1 Additional Features
XPath for arithmetics, cross-references, and XPath for arithmetics, cross-references, and string manipulation string manipulation
Generating textGenerating text– for contentfor content– for attribute valuesfor attribute values
Repetition, sorting and conditional processingRepetition, sorting and conditional processing Numbering document contentsNumbering document contents
5.2 Computing with XSLT 5.2 Computing with XSLT
SDPL 2007 XSLT: Additional Features and Computing
2
XPath: ArithmeticsXPath: Arithmetics
Double-precision floating-point operators Double-precision floating-point operators ++, , --, , **, , divdiv, , modmod (same as (same as %% in Java)in Java)
» e.g. e.g. 2.3 mod 1.12.3 mod 1.1 ≈ 0.1≈ 0.1 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 observed temps below current node:Average of observed temps below current node:sum(sum(.//obs/@temperature.//obs/@temperature))
divdiv count( count(.//obs.//obs))
SDPL 2007 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 2007 XSLT: Additional Features and Computing
5
String manipulationString manipulation
Equality and inequality of strings with Equality and inequality of strings 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 2007 XSLT: Additional Features and Computing
6
XPath: more string functionsXPath: more string functions
Insert text nodes with a computed value in the result:Insert text nodes with a computed value in the result:<<xsl:value-ofxsl:value-of select=" select="ExprExpr" />" />
– if if ExprExpr gives a node-set, the gives a node-set, the value of the first nodevalue of the first node in in document order is used (in XSLT 2.0 all, space-separated)document order is used (in XSLT 2.0 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:<xsl:value-ofvalue-of select="first" /> select="first" />("("<xsl:<xsl:value-ofvalue-of select="@alias" /> select="@alias" />")") <xsl:<xsl:value-ofvalue-of select="last" /> 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 2007 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 2007 XSLT: Additional Features and Computing
12
Example (of Example (of 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>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 2007 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: <xsl:for-eachfor-each select="//section/title"> select="//section/title"> <LI><xsl:apply-templates /></LI> <LI><xsl:apply-templates /></LI> </xsl: </xsl:for-eachfor-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-refs can also be processed usingCross-refs can also be processed using 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)"> <!-- just one selected --><!-- just one selected --> 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 2007 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, like, like– 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 2007 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 2007 XSLT: Additional Features and Computing
17
Conditional processingConditional processing
A template can be instantiated or ignored withA template can be instantiated or ignored with<xsl:if<xsl:if test="test="BooleanExprBooleanExpr">">
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:ifxsl:if test="test="position() < last()position() < last()"" > >,, </</xsl:ifxsl:if>></xsl:template></xsl:template>
SDPL 2007 XSLT: Additional Features and Computing
18
An aside: Meaning of An aside: Meaning of position()position()
Evaluation wrt the current node list. The above applied to Evaluation wrt the current node list. The above applied to
<!-- 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 2007 XSLT: Additional Features and Computing
20
Example (cont; Eliminating duplicate names)Example (cont; Eliminating duplicate names)
Only the Only the current()current() node accessible in current node list node accessible in current node 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 and
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– supports common numbering schemes : single-level, supports common numbering schemes : single-level,
hierarchical, and sequential through levelshierarchical, and sequential through 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 2007 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 2007 XSLT: Additional Features and Computing
24
Generating numbers: Example 2Generating numbers: Example 2
Generating numbers: Example 3Generating numbers: Example 3
Sequential numbering of notesSequential numbering of notes withinwithin chapterschapters::(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 chap chap
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 2007 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 XML transformationsfor XML transformations– Could we use it for general computing?Could we use it for general 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:xsl:for-eachfor-each))
» in XSLT 2.0 iteration over arbitrary sequencesin XSLT 2.0 iteration over arbitrary sequences
(XSLT 2.0 allows unlimited processing of (computed) (XSLT 2.0 allows unlimited processing of (computed) sequences)sequences)
SDPL 2007 XSLT: Additional Features and Computing
32
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 2007 XSLT: Additional Features and Computing
39
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?
» Awkward as a general-purpose programming language!Awkward 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(!)