我从很短的时间开始使用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和ab c。相反,第一个使用ab,另一个使用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>
但是我总是得到两个div和ab c。
不,这不是应用您在此处发布的代码的结果。实际结果是:
<div id="node_1">ac</div>
在XSLT 1.0中,以及:
<div id="node_1">a bc</div>
在XSLT 2.0中。
div
输出中只有一个,因为Nodes
源XML中只有一个节点-并且唯一创建a的模板div
是match 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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句