如何将分号从分号更改为逗号并在XSLT中按字母顺序排序值?请指教。XSLT将分号从分号更改为逗号,并按字母顺序对值进行排序
现有标签
<component>
<rate>T;P;C;X;R</rate>
</component>
预期标签
<component>
<rate>C,P,R,T,X</rate>
</component>
如何将分号从分号更改为逗号并在XSLT中按字母顺序排序值?请指教。XSLT将分号从分号更改为逗号,并按字母顺序对值进行排序
现有标签
<component>
<rate>T;P;C;X;R</rate>
</component>
预期标签
<component>
<rate>C,P,R,T,X</rate>
</component>
与改变分隔符的部分很容易:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/component">
<component>
<rate>
<xsl:value-of select="translate(rate, ';', ',')"/>
</rate>
</component>
</xsl:template>
</xsl:stylesheet>
但是,对于排序的一部分,它是多一点cha llenging ...
如果你可以使用XSLT 3.0,您可以使用tokenize()
,string-join()
,并sort()
...
<xsl:template match="rate">
<xsl:copy>
<xsl:value-of select="string-join(sort(tokenize(normalize-space(),';')),',')"/>
</xsl:copy>
</xsl:template>
如果你可以使用XSLT 2.0,您可以使用tokenize()
和string-join()
,但你“将不得不使用xsl:sort
(或xsl:perform-sort
)来进行排序...
<xsl:template match="rate">
<xsl:variable name="rates" as="item()*">
<xsl:for-each select="tokenize(normalize-space(),';')">
<xsl:sort data-type="text"/>
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:variable>
<xsl:copy>
<xsl:value-of select="string-join($rates,',')"/>
</xsl:copy>
</xsl:template>
谢谢@丹尼尔,Babelabout ..这有助于。不幸的是,我们使用XSLT 1.0,所以你可以请教如何在排序时完成这个工作? –
@DJayE您正在使用哪种XSLT 1.0处理器? –
@ michael.hor257k传统服务器处理XSLT并将其转换为其他系统的XML文件。所以不得不忍受XSLT 1.0 –
XSLT 1.0,2.0或3.0? – AntonH