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

RE: passing intermediate result while recursively buil

Subject: RE: passing intermediate result while recursively building nodeset
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Fri, 3 May 2002 18:31:15 +0100
michael buil
> I have question which is a variation of a previous question I
> had ... I'd like to recursively build up a nodeset, and I'd
> like to pass the intermediate result on with each recursive call.
>
> For example, if the template is passed the nodeset:
>     <a i=1/><a i=2/>
> and it creates the node:
>     <a i=3/>
> then I'd like to the template to recursively call itself
> passing the nodeset:
>     <a i=1/><a i=2/><a i=3/>
>
> Is this possible/reasonable/efficient?
>
As others have indicated, it's possible, but it's not necessarily going to
be efficient. Doing a recursive template like

<xsl:template name="build">
  <xsl:param name="n"/>
  <xsl:param name="tree"/>
  <xsl:call-template name="build">
     <xsl:with-param name="n" select="$n+1"/>
     <xsl:with-param name="tree">
       <xsl:copy-of select="$tree"/>
       <a i="{$n}"/>
     </xsl:with-param>
  </xsl:call-template>
</xsl:template>

will work (if you give it a terminating condition), but it will have O(n^2)
performance.

However, the nodes in a node-set don't have to be in the same tree. You can
equally well create a node-set by putting each new node in a different tree:

<xsl:template name="build">
  <xsl:param name="n"/>
  <xsl:param name="nodeset"/>
  <xsl:variable name="new">
    <a i="{$n}"/>
  </xsl:variable>
  <xsl:call-template name="build">
     <xsl:with-param name="n" select="$n+1"/>
     <xsl:with-param name="nodeset" select="$new/a | $nodeset"/>
  </xsl:call-template>
</xsl:template>

This will probably be faster than building the tree incrementally, but it
has two drawbacks: (a) it requires the xx:node-set() extension in XSLT 1.0,
and (b) there is no guarantee about the order of the nodes from different
documents.

So the question is, what are you actually trying to achieve?

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx


 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.