如何使用 XSLT/XPath 生成逗号分隔的列表? [英] How do I generate a comma-separated list with 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() < 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() < last()"
.
这篇关于如何使用 XSLT/XPath 生成逗号分隔的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!