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 September 7th, 2006, 03:58 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default xsl:for-each loop


I have the following problem:

I have to visit the childs of a particular parent node and when I find some particular information in one of that childs I have to produce something in the output.At this moment I dont need visting more childs because I dont have to produce this outuput again.

Ive done this with a xsl:for-each loop and I know I cant break out the loop like in other prgramming languages(all nodes are visited in parallel).

I would like to know if there is any pattern I can use to find if there is a child node containig the specific information I'm looking for.

XML INPUT
---------

<a>
    <b>
        <c>FOO </c>
    </b>
    <b>
        <c>BANANA </c>
    </b>

    <b>
        <c>FOO </c>
    </b>
</a>




 
Old September 7th, 2006, 04:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to write a predicate that describes which nodes are to be processed. For example, to process the first three children:

<xsl:for-each select="*[position() &lt; 4]">

to process all nodes provided they are followed by a BANANA node:

<xsl:for-each select="*[following-sibling::b[c='BANANA']]">

Sometimes it's not possible to do in that way, in which case you have to traverse the children using sibling recursion. (apply-templates to the first child, which it turn does apply-templates to the next child, and so, on passing any accumulated information in parameters).

Or in XSLT 2.0 you can use xsl:for-each-group.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old September 7th, 2006, 05:33 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Which will be the pattern to select all the nodes 'b' that have a node 'c' with text different to 'FOO'.

XML INPUT
---------

<a>
    <b>
        <c>FOO </c>
    </b>
    <b>
        <c>BANANA </c>
    </b>

    <b>
        <c>FOO </c>
    </b>

    <b>
        <c>JOE </c>
    </b>
</a>

Group to select {BANANA,JOE}

Maybe something like:

 <xsl:for-each-group select="b[c/text() != 'LOS']">

Thanks for the help.

 
Old September 7th, 2006, 05:35 AM
Authorized User
 
Join Date: Jul 2006
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe using group-by?






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to loop through a HashMap in XSL? sunilnk XSLT 6 April 10th, 2017 04:09 AM
help: how can I break from xsl for-each loop ? back2grave XSLT 5 May 31st, 2012 12:03 PM
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
Loop in XSL NEO1976 XSLT 31 September 5th, 2006 03:39 AM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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