sdpl 2007xslt: additional features and computing1 5.1 additional features n xpath for arithmetics,...
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.):
» format-number(-1.2534, format-number(-1.2534, ""0.00.0"") = ) = ""-1.3-1.3""
– XSLTXSLT 1.0 function; uses Java decimal format patterns 1.0 function; uses Java decimal format patterns
SDPL 2007 XSLT: Additional Features and Computing
3
Aggregate FunctionsAggregate Functions
Counting nodes Counting nodes » count(count(node-setnode-set))
– 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
(See an example later)(See an example later)
Examples:Examples:– id('sect:intro')id('sect:intro')
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
– substring-before("ftp:substring-before("ftp:////a", "a", "////") = ") = substring-before("ftp:substring-before("ftp:///a", "/a", "//") = "ftp:"") = "ftp:"
– substring-after("ftp:substring-after("ftp:///a", "/a", "//")= "/a"")= "/a"– string-length("dogbert")=7string-length("dogbert")=7– substring(substring(string, start, length?string, start, length?):):
» substring("dogbert", 1, 3) = "dog"substring("dogbert", 1, 3) = "dog"» substring("dogbert", 3) = "gbert"substring("dogbert", 3) = "gbert"
– translate(translate(Str, Replaced, ReplacingStr, Replaced, Replacing):):» translate("translate("doggdoggy","y","dgodgo","Ssi") = "Sissy"","Ssi") = "Sissy"
SDPL 2007 XSLT: Additional Features and Computing
7
Generating TextGenerating Text
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
<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 Charlie ("("BirdBird")") Parker Parker
SDPL 2007 XSLT: Additional Features and Computing
8
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 }}
Example: Transform source elementExample: Transform 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 2007 XSLT: Additional Features and Computing
10
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/<img src="/images/{file}{file}""width="width="{size/@width}{size/@width}" />" />
</xsl:template></xsl:template> ExpressionsExpressions {file}{file} andand {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 2007 XSLT: Additional Features and Computing
11
XSLT: RepetitionXSLT: Repetition
Nodes can be "pulled" from source for Nodes can be "pulled" from source for processing using processing using
<<xsl:for-eachxsl:for-each select=" select="ExprExpr">">TemplateTemplate
</</xsl:for-eachxsl:for-each>>
– 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"/>
</BODY> </HTML> </BODY> </HTML></xsl:template></xsl:template>
SDPL 2007 XSLT: Additional Features and Computing
14
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
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 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
<namelist><name>a</name><name>b</name></namelist> <namelist><name>a</name><name>b</name></namelist> <namelist><name>c</name><name>d</name></namelist><namelist><name>c</name><name>d</name></namelist>
by invocationby invocation
<xsl:apply-templates select="//name" /><xsl:apply-templates select="//name" /> yieldsyields ""aa,,bb,,cc,,dd" " ( (←← single node list); single node list);
With invocation fromWith invocation from <xsl:template match="namelist"><xsl:template match="namelist">
<xsl:apply-templates select="name" /><xsl:apply-templates select="name" />
we'd getwe'd get ""aa,,bb"" andand ""cc,,dd"" (Clever, and tricky!) (Clever, and tricky!)
SDPL 2007 XSLT: Additional Features and Computing
19
Conditional processing (2)Conditional processing (2)
A case construct (A case construct ( switchswitch in Java): in Java): <xsl:choose><xsl:choose>
<!-- The first '<!-- The first 'whenwhen' whose ' whose test=true()test=true() is is instantiated: -->instantiated: -->
<xsl:when test="<xsl:when test="ExprExpr11">"> … … </xsl:when></xsl:when>
<xsl:when test="<xsl:when test="ExprExpr22">"> … … </xsl:when></xsl:when>… …
<!-- 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
last=current()/last] )">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 2007 XSLT: Additional Features and Computing
21
Numbering Document ContentsNumbering Document Contents
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
<xsl:template match="ol/item"><xsl:template match="ol/item"> <!-- default: count similar <!-- default: count similar siblingssiblings (items) --> (items) --> <xsl:number format="1. "/><xsl:number 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 2007 XSLT: Additional Features and Computing
23
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
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 2007 XSLT: Additional Features and Computing
25
Example 2: VariationExample 2: Variation
As above, but number titles within appendices As above, but number 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"> <xsl:number level="multiple" <xsl:number 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 2007 XSLT: Additional Features and Computing
26
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 2007 XSLT: Additional Features and Computing
27
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)
<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>
SDPL 2007 XSLT: Additional Features and Computing
28
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
– conditional evaluation (conditional evaluation (xsl:xsl:ifif andand xsl: xsl:choosechoose))
SDPL 2007 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 name="LAndF" <xsl:variable name="LAndF"
select="concat(last, ', ', first)"select="concat(last, ', ', first)" />/> ......
... ...</xsl:for-each></xsl:for-each>
– callablecallable named templatesnamed templates withwith parametersparameters:: <xsl:call-template name="process-name"><xsl:call-template name="process-name">
<xsl:with-param name="pname" select="$LAndF" /> <xsl:with-param name="pname" select="$LAndF" /> </xsl:call-template> </xsl:call-template>
SDPL 2007 XSLT: Additional Features and Computing
31
Result Tree FragmentsResult Tree Fragments
Result tree fragmentsResult tree fragments built by templates can be stored, too: built by templates can be stored, too:
<xsl:variable name="fooBar"><xsl:variable name="fooBar"><FOO>BAR</FOO><FOO>BAR</FOO>
</xsl:variable></xsl:variable> They can only be used as string valuesThey can only be used as string values
substring($fooBar, 2, 2) = "AR"substring($fooBar, 2, 2) = "AR" or inserted in the result:or inserted in the result:
<xsl:copy-of select="$fooBar" /><xsl:copy-of select="$fooBar" />
(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:
<xsl:for-each select="//name"><xsl:for-each select="//name"> <xsl:variable name="LAndF" <xsl:variable name="LAndF"
select="concat(last, ', ', first)"select="concat(last, ', ', first)" />/> ... ... <xsl:call-template name="process-name"> <xsl:call-template name="process-name">
<xsl:with-param name="pname" <xsl:with-param name="pname" select="$LAndF" />select="$LAndF" /> </xsl:call-template> </xsl:call-template>
... ...
</xsl:for-each></xsl:for-each><TABLE> . . . </TABLE><TABLE> . . . </TABLE>
SDPL 2007 XSLT: Additional Features and Computing
33
A Real-Life ExampleA Real-Life Example
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
<tgroup cols="3"><tgroup cols="3">......
</tgroup> </tgroup>to corresponding LaTeX environments:to corresponding LaTeX environments:
\begin{tabular}{lll} %3 left-justified \begin{tabular}{lll} %3 left-justified colscols
... ... \end{tabular}\end{tabular}
How to do this? How to do this?
SDPL 2007 XSLT: Additional Features and Computing
34
Possible solution (for up to 4 columns)Possible solution (for up to 4 columns)
<xsl:template match="tgroup"><xsl:template match="tgroup">
\begin{tabular}{l\begin{tabular}{l
} }<xsl:apply-templates /><xsl:apply-templates />
\end{tabular}\end{tabular}
</xsl:template></xsl:template>
OK, but inelegant!OK, but inelegant! How to support arbitrarily many columns?How to support arbitrarily many columns?
<xsl:if test="@cols > 1">l</xsl:if<xsl:if test="@cols > 1">l</xsl:if ><xsl:if test="@cols > 2">l</xsl:if><xsl:if test="@cols > 2">l</xsl:if ><xsl:if test="@cols > 3">l</xsl:if>><xsl:if test="@cols > 3">l</xsl:if>
SDPL 2007 XSLT: Additional Features and Computing
35
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:
<xsl:template match="tgroup"><xsl:template match="tgroup">
\begin{tabular}{\begin{tabular}{
} } <xsl:apply-templates /><xsl:apply-templates />
\end{tabular}\end{tabular}
</xsl:template></xsl:template>
<xsl:call-template name="gen-cols"><xsl:call-template name="gen-cols"> <xsl:with-param name="count" select="<xsl:with-param name="count" select="@cols@cols" />" /> <xsl:with-param name="symb" select="'l'" /><xsl:with-param name="symb" select="'l'" /> </xsl:call-template></xsl:call-template>
SDPL 2007 XSLT: Additional Features and Computing
36
Solution 2/2: Recursive Solution 2/2: Recursive gen-colsgen-cols
<xsl:template name="gen-cols"><xsl:template name="gen-cols">
<xsl:param name="count" /><xsl:param name="count" />
<xsl:param name="symb" /><xsl:param name="symb" />
<xsl:if test="$count > 0"><xsl:if test="$count > 0"><xsl:value-of select="$symb" /><xsl:value-of select="$symb" /><xsl:call-template name="gen-cols"><xsl:call-template name="gen-cols">
<xsl:with-param name="count" <xsl:with-param name="count" select="$count - 1" />select="$count - 1" />
<xsl:with-param name="symbol" <xsl:with-param name="symbol" select="$symbol" />select="$symbol" />
</xsl:call-template></xsl:call-template>
</xsl:if></xsl:if>
</xsl:template></xsl:template>
formal parametersformal parameters
SDPL 2007 XSLT: Additional Features and Computing
37
Stylesheet ParametersStylesheet Parameters
Stylesheet can get params from command line, or through Stylesheet can get params from command line, or through JAXP with JAXP with
Transformer.setParameter(name, value)Transformer.setParameter(name, value) ::<xsl:transform ... ><xsl:transform ... >
<xsl:output method="text" /><xsl:output method="text" />
<xsl:param name="In" select="0" /><xsl:param name="In" select="0" /> <xsl:template <xsl:template match="/">match="/">
<xsl:value-of select="2*<xsl:value-of select="2*$In$In"/> </xsl:template>"/> </xsl:template>
</xsl:transform></xsl:transform>
$ java -jar saxon.jar dummy.xml double.xslt In=120$ java -jar saxon.jar dummy.xml double.xslt In=120
240240
default valuedefault value
SDPL 2007 XSLT: Additional Features and Computing
38
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(!)