如何在 for-each 循环中的每个元素之间添加分隔符? [英] How to add a delimiter between each element in a for-each loop?

查看:18
本文介绍了如何在 for-each 循环中的每个元素之间添加分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遍历列表中的每个元素并输出某个值:

I am looping through each element in a list and outputting a certain value:

<xsl:for-each select="properties/property">
    <xsl:value-of select="name"/>
</xsl:for-each>

这只是输出属性的 name 节点的串联.

This simply outputs a concatenation of the name node of property.

我想在每个元素之间添加分隔符,例如;.这怎么办?

I want to add delimiter, such as ; between each element. How can this be done?

我列出了 XSLT 版本 1.0、2.0 和 3.0,因为不同版本之间的功能可能不同.

I listed XSLT versions 1.0, 2.0 and 3.0 as functionalities might differ between different versions.

推荐答案

如果您使用的是 XSLT 2.0 或更高版本,您可以删除 xsl:for-each 并在单个语句中完成

If you are using XSLT 2.0 or above, you can drop the xsl:for-each and just do it in a single statement

<xsl:value-of select="properties/property/name" separator=";" />

在 XSLT 1.0 中,您需要做更多的工作......

In XSLT 1.0, you would need to do more work though....

<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 中,如果有多个,它只返回第一个节点的值.

The difference between XSLT 2.0 and XSLT 1.0 is because in XSLT 2.0, xsl:value-of will return the value of all the nodes returned by the "select" statement, but in XSLT 1.0, it only returns the value of the first node should there be more than one.

这篇关于如何在 for-each 循环中的每个元素之间添加分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆