【发布时间】:2017-10-25 12:48:22
【问题描述】:
我有以下文件(其结构不应更改):
<file>
<one>First</one>
<three>Third</three>
<two>Second</two>
<five>Fifth</five>
</file>
我正在寻找 XSLT 转换,它提供以下输出(自定义排序 + 逗号分隔):
一、二、三、五
我要手动定义排序:
<xsl:apply-templates select="one">
<xsl:apply-templates select="two">
<xsl:apply-templates select="three">
<xsl:apply-templates select="four">
<xsl:apply-templates select="five">
请注意,原始文件中缺少元素 <four>!
不幸的是,常用的逗号分隔方法
<xsl:for-each select="one|two|three|four|five">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">, </xsl:if>
</xsl:for-each>
在这种情况下不起作用。当然,我可以使用带有一些排序标准的xsl:sort...
但也许有一个简单而优雅的解决方案,是吗?
【问题讨论】:
-
好吧,如果您可以使用 XSLT 2 或 3,您可以使用
<xsl:apply-templates select="one, two, three, four, five"/>,然后使用例如您的方法<xsl:template match="file/*"><xsl:value-of select="."/><xsl:if test="position() != last()">, </xsl:if></xsl:template>可以工作,尽管简单地使用<xsl:value-of select="one, two, three, four, five" separator=", "/>更容易。 -
您愿意详细说明它是如何不起作用的吗?
-
@MartinHonnen 不幸的是,我仅限于 XSLT 1.0。 @Steve for-each 方法的问题在于它忽略了排序(如果我不使用
xsl:sort) -
如果您要询问有关 XSLT 1.0 的问题,请将它们标记为此类。它现在是一种遗留语言变体,我们中的一些人喜欢过滤我们感兴趣的技术。