【问题标题】:How to add a delimiter between each element in a for-each loop?如何在 for-each 循环中的每个元素之间添加分隔符?
【发布时间】:2019-09-09 14:39:59
【问题描述】:
我正在遍历列表中的每个元素并输出某个值:
<xsl:for-each select="properties/property">
<xsl:value-of select="name"/>
</xsl:for-each>
这只是输出name 属性节点的串联。
我想在每个元素之间添加分隔符,例如;。如何做到这一点?
我列出了 XSLT 版本 1.0、2.0 和 3.0,因为不同版本之间的功能可能不同。
【问题讨论】:
标签:
xslt
xslt-1.0
xslt-2.0
xslt-3.0
【解决方案1】:
如果您使用的是 XSLT 2.0 或更高版本,则可以删除 xsl:for-each 并在单个语句中执行此操作
<xsl:value-of select="properties/property/name" separator=";" />
在 XSLT 1.0 中,您需要做更多的工作......
<xsl:for-each select="properties/property">
<xsl:if test="position() > 1">;</xsl:if>
<xsl:value-of select="name"/>
</xsl:for-each>
XSLT 2.0 和 XSLT 1.0 的区别在于,在 XSLT 2.0 中,xsl:value-of 会返回“select”语句返回的所有节点的值,而在 XSLT 1.0 中,它只返回第一个节点的值应该不止一个。