XSLT:for-each div 有部分孩子

XSLT: for-each div with part of childs

我做 XSL 的时间很短,我必须用 child 标记的一部分创建多个 div。所以我有这样的东西:

<Nodes>
    <Node>
        <Tag>a</Tag>
        <Tag>b</Tag>
    </Node>
    <Node>
        <Tag>c</Tag>
    </Node>
</Nodes>

我以为我可以做这样的事情:

<xsl:for-each select="/Nodes">
    <div id="node_{position()}">
        <xsl:for-each select="Node">
            <xsl:value-of select="Tag" />
        </xsl:for-each>
    </div>
</xsl:for-each>

而我需要的是:

<div>
    a
    b
</div>
<div>
    c
</div>

但我总是得到两个 div 和 a b c。相反,第一个带有 b,另一个带有 c。 我是否必须枚举标签或类似的东西?

编辑:

<ProjectTopology>
    <Nodes>
        <Node>
            <Tag>Section1</Tag>
            <Nodes>
                <Node>
                    <Tag>Another section1</Tag>
                    <Tag>Another section2</Tag>
                </Node>
            </Nodes>
            <Tag>Section2</Tag>
            <Nodes>
                <Node>
                    <Tag>Another section3</Tag>
                    <Tag>Another section4</Tag>
                </Node>
            </Nodes>
        </Node>
    </Nodes>
</ProjectTopology>

好的,我正在寻找这样的东西:

<div id="section_1">
    Another section1
    Another section2
</div>
<div id="section_2">
    Another section3
    Another section4
</div>

But I always get two div with a b c.

不,那是不是应用您在此处发布的代码的结果。实际结果是:

<div id="node_1">ac</div>

在 XSLT 1.0 中,并且:

<div id="node_1">a bc</div>

在 XSLT 2.0 中。

输出中只有一个 div,因为源中只有一个 Nodes 节点 XML - 并且是创建 div 的唯一模板是匹配 Nodes.

的那个

要获得您正在寻找的结果,您应该尝试类似的方法:

XSLT 1.0

<xsl:template match="/Nodes">
    <root>
        <xsl:for-each select="Node">
            <div id="node_{position()}">
                <xsl:for-each select="Tag">
                    <xsl:value-of select="." />
                </xsl:for-each>
            </div>
        </xsl:for-each>
    </root>
</xsl:template>

结果

<root>
   <div id="node_1">ab</div>
   <div id="node_2">c</div>
</root>

XSLT 2.0

<xsl:template match="/Nodes">
    <root>
        <xsl:for-each select="Node">
            <div id="node_{position()}">
                <xsl:value-of select="Tag" />
            </div>
        </xsl:for-each>
    </root>
</xsl:template>

结果

<root>
   <div id="node_1">a b</div>
   <div id="node_2">c</div>
</root>