[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] RE: Is xsl:for-each "syntactic sugar"?


Subject: Re: [xsl] RE: Is xsl:for-each "syntactic sugar"?
From: David Carlisle <davidc@xxxxxxxxx>
Date: Sat, 08 May 2010 00:06:18 +0100

On 07/05/2010 23:34, Costello, Roger L. wrote:
I do not believe that this task can be accomplished using xsl:for-each.

why not?


> Do you agree?

No.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<xsl:template match="BankTransactions"> <xsl:for-each select="Transaction"> <xsl:value-of select="sum(.|preceding-sibling::*),' '"/> </xsl:for-each> </xsl:template>

</xsl:stylesheet>


$ saxon9 rc.xml rc.xsl <?xml version="1.0" encoding="UTF-8"?>95 92.5 102.5 95

It's (probably, unless Michael rewrites it aggressively behind the scenes) an algorithm with theoretically greater computational complexity than the versions using + (order n squared instead of linear) but it's a lot easier to read and write and unless there are hundreds of siblings the complexity may not matter,


<xsl:if test="following-sibling::Transaction">
<xsl:apply-templates select="following-sibling::Transaction[1]">



the xsl:if isn't needed as if the test is false the apply-templates wouldn't do anything anyway.


As Dimitre has already said, you can't give general advice about which kind of algorithm is superior, it depends a lot on whether the system does tail recursion elimination on the explicitly recursive forms, so like most optimisation questions the answers are system specific.

David


Current Thread