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

Re: [xsl] Traversing the tree


Subject: Re: [xsl] Traversing the tree
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Sat, 10 Sep 2005 22:19:00 +0530

Hi Anupam,
   Sorry that I am answering late in this thread. I found this problem
interesting. Your requirement has also changed from the problem
description stated in your 1st mail of this thread. The below
stylesheet solves the original problem you described, and not the
changed requirement. I hope it helps a bit..

This is the XSLT 2.0 solution, tested with Saxon 8.5.1

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:func="http://whatever"
                version="2.0">

<xsl:output method="text" />

<xsl:template match="/top">
   <xsl:apply-templates select="a" />
</xsl:template>

<xsl:template match="a">
   <xsl:for-each select="reg">
     <xsl:variable name="n" select="func:getnum(., -1)" />
     <xsl:value-of select="." /> : <xsl:value-of select="$n"
/><xsl:text>&#xa;</xsl:text>
   </xsl:for-each>
</xsl:template>

<xsl:function name="func:getnum" as="xs:double">
   <xsl:param name="node" as="element()?" />
   <xsl:param name="x" as="xs:double" />

   <xsl:choose>
     <xsl:when test="$node/@offset">
       <xsl:sequence select="$node/@offset" />
     </xsl:when>
     <xsl:when test="$node">
       <xsl:sequence select="
                     if ($node/preceding-sibling::reg[1]/@offset)
                     then
                       1 + $node/preceding-sibling::reg[1]/@offset
                     else
                       1+ func:getnum($node/preceding-sibling::reg[1], -1)"
       />
     </xsl:when>
     <xsl:otherwise>
       <xsl:sequence select="$x" />
     </xsl:otherwise>
   </xsl:choose>
</xsl:function>

</xsl:stylesheet>

Regards,
Mukul

On 9/9/05, Agnisys Technology (P) Ltd. <agnisys@xxxxxxxxx> wrote:
> Hi,
>  This is probably simple for the experts of the group but I can't figure
this out. Problem I got
> stuck on was how to find the closest preceding-sibling node with an offset
attribute, then count
> the nodes that don't have the attribute since that node.
>
> Input :
> <top>
>     <a>
>          <reg                > A1 </reg>
>          <reg                > A2 </reg>
>          <reg  offset="10"   > A3 </reg>
>          <reg                > A4 </reg>
>          <reg                > A5 </reg>
>          <reg  offset="24"   > A6 </reg>
>          <reg                > A7 </reg>
>      </a>
>      <a>
>          <reg  offset="6"> A8 </reg>
>          <reg            > A9 </reg>
>      </a>
>      <a>
>          <reg            > A10 </reg>
>      </a>
>  </top>
>
> Output:
> A1 : 0
> A2 : 1
> A3 : 10
> A4 : 11
> A5 : 12
> A6 : 24
> A7 : 25
> A8 : 6
> A9 : 7
> A10: 0
>
> So, start with 0 and count unless @offset is encountered, if so, jump to the
offset and start
> counting again.
> Offsets are guaranteed to be monotonically increasing, although I do need to
create a test
> elsewhere that verifies that it does.
>
> Thanks,
> Anupam.


Current Thread
Keywords