5.2 computing with xslt

16
SDPL 2002 Notes 5.2: Computing with X SLT 1 5.2 Computing with XSLT 5.2 Computing with XSLT XSLT is principally a declarative XSLT is principally a declarative rule-based language rule-based language Can we express procedural computations with Can we express procedural computations with XSLT? XSLT? 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 selected source nodes ( iteration over selected source nodes ( xsl:for- xsl:for- each each ) ) conditional evaluation ( conditional evaluation ( xsl:if xsl:if and and xsl:choose xsl:choose ) )

Upload: axel-mclaughlin

Post on 31-Dec-2015

43 views

Category:

Documents


3 download

DESCRIPTION

5.2 Computing with XSLT. XSLT is principally a declarative rule-based language Can we express procedural computations with XSLT? What is the exact computational power of XSLT? We've seen some programming-like features: iteration over selected source nodes ( xsl:for-each ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 1

5.2 Computing with XSLT5.2 Computing with XSLT

XSLT is principally a declarative rule-based XSLT is principally a declarative rule-based languagelanguage– Can we express procedural computations with XSLT?Can we express procedural computations with XSLT?– 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 selected source nodes (iteration over selected source nodes (xsl:for-eachxsl:for-each))– conditional evaluation (conditional evaluation (xsl:if xsl:if andand xsl:choose xsl:choose))

Page 2: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 2

Computing with XSLTComputing with XSLT

Further programming-like features:Further programming-like features:– limitedlimited variablesvariables (names bound to values): (names bound to 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)"" />/>… …

– explicitely callableexplicitely callable named templates named templates with with parametersparameters:: <xsl:call-template <xsl:call-template name="process-name"name="process-name">>

<xsl:with-param <xsl:with-param name="pname"name="pname" select="select="$LAndF$LAndF"" /> /> </xsl:call-template> </xsl:call-template>

… …</xsl:for-each></xsl:for-each>

Page 3: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 3

A Real-Life ExampleA Real-Life Example

We used LaTex to format an XML article. For this, source We used LaTex to format an XML article. For this, source table structurestable structures

<tgroup cols="3"><tgroup cols="3">......

</tgroup> </tgroup>had to be mapped to corresponding LaTex environments:had to be mapped 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?

Page 4: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 4

Solution (1/2)Solution (1/2)

Pass the column count to a named template which Pass the column count to a named template which generates an appropriate number of ‘l’s:generates an appropriate number of ‘l’s:

<xsl:template match="tgroup"><xsl:template match="tgroup">

\begin{tabular}{\begin{tabular}{<xsl:call-template name="<xsl:call-template name="gen-colsgen-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>}}

<xsl:apply-templates /><xsl:apply-templates />

\end{tabular}\end{tabular}

</xsl:template></xsl:template>

Page 5: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 5

Solution 2/2: Recursive Solution 2/2: Recursive gen-colsgen-cols

<xsl:template name="<xsl:template name="gen-colsgen-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="<xsl:call-template name="gen-colsgen-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>

Page 6: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 6

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 may provide extension mechanisms, Implementations may provide extension mechanisms,

e.g., to call arbitrary Java methodse.g., to call arbitrary Java methods– Are there limits to XSLT processing that we can do Are there limits to XSLT processing that we can do

without extensions?without extensions?

We can show that We can show that anyany algorithmic computation algorithmic computation can be simulated with XSLTcan be simulated with XSLT– shown indirectly, through simulating Turing machines by shown indirectly, through simulating Turing machines by

XSLTXSLT

Page 7: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 7

Turing machineTuring machine

Alan Turing 1936/37Alan Turing 1936/37 formal model of algorithmsformal model of algorithms primitive but powerful to simulate any primitive but powerful to simulate any

computation expressible in any algorithmic model computation expressible in any algorithmic model ((Church/Turing thesisChurch/Turing thesis))

Turing machineTuring machine– A finite set of A finite set of statesstates– unlimited unlimited tapetape of cells for symbols, examined by a tape of cells for symbols, examined by a tape

headhead

Page 8: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 8

Illustration of a Turing MachineIllustration of a Turing Machine

00 11 00001111 11

state-basedstate-basedcontrolcontrol

Page 9: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 9

Control of a Turing machineControl of a Turing machine

Control defined by a Control defined by a transition functiontransition function ((qq11, , aa) = () = (qq22, , b, db, d), where ), where dd {left, right} {left, right}

– Meaning: with current state Meaning: with current state qq11 and tape symbol and tape symbol a,a,

» move to new state move to new state qq22

» write new symbol 'write new symbol 'bb' at the place of '' at the place of 'a'a'» move tape-head one step in direction move tape-head one step in direction dd

Such control can be simulated in XSLT with a Such control can be simulated in XSLT with a recursive named-template; Call it recursive named-template; Call it transitiontransition

Page 10: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 10

The “The “transitiontransition” template” template

Parameters:Parameters:– statestate: the current state: the current state– leftleft: contents of the tape up to the tape head: contents of the tape up to the tape head– rightright: contents of the tape starting at the cell : contents of the tape starting at the cell

pointed by the tape headpointed by the tape head TransitionTransition simulates a single transition step; simulates a single transition step;

calls itself with updated parameterscalls itself with updated parameters

Page 11: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 11

Overall structure of the simulationOverall structure of the simulation

<xsl:template name="transition"><xsl:template name="transition"><!-- parameters and trace output omitted --><!-- parameters and trace output omitted --><xsl:choose><xsl:choose><xsl:when test="$state='YES'"><xsl:when test="$state='YES'">

<ACCEPT /><ACCEPT /></xsl:when></xsl:when><!-- an xsl:when for simulating each defined <!-- an xsl:when for simulating each defined single transition comes here ... -->single transition comes here ... -->

<xsl:otherwise><xsl:otherwise><REJECT /><REJECT />

</xsl:when></xsl:when></xsl:choose></xsl:choose>

</ xsl:template></ xsl:template>

Page 12: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 12

Updating the representation of the tapeUpdating the representation of the tape

For each For each rightright-move -move ((qq11, , aa) = () = (qq22, , bb, right), , right),

concatenate concatenate 'b''b' at the end of at the end of $left$left and drop and drop the first character of the first character of $right$right

LeftLeft-moves -moves ((qq11, , aa) = () = (qq22, , bb, left) in a similar , left) in a similar

manner: manner: – drop the last character of drop the last character of $left, $left, and concatenate it and concatenate it

in front of in front of $right$right whose first character has been whose first character has been replaced by 'b'replaced by 'b'

Example: a TM for palindromes over alphabet Example: a TM for palindromes over alphabet {a, b}; ('#' used for denoting blank tape slots){a, b}; ('#' used for denoting blank tape slots)

Page 13: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 13

Simulating a single transition (1/2)Simulating a single transition (1/2)

<!-- sigma(mark,a) = (move_a,#,right): --><!-- sigma(mark,a) = (move_a,#,right): -->

<xsl:when test="$state='mark' and <xsl:when test="$state='mark' and substring($right, 1, 1)='a'"> substring($right, 1, 1)='a'">

<!-- First update the parameters: --> <!-- First update the parameters: -->

<xsl:variable name="newstate" <xsl:variable name="newstate" select="'move_a'"/> select="'move_a'"/>

<xsl:variable name="newleft" <xsl:variable name="newleft" select="concat($left, '#')"/> select="concat($left, '#')"/>

<xsl:variable name="newright" <xsl:variable name="newright" select="substring($right, 2)"/> select="substring($right, 2)"/>

Page 14: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 14

Simulating a single transition (2/2)Simulating a single transition (2/2)

<!-- Then call "transition" with new params: --><!-- Then call "transition" with new params: -->

<xsl:call-template name="transition"><xsl:call-template name="transition">

<xsl:with-param name="state" <xsl:with-param name="state" select="$newstate"/>select="$newstate"/>

<xsl:with-param name="left" <xsl:with-param name="left" select= "$newleft"/>select= "$newleft"/>

<xsl:with-param name = "right" <xsl:with-param name = "right" select="$newright"/>select="$newright"/>

</xsl:call-template></xsl:call-template>

</xsl:when></xsl:when>

Page 15: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 15

Sample trace of the simulationSample trace of the simulation

$ saxon dummy.xml tm-palindr.xsl input-tape=aba$ saxon dummy.xml tm-palindr.xsl input-tape=aba<M state="#[mark]aba#"/><M state="#[mark]aba#"/><M state="##[move_a]ba#"/><M state="##[move_a]ba#"/><M state="##b[move_a]a#"/><M state="##b[move_a]a#"/><M state="##ba[move_a]#"/><M state="##ba[move_a]#"/><M state="##b[test_a]a#"/><M state="##b[test_a]a#"/><M state="##[return]b##"/><M state="##[return]b##"/><M state="#[return]#b##"/><M state="#[return]#b##"/><M state="##[mark]b##"/><M state="##[mark]b##"/><M state="###[move_b]##"/><M state="###[move_b]##"/><M state="##[test_b]###"/><M state="##[test_b]###"/><M state="#[YES]####"/><M state="#[YES]####"/><ACCEPT/><ACCEPT/>

state shown asstate shown astape-lefttape-left[[statestate]]tape-righttape-right

Page 16: 5.2 Computing with XSLT

SDPL 2002 Notes 5.2: Computing with XSLT 16

What does this mean?What does this mean?

XSLT has XSLT has full algorithmic programming powerfull algorithmic programming 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)

» Malicious hacker could cause "denial-of-service" through non-Malicious hacker could cause "denial-of-service" through non-terminating style sheets terminating style sheets