一个XSL select =" child :: node"问题

时间:2013-01-21 07:02:11

标签: xslt xpath

有一个我无法理解的问题,我将不胜感激:

我有一个看起来像这样的xml文件

<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>

和xsl模板:

<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>
使用<xsl:with-param name="top_node" select="xml/parent">

调用

我希望这只返回作为单个父节点的子节点的子节点,因为它显示为here,但它返回所有子节点。我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

很难弄清楚这个XSLT的最终目的是什么而没有看到更多,但你得到这种行为的原因是路径xml/parent选择所有节点匹配那条路径,而不仅仅是第一条路径。如果您想将它应用于第一个,您可以这样做:

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

如果您想将其应用于某个其他人:

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