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

RE: Counting nodes efficiently

Subject: RE: Counting nodes efficiently
From: "Andrew Welch" <AWelch@xxxxxxxxxxxxxxx>
Date: Thu, 19 Feb 2004 08:59:17 -0000
xslt counting node efficiently
> Greetings.
> 
> I've been using Jeni's method from the XSLT FAQ to assign 
> unique id's to 
> nodes. In order to speed things up, can anyone think of a way 
> that I could 
> store the running totals for the different nodes, rather than 
> having to 
> call the count() function repeatedly? A generalized method 
> would obviously 
> be the best, so that it could be applied to any arbitrary set 
> of nodes, 
> but I don't know if this is even possible.
> 
> <xsl:template match="*">
>    <xsl:variable name="name" select="name()" />
>    <xsl:element name="{name()}">
>      <xsl:attribute name="id">
>        <xsl:value-of select="concat($name, '-',
>        count(preceding::*[name()= $name]) +
>        count(ancestor::*[name()= $name]))" />
>      </xsl:attribute>
>      <xsl:apply-templates />
>    </xsl:element>
> </xsl:template>

3 ways:

1.  Create a node-set by selecting all the elements you wish to count
and numbering them using position().  You can then query into this
node-set using the generate-id() function to get the correct number for
the element you're processing.  This only requies one pass of the data
so its quite efficient.

2.  Write a SAX Filter in java that numbers the elements on their way
into the transform.  You can then select this number as if it was
already in the data.

3.  If you are using saxon, you can substring the value returned from
generate-id() after the 'e', as the generated id's take form 'dxxeyy'
where d is the document number and e is the element number.

cheers
andrew

 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.