svz
svz

Reputation: 4588

An XSL select="child::node" issue

There is an issue that I can't understand and will be grateful for help:

I have an xml file that looks like this

<xml>
    <parent>
        <child_node>1</child_node>
        <child_node>2</child_node>
        <child_node>3</child_node>
    </parent>
    <parent>
        <child_node>4</child_node>
    </parent>
</xml>

And an xsl template:

<xsl:template name="template">
    <xsl:param name="top_node"/>

    <xsl:for-each select="$top_node/child::child_node">
        <xsl:value-of select="."/>
    </xsl:for-each>
</xsl:template>

which is called with <xsl:with-param name="top_node" select="xml/parent">

I expect this to return only the child nodes that are children of a single parent node, as it is shown here, however it returns all the child nodes. What am I missing here?

Upvotes: 0

Views: 757

Answers (1)

JLRishe
JLRishe

Reputation: 101680

It's hard to figure out what the ultimate intent of this XSLT is without seeing more of it, but the reason you're getting this behavior is that the path xml/parent selects all nodes that match that path, not just the first one. If you want to apply it to just the first one, you can do this:

<xsl:with-param name="top_node" select="xml/parent[1]">

If you want to apply it to a certain other one:

<xsl:with-param name="top_node" select="xml/parent[2]">
<xsl:with-param name="top_node" select="xml/parent[3]">
etc.

Upvotes: 2

Related Questions