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

Re: [xsl] number continuation problem


Subject: Re: [xsl] number continuation problem
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 27 Jun 2003 15:45:39 +0100

David wrote:
>> The way Ive resolved it is to build a temporary tree that flattens
>> out the hierarchy to a single level,
>
> yes I was going to suggest the same (unless Jeni rises to the
> challenge of doing it in one pass:-)

Well, I think you can do it with the following, though it's a bit
tedious if you have a big document because you need to work out the
number of the previous node to work out this one:

<xsl:template match="node" mode="number">
  <xsl:choose>
    <xsl:when test="preceding::node">
      <xsl:variable name="number">
        <xsl:choose>
          <xsl:when test="not(preceding-sibling::node)">
            <xsl:choose>
              <xsl:when test="parent::if">
                <xsl:variable name="else" select="preceding::node[1]/parent::else" />
                <xsl:choose>
                  <xsl:when test="$else">
                    <xsl:variable name="if-number">
                      <xsl:apply-templates select="$else/preceding-sibling::if[1]/node[last()]"
                                           mode="number" />
                    </xsl:variable>
                    <xsl:variable name="else-number">
                      <xsl:apply-templates select="preceding::node[1]"
                                           mode="number" />
                    </xsl:variable>
                    <xsl:choose>
                      <xsl:when test="$if-number > $else-number">
                        <xsl:value-of select="$if-number" />
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="$else-number" />
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:apply-templates select="preceding::node[1]" mode="number" />
                  </xsl:otherwise>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>
                <xsl:variable name="number">
                  <xsl:apply-templates
                  select="parent::else/preceding-sibling::if[1]/node[1]" mode="number" />
                </xsl:variable>
                <xsl:value-of select="$number - 1" />
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="preceding::node[1]" mode="number" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:value-of select="$number + 1"/>
    </xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>
</xsl:template>

I was thinking about a recursive solution that would step through the
<node> elements one-by-one, but the nested <if>s make it pretty
difficult.

Anyway, I'm sure that flattening is a much better approach...

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



Current Thread