How to add a delimiter between each element in a for-each loop?

扶醉桌前 提交于 2020-01-25 02:49:05

问题


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>

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?

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


回答1:


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=";" />

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>

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.



来源:https://stackoverflow.com/questions/57856240/how-to-add-a-delimiter-between-each-element-in-a-for-each-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!