Re: [xsl] Trouble using recursion to make a Counter

Subject: Re: [xsl] Trouble using recursion to make a Counter
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 29 Jul 2004 17:58:25 -0400
Hi,

This can be accomplished with recursion, but why? It is generally much easier to do either of two things to get this kind of output:

* Each time you match an item that you want, count all the items you want that precede it and add 1.
* Or, simply use <xsl:number/>, which does this for you. "Unique sequential numbers" is exactly what it gives you.


For example, you could say:

<xsl:template match="Level1"/>
  <xsl:number count="Level1|Level2|Level3" level="any"/>
  <xsl:apply-templates select="Level1Desc"/>
  <xsl:apply-templates select="Level2"/>
</xsl:template>

etc. (Any good reference will tell you details about xsl:number, which is very versatile.)

(BTW, if this truly is a recursive structure you'd find it easier to name everything "Level", "Desc" and so on, not hard-coding the particular level you are on but letting XSLT take care of that when necessary.)

If you could clarify your requirements we'd have more confidence as to whether recursion is necessary. In any case (even if it proves to be) it's hard to help you figure it out without any indication of what you've tried so far.

Cheers,
Wendell

At 05:51 PM 7/29/2004, you wrote:
Hello!  I've been struggling with something that seemed very simple when I
first started looking at it, but has become frustrating.  I have hit the
archives and google, yet have found nothing to spark my imagination.  All I
need is to process my xml and for each node that I process increment and
print a counter to the result file.

I have data that looks like this:

<level1>
      <Level1Desc>Neighborhood</Level1Desc>
      <Level1ID>1</Level1ID>
      <Level2>
            <Level2Desc>Street</Level2Desc>
            <Level2ID>100</Level2ID>
            <Level3>
                  <Level2Desc>House</Level2Desc>
                  <Level2ID>1000</Level2ID>
            </Level3>
            <Level3>
                  <Level2Desc>Condo</Level2Desc>
                  <Level2ID>1001</Level2ID>
            </Level3>
      </Level2>
      <Level2>
            <Level2Desc>Park</Level2Desc>
            <Level2ID>101</Level2ID>
      </Level2>
</level1>

There are many level 1s in my data.

I have looked at using the position function, but it seems to be based on
levels, so i was getting 1s 2s and 3s quite a bit.  I tried looking at
recursion, but the examples I found were not nested.

I have been trying to use recursion samples that I found using this list
which I shall not reprint, but can supply a copy if needed.

So the end result of the processing would look something like

1 Neighborhood
2 Street
3 House
4 Condo
5 Park

Which is to say that every item that I do something with I need a unique
sequential number.

Thank you.
--
"NOTICE:  The information contained in this electronic mail transmission is
intended by Convergys Corporation for the use of the named individual or
entity to which it is directed and may contain information that is
privileged or otherwise confidential.  If you have received this electronic
mail transmission in error, please delete it from your system without
copying or forwarding it, and notify the sender of the error by reply email
or by telephone (collect), so that the sender's address records can be
corrected."


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread