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 July 2nd, 2007, 08:50 PM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select first child node that matches criteria

I am using XSLT to loop through an xml structure to find the first child node that matches the specified criteria. When I reach that match I want to exit the loop and display only that first node in the output. But i am not able to exit the for loop prematurely. How do I solve this problem?

 
Old July 3rd, 2007, 01:51 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You cannot exit a loop prematurely because there is no such thing as a loop in XSLT. If you need to match a specific node then use xsl:apply-templates and select the correct node in combination with a suitable template. If you show some of the XML and your desired output I'd be able to give more specific advice.

--

Joe (Microsoft MVP - XML)
 
Old July 3rd, 2007, 04:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Why do you need a loop? You just need a filter expression:

<xsl:value-of select="nodes[criteria go here][1]"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 3rd, 2007, 10:24 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. Can I get some more information about the nodes() function suggested? Is there any reference to this function anywhere?

 
Old July 3rd, 2007, 10:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I assumed you would work out what I was trying to say.

If NODES is the expression that selects the nodes you want to "loop over", and CRITERIA is the criteria for matching, then NODES[CRITERIA][1] selects the first node that matches the criteria. For example //blonde[eye-colour='blue'][1] selects the first blue-eyed blonde.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Select correct child node richieWolf XSLT 4 September 24th, 2008 08:53 AM
The reference node is not a child of this node.XSL XMLUser XSLT 2 February 25th, 2008 05:22 AM
how to append child node after an node in XML + C# vishnu108mishra XML 5 November 13th, 2007 05:30 AM
Select node whose child value matches Fantmx XSLT 5 February 16th, 2007 04:54 PM
Select parent node when any one child has key pparadesia XSLT 2 January 7th, 2005 11:29 AM





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