|
| using position() but not in document order |
 |
Mon, 7 Apr 2008 08:58:31 -0700 |
I want to do a sort on some nodes and then get the 1st few elements
based on the sort. I have a solution, but I am wondering about
efficiency.
Say I start with this xml:
<practice>
<a no="3">
<b>white</b>
</a>
<a no="2">
<b>red</b>
</a>
<a no="4">
<b>blue</b>
</a>
</practice>
and I want to get the first 2 sorting on @ no ("red white"). So this
works:
<xsl:template match="practice">
<xsl:apply-templates select="a">
<xsl:sort select="@no"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="a">
<xsl:if test="position()<=2">
<xsl:value-of select="."/>
</xsl:if>
</xsl:template>
My question is, will the processor run through every node returned by
the template (i.e., evaluate all three against the if test)? I don't
see why it would stop after the first 2. Am I wrong? In this instance,
it obviously doesn't matter due to the small source file and maybe it
won't ever in my real world uses.
If I am not wrong about the way the processor would work through the
if test, then is this the best solution? Or is there a better way to
go about it?
Thanks.
kj
|
| Post Reply
|
| Re: using position() but not in document order |
 |
Mon, 07 Apr 2008 18:41:27 +020 |
kj wrote:
> My question is, will the processor run through every node returned by
> the template (i.e., evaluate all three against the if test)? I don't
> see why it would stop after the first 2. Am I wrong? In this instance,
> it obviously doesn't matter due to the small source file and maybe it
> won't ever in my real world uses.
>
> If I am not wrong about the way the processor would work through the
> if test, then is this the best solution? Or is there a better way to
> go about it?
Well to find the first two elements in sort order the XSLT processor has
to sort all nodes. So the
<xsl:apply-templates select="a">
<xsl:sort select="@no"/>
will process all 'a' elements after sorting them. And that way the
<xsl:template match="a">
<xsl:if test="position()<=2">
<xsl:value-of select="."/>
</xsl:if>
will be applied to all of the sorted 'a' elements. Unless the processor
is doing some clever optimization based on the xsl:if that enables it to
stop after two elements. Such an optimization is processor specific and
you will have to ask an implementor of an XSLT processor whether such
things are done.
As for other ways to solve that, you could use
<xsl:template match="a[position()<=2]">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="a[position() > 2]"/>
instead but I am not sure that improves anything in terms of potential
optimization.
--
Martin Honnen --- MVP XML
|
| Post Reply
|
| Re: using position() but not in document order |
 |
Wed, 9 Apr 2008 08:30:54 -0700 |
Thanks for confirming that.
kj
|
| Post Reply
|
|
|
|
|
|
|
|
|
|