xsl-list
[Top] [All Lists]

Re: For-each in XSL

2004-08-02 14:14:40

At 2004-08-02 15:17 -0500, Subbiah wrote:
My XML -
<location id="texas">
  <name>Jim</name>
        <children>tommy</children>
        <children>pam<children>
        <children>sam<children>
   </name>
</location>

I need to get to print the chilrens name in a page on a new line

A common requirement.

 The number of children can vary so I need to get all the children, when I
use a for-each tag I am not able to access it.

You need to review some basic principles of XSLT that should be easily available in online resources.

When you are processing the template supplied with an <xsl:for-each> instruction, your current node list is the collection of nodes addressed in the select= attribute, and the template is processed for adding to the result tree as many times as there are members in the current node list. Each time the template is processed the next node in document order of the current node list becomes the current node.

<xsl:for-each select = "location/name/children">
                <fo:block text-align="left">
                <xsl:value-of select = "location/name/children"/>

Above you are not considering that your current node is already <children> and you only need the value of the current node, addressed using ".". Though best practice is to use <xsl:apply-templates/> so as to allow your stylesheet to be imported by other stylesheets for specialization purposes. Doing this would engage the built-in template rules for the text of the <children> element.

                </fo:block>
        </xsl:for-each>

So you would have:

        <xsl:for-each select = "location/name/children">
                <fo:block text-align="left">
                        <xsl:apply-templates/>
                </fo:block>
        </xsl:for-each>

I hope this helps.

................... Ken

--
World-wide on-site corporate, govt. & user group XML/XSL training.
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal



<Prev in Thread] Current Thread [Next in Thread>