Subject: RE: [xsl] Calculating cumulative values From: "Simon Shutter" <simon@xxxxxxxxxxx> Date: Sat, 3 Feb 2007 14:45:26 -0800 |
That's brilliant Andrew. If I needed to perform a sort on x and y1 first, could I do it in the same stylesheet? Simon -----Original Message----- From: Andrew Welch [mailto:andrew.j.welch@xxxxxxxxx] Sent: February 3, 2007 1:59 PM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] Calculating cumulative values On 2/3/07, Simon Shutter <simon@xxxxxxxxxxx> wrote: > Hello, > > Here is a tricky (to me) problem that requires looping over several > sets and calculating cumulative values (y2) from x and y1. That is, > for each x, set > y2 to be equal to the cumulative value of y1 as shown in the example > output below. > > I made my best attempt with the stylesheet below, which I know is > quite incorrect but wanted to show I did actually try to solve the > problem. I am now bogged down with the use of the variable element. > > Any assistance would be greatly appreciated, > > Simon > > --------------------------------------------- > Input > --------------------------------------------- > > <root id="theroot"> > <set id="1"> > <point x="1" y1="2" /> > <point x="1" y1="3" /> > <point x="1" y1="0" /> > <point x="1" y1="2" /> > <point x="1" y1="2" /> > <point x="2" y1="3" /> > <point x="2" y1="0" /> > <point x="2" y1="2" /> > <point x="3" y1="2" /> > <point x="3" y1="3" /> > <point x="3" y1="1" /> > <point x="3" y1="2" /> > <point x="3" y1="2" /> > </set> > <set id="2"> > <point x="1" y1="2" /> > <point x="1" y1="3" /> > <point x="1" y1="0" /> > <point x="1" y1="2" /> > <point x="2" y1="2" /> > <point x="3" y1="2" /> > <point x="3" y1="2" /> > <point x="3" y1="2" /> > </set> > <set id="n"> > <point x="1" y1="2" /> > <point x="1" y1="3" /> > <point x="1" y1="2" /> > <point x="2" y1="3" /> > <point x="2" y1="0" /> > <point x="2" y1="2" /> > <point x="3" y1="3" /> > </set> > </root> > > --------------------------------------------- > Desired output > --------------------------------------------- > <root id="theroot"> > <set id="1"> > <point x="1" y1="2" y2="2"/> > <point x="1" y1="3" y2="5"/> > <point x="1" y1="0" y2="5"/> > <point x="1" y1="2" y2="7"/> > <point x="1" y1="2" y2="9"/> > <point x="2" y1="3" y2="3"/> > <point x="2" y1="0" y2="3"/> > <point x="2" y1="2" y2="5"/> > <point x="3" y1="2" y2="2"/> > <point x="3" y1="3" y2="5"/> > <point x="3" y1="1" y2="6"/> > <point x="3" y1="2" y2="8"/> > <point x="3" y1="2" y2="10"/> > </set> > <set id="2"> > <point x="1" y1="2" y2="2"/> > <point x="1" y1="3" y2="5"/> > <point x="1" y1="0" y2="5"/> > <point x="1" y1="2" y2="7"/> > <point x="2" y1="2" y2="2"/> > <point x="3" y1="2" y2="2"/> > <point x="3" y1="2" y2="4"/> > <point x="3" y1="2" y2="6"/> > </set> > <set id="n"> > <point x="1" y1="2" y2="2"/> > <point x="1" y1="3" y2="5"/> > <point x="1" y1="2" y2="7"/> > <point x="2" y1="3" y2="3"/> > <point x="2" y1="0" y2="3"/> > <point x="2" y1="2" y2="5"/> > <point x="3" y1="3" y2="3"/> > </set> > </root> > > --------------------------------------------- > Prototype stylesheet - just to show I tried > --------------------------------------------- > <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> > <xsl:variable name="tempy" select="5"/> > <!-- Convert root and all elements tags 'as is' --> <xsl:template > match="@*|node()"> > <xsl:copy> > <xsl:apply-templates select="@*|node()"/> > </xsl:copy> > </xsl:template> > <xsl:template match="//point"> > <xsl:copy> > <xsl:copy-of select="@*"/> > <xsl:for-each select="@y1"> > <xsl:attribute name="y2"> > <xsl:choose> > <xsl:when test=". > $tempy"> > <xsl:value-of select="$tempy * ."/> > </xsl:when> > <xsl:otherwise> > <xsl:value-of select="$tempy + ."/> > </xsl:otherwise> > </xsl:choose> > </xsl:attribute> > </xsl:for-each> > </xsl:copy> > </xsl:template> > </xsl:stylesheet> You can just use XPath here, no need for recursion: <xsl:template match="point"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:attribute name="y2"> <xsl:value-of select="sum(./@y1|preceding-sibling::point[@x = current()/@x]/@y1)"/> </xsl:attribute> </xsl:copy> </xsl:template> cheers andrew
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Calculating cumulative va, Andrew Welch | Thread | Re: [xsl] Calculating cumulative va, Dimitre Novatchev |
Re: [xsl] Calculating cumulative va, Andrew Welch | Date | Re: [xsl] Calculating cumulative va, Dimitre Novatchev |
Month |