[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: number continuation problem

Subject: Re: number continuation problem
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 27 Jun 2003 15:45:39 +0100
xsl test if else
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.