如何使用 XSLT/XPath 生成逗号分隔的列表? [英] How do I generate a comma-separated list with XSLT/XPath?

查看:24
本文介绍了如何使用 XSLT/XPath 生成逗号分隔的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于此 XML 数据:

<前><根><item>苹果</item><item>橙色</item><item>香蕉</item></root>

我可以使用这个 XSLT 标记:

<前>...<xsl:for-each select="root/item"><xsl:value-of select="."/>,</xsl:for-each>...

得到这个结果:

<块引用>苹果、橙子、香蕉、

但是如何生成最后一个逗号不存在的列表?我认为可以按照以下方式做一些事情:

<前>...<xsl:for-each select="root/item"><xsl:value-of select="."/><xsl:if test="...">,</xsl:if></xsl:for-each>...

但是测试表达式应该是什么?

我需要一些方法来确定列表有多长以及我当前在列表中的位置,或者,如果我当前正在处理列表中的最后一个元素(这意味着我不在乎它多长时间是或当前位置是什么).

解决方案

看一下 position(), count()last() 函数;例如,test="position() &lt; last()".

Given this XML data:

<root>
  <item>apple</item>
  <item>orange</item>
  <item>banana</item>
</root>

I can use this XSLT markup:

...
<xsl:for-each select="root/item">
  <xsl:value-of select="."/>,
</xsl:for-each>
...

to get this result:

apple, orange, banana,

but how do I produce a list where the last comma is not present? I assume it can be done doing something along the lines of:

...
<xsl:for-each select="root/item">
  <xsl:value-of select="."/>
  <xsl:if test="...">,</xsl:if>
</xsl:for-each>
...

but what should the test expression be?

I need some way to figure out how long the list is and where I currently am in the list, or, alternatively, if I am currently processing the last element in the list (which means I don't care how long it is or what the current position is).

解决方案

Take a look at the position(), count() and last() functions; e.g., test="position() &lt; last()".

这篇关于如何使用 XSLT/XPath 生成逗号分隔的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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