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

Re: xsl:key on part of a document in XSLT2.0?

Subject: Re: xsl:key on part of a document in XSLT2.0?
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Tue, 31 Oct 2006 09:55:01 +0000
optimize xslt key document
On 10/31/06, len feremans <lenferemans@xxxxxxxxxxx> wrote:
Hi,

I am writing XSLT 1.0 stylesheets over a year now and was wondering if the
following is possible in XSLT 2.0: Applying a key to only part of a
document.

For example:
Let's say I do a for-each on each child of a node "$node1" and then check if
a "$node2" has any child with the same id...

<xsl:template name="compare">
                <xsl:param name="node1"/>
                <xsl:param name="node2"/>
                <xsl:for-each select="$node1/e">
                        <xsl:variable name="id1" select="@id"/>
                        <xsl:variable name="n2" select="$node2/e[@id = $id1]"/> //can be slow
                        //do something with node2
                </xsl:for-each>
</xsl:template>

Now this code can be quite slow if there are thousands of nodes under $node2
or $node1.
The complexity would be O(N*K) where N = number of children of $node1 and K
= number of children of $node2. If I could use a key then complexity could
be O(N*log(K)). xsl:key would be also very usefull if it could be created
dynamically inside a template... The code could look something like this:
<xsl:template name="compare">
                <xsl:param name="node1"/>
                <xsl:param name="node2"/>
                <xsl:key name="nodes" match="$node2/e" use="@id"/>
                <xsl:for-each select="$node1/e">
                        <xsl:variable name="id1" select="@id"/>
                        <xsl:variable name="n2" select="key('nodes',$id1)"/> //fast
                        //do something with node2
                </xsl:for-each>
</xsl:template>

You could just do a set compare:


<xsl:apply-templates select="$node2/e[@id = $node1/e/@id]"/>

which you could optimize by storing $node1 ids in a variable:

<xsl:variables name="node1IDs" select="$node1/e/@id" as="xs:string+"/>
<xsl:apply-templates select="$node2/e[@id = $node1IDs]"/>

Or key it:

<xsl:key name="e-by-id" match="e" use="@id"/>
<xsl:apply-templates select="$node2/e[key('e-by-id', @id, $node1)]"/>

Either way should be faster than the for-each.

cheers
andrew

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-2011 All Rights Reserved.