Xml XSLT:逗号分隔和自定义排序
我有以下文件,其结构不应更改:Xml XSLT:逗号分隔和自定义排序,xml,xpath,xslt-1.0,Xml,Xpath,Xslt 1.0,我有以下文件,其结构不应更改: <file> <one>First</one> <three>Third</three> <two>Second</two> <five>Fifth</five> </file> 我正在寻找XSLT转换,它提供以下输出自定义排序+逗号分隔: 第一,第二,第三,第五 我将手动定义排序: <xsl:apply-
<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">
请注意,原始文件中缺少元素
不幸的是,常用的逗号分隔方法
<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.0,您就可以做到这一点
<xsl:for-each select="one,two,three,four,five">
但是,在XSLT1.0中,一种方法是两个人做类似的事情
<xsl:for-each select="one|two|three|four|five">
<xsl:sort select="string-length(substring-before('|one|two|three|four|five|', concat('|', local-name(), '|')))" />
或者,在这种情况下可能稍微简单一点
<xsl:for-each select="one|two|three|four|five">
<xsl:sort select="string-length(substring-before('onetwothreefourfive',local-name()))" />
对于元素1到4,如果元素存在,则需要在其值后面加逗号;如果元素不存在,则不需要逗号
通过在负责的模板中发出逗号很容易处理这个问题,因为当且仅当匹配元素存在时,才会计算相应的模板
我会写信的
<xsl:template match="one|two|three|four">
<xsl:value-of select="string()"/>
<xsl:text>, </
</
<xsl:template match="five">
<xsl:value-of select="string()"/>
</
好吧,如果你能使用XSLT 2或3,你可以使用,然后你的方法,例如,将工作,虽然简单地使用更容易。你愿意详细说明它是如何不工作的吗?@MartinHonnen不幸的是,我只限于XSLT 1.0@Steve每种方法的问题是,如果我不使用xsl:sortIf,那么它会忽略排序。如果您询问有关XSLT1.0的问题,请将它们标记为这样。它现在是一种传统的语言变体,我们中的一些人喜欢过滤我们感兴趣的技术。