1 / 16

5.2 Computing with XSLT

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 )

Download Presentation

5.2 Computing with XSLT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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) • conditional evaluation (xsl:if and xsl:choose) Notes 5.2: Computing with XSLT

  2. Computing with XSLT • Further programming-like features: • limitedvariables (names bound to values): <xsl:for-each select="//name"> <xsl:variable name="LAndF" select="concat(last, ', ', first)"/> … • explicitely callable named templates withparameters:<xsl:call-template name="process-name"> <xsl:with-param name="pname"select="$LAndF" /> </xsl:call-template> …</xsl:for-each> Notes 5.2: Computing with XSLT

  3. A Real-Life Example • We used LaTex to format an XML article. For this, source table structures<tgroup cols="3"> ... </tgroup>had to be mapped to corresponding LaTex environments:\begin{tabular}{lll} %3 left-justified cols ... \end{tabular} • How to do this? Notes 5.2: Computing with XSLT

  4. Solution (1/2) • Pass the column count to a named template which generates an appropriate number of ‘l’s:<xsl:template match="tgroup"> \begin{tabular}{<xsl:call-template name="gen-cols"> <xsl:with-param name="count" select="@cols" /> <xsl:with-param name="symb" select="'l'" /> </xsl:call-template>} <xsl:apply-templates /> \end{tabular} </xsl:template> Notes 5.2: Computing with XSLT

  5. Solution 2/2: Recursive gen-cols <xsl:template name="gen-cols"> <xsl:param name="count" /> <xsl:param name="symb" /> <xsl:if test="$count > 0"> <xsl:value-of select="$symb" /> <xsl:call-template name="gen-cols"> <xsl:with-param name="count" select="$count - 1"/> <xsl:with-param name="symbol" select="$symbol"/> </xsl:call-template> </xsl:if> </xsl:template> Notes 5.2: Computing with XSLT

  6. Computational power of XSLT • XSLT seems quite powerful, but how powerful is it? • Implementations may provide extension mechanisms, e.g., to call arbitrary Java methods • Are there limits to XSLT processing that we can do without extensions? • We can show that any algorithmic computation can be simulated with XSLT • shown indirectly, through simulating Turing machines by XSLT Notes 5.2: Computing with XSLT

  7. Turing machine • Alan Turing 1936/37 • formal model of algorithms • primitive but powerful to simulate any computation expressible in any algorithmic model (Church/Turing thesis) • Turing machine • A finite set of states • unlimited tape of cells for symbols, examined by a tape head Notes 5.2: Computing with XSLT

  8. Illustration of a Turing Machine 1 0 1 1 1 0 0 state-based control Notes 5.2: Computing with XSLT

  9. Control of a Turing machine • Control defined by a transition functions:s(q1, a) = (q2, b, d), where de {left, right} • Meaning: with current state q1 and tape symbol a, • move to new state q2 • write new symbol 'b' at the place of 'a' • move tape-head one step in direction d • Such control can be simulated in XSLT with a recursive named-template; Call it transition Notes 5.2: Computing with XSLT

  10. The “transition” template • Parameters: • state: the current state • left: contents of the tape up to the tape head • right: contents of the tape starting at the cell pointed by the tape head • Transition simulates a single transition step; calls itself with updated parameters Notes 5.2: Computing with XSLT

  11. Overall structure of the simulation <xsl:template name="transition"> <!-- parameters and trace output omitted --> <xsl:choose> <xsl:when test="$state='YES'"> <ACCEPT /> </xsl:when> <!-- an xsl:when for simulating each defined single transition comes here ... --> <xsl:otherwise> <REJECT /> </xsl:when> </xsl:choose> </ xsl:template> Notes 5.2: Computing with XSLT

  12. Updating the representation of the tape • For each right-move s(q1, a) = (q2, b, right), concatenate 'b' at the end of $left and drop the first character of $right • Left-moves s(q1, a) = (q2, b, left) in a similar manner: • drop the last character of $left, and concatenate it in front of $right whose first character has been replaced by 'b' • Example: a TM for palindromes over alphabet {a, b}; ('#' used for denoting blank tape slots) Notes 5.2: Computing with XSLT

  13. Simulating a single transition (1/2) <!-- sigma(mark,a) = (move_a,#,right): --> <xsl:when test="$state='mark' and substring($right, 1, 1)='a'"> <!-- First update the parameters: --> <xsl:variable name="newstate" select="'move_a'"/> <xsl:variable name="newleft" select="concat($left, '#')"/> <xsl:variable name="newright" select="substring($right, 2)"/> Notes 5.2: Computing with XSLT

  14. Simulating a single transition (2/2) <!-- Then call "transition" with new params: --> <xsl:call-template name="transition"> <xsl:with-param name="state" select="$newstate"/> <xsl:with-param name="left" select= "$newleft"/> <xsl:with-param name = "right" select="$newright"/> </xsl:call-template> </xsl:when> Notes 5.2: Computing with XSLT

  15. Sample trace of the simulation $ saxon dummy.xml tm-palindr.xsl input-tape=aba <M state="#[mark]aba#"/> <M state="##[move_a]ba#"/> <M state="##b[move_a]a#"/> <M state="##ba[move_a]#"/> <M state="##b[test_a]a#"/> <M state="##[return]b##"/> <M state="#[return]#b##"/> <M state="##[mark]b##"/> <M state="###[move_b]##"/> <M state="##[test_b]###"/> <M state="#[YES]####"/> <ACCEPT/> state shown as tape-left[state]tape-right Notes 5.2: Computing with XSLT

  16. What does this mean? • XSLT has full algorithmic programming power • (It is "Turing-complete") • Is this intentional? • Inconvenient as a general-purpose programming language! • Impossible to recognise non-terminating transformations automatically (<- the "halting problem" has no algorithmic solution) • Malicious hacker could cause "denial-of-service" through non-terminating style sheets Notes 5.2: Computing with XSLT

More Related