sdpl 2006xslt: additional features and computing1 5.1 additional features n xpath support for...

38
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

Upload: leon-rodgers

Post on 29-Dec-2015

224 views

Category:

Documents


0 download

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

SDPL 2006 XSLT: Additional Features and Computing

2

XPath: Arithmetical OperationsXPath: Arithmetical Operations

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.):

» format-number(-1.2534, format-number(-1.2534, ""0.00.0"") = ) = ""-1.3-1.3""

– XSLTXSLT function; applies Java decimal format patterns function; applies Java decimal format patterns

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

(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 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

– 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"– 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"

– string-length("dogbert")=7string-length("dogbert")=7– translate(translate(Str, Replaced, ReplacingStr, Replaced, Replacing):):

» translate("translate("doggdoggy","y","dgodgo","Ssi")="Sissy"","Ssi")="Sissy"

SDPL 2006 XSLT: Additional Features and Computing

7

Generating TextGenerating 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-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

<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 2006 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: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 }}

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 2006 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 2006 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 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"/>

</BODY> </HTML> </BODY> </HTML></xsl:template></xsl:template>

SDPL 2006 XSLT: Additional Features and Computing

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

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 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 test="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:ifxsl:if test="test="position() &lt; last()position() &lt; 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

<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 ""a,b,c,da,b,c,d" " ( (←← 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" />

it yieldsit yields ""a,ba,b"" andand ""c,dc,d"" (Clever, and tricky!) (Clever, and tricky!)

SDPL 2006 XSLT: Additional Features and Computing

19

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 '<!-- 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 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] )">

<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 2006 XSLT: Additional Features and Computing

21

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: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

<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 2006 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 2006 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 2006 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 2006 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 2006 XSLT: Additional Features and Computing

27

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)

<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 2006 XSLT: Additional Features and Computing

28

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

select="select="concat(last, ', ', first)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 2006 XSLT: Additional Features and Computing

31

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 2006 XSLT: Additional Features and Computing

32

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 2006 XSLT: Additional Features and Computing

33

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 inelegantOK, 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 2006 XSLT: Additional Features and Computing

34

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 2006 XSLT: Additional Features and Computing

35

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 2006 XSLT: Additional Features and Computing

36

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"/> <!-- default --> <!-- default -->

<xsl:template match="/"><xsl:template 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

SDPL 2006 XSLT: Additional Features and Computing

37

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