Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: using position() but not in document order

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 4/7/2008 6:41:00 PM

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()&lt;=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()&lt;=2]">
		<xsl:value-of select="."/>
	</xsl:template>

	<xsl:template match="a[position() &gt; 2]"/>

instead but I am not sure that improves anything in terms of potential 
optimization.


-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent