Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 5th, 2009, 10:05 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default Nested xsl:if test= with position()

Hi All,

I'm having trouble with an xsl:if test and the position function that I haven't been able to work out. I'm first testing to see if an element in my XML contains either 'Y' or 'N' and then want to return the first 5 records that match the first test criteria.

Code:
<xsl:if test="contains(deleted,'N') and position() &lt; 5">
The problem is that if there are no elements in the top 5 that are 'N' it returns nothing. The result I'm trying to achieve is that the XSL will return the first 5 it finds that are 'N' regardless of their position in the XML document and not to only test the top 5 records.

Any suggestions?

Thanks in advance for any help you can offer :)
 
Old March 5th, 2009, 10:13 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to output the first five 'foo' elements for which the condition holds then try
Code:
<xsl:copy-of select="foo[contains(deleted, 'N')][position() &lt; 6]"/>
So basically put the position check into a second predicate after the predicate with the first condition.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 5th, 2009, 10:30 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks!! Problem solved. That's been driving me nuts for a week and it was something so simple. I actually removed my xsl:if and updated my existing xsl:for-each select="" with what you suggested.

What's the difference between xsl:for-each select="" and xsl:copy-of select="" ??

Thanks again :)
 
Old March 5th, 2009, 10:38 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

xsl:copy-of will simply copy the selected nodes to the result tree. With xsl:for-each you process the selected nodes and the contents of the xsl:for-each element defines the output for each processed node.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
spinout (March 5th, 2009)
 
Old March 5th, 2009, 10:56 AM
Authorized User
 
Join Date: Sep 2004
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Martin Honnen View Post
xsl:copy-of will simply copy the selected nodes to the result tree. With xsl:for-each you process the selected nodes and the contents of the xsl:for-each element defines the output for each processed node.
Nice one! Thanks very much for that, your suggestion worked perfectly! Have a good day!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with xsl:value in nested xsl:for each rabs XSLT 1 March 4th, 2009 01:04 PM
Working with position() in nested for-each edu1980 XSLT 2 July 5th, 2006 02:10 AM
Sorting a xsl:if test="position()" charmaigne XSLT 2 April 12th, 2006 04:42 AM
combining a form input name with xsl position() ? joeprice XSLT 1 February 23rd, 2006 09:54 AM
XSL Test tclancy XSLT 2 February 3rd, 2006 02:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.