我面临一个问题,我需要对元素进行排序,具体取决于它们的值,它包含一个以句点分隔的数字.我需要根据第一个时段之前的数字值,然后是第一个和第二个时段之间的数字等对元素进行排序.我不知道,这种等级制度有多深,这是最大的问题.
<?xml version="1.0" encoding="UTF-8"?> <root> <ROW>2.0.1</ROW> <ROW>1.2</ROW> <ROW>1.1.1</ROW> <ROW>1.2.0</ROW> <ROW>1</ROW> </root>
结果应该是这样的:
<?xml version="1.0" encoding="UTF-8"?> <root> <ROW>1</ROW> <ROW>1.1.1</ROW> <ROW>1.2</ROW> <ROW>1.2.0</ROW> <ROW>2.0.1</ROW> </root>
这有可能吗?感谢任何帮助.
解决方法
有一个“简单”的答案,不使用任何扩展:将行值拆分为chucks并对其进行排序.
<xsl:template match="root"> <xsl:copy> <xsl:apply-templates select="ROW"> <xsl:sort select="substring-before(concat(.,'.'),'.')" data-type="number"/> </xsl:apply-templates> </xsl:copy> </xsl:template> <xsl:template match="ROW"> <xsl:param name="prefix" select="''"/> <xsl:choose> <!-- end of recursion,there isn't any more ROW with more chucks --> <xsl:when test=". = substring($prefix,1,string-length($prefix)-1)"> <xsl:copy-of select="."/> </xsl:when> <xsl:otherwise> <xsl:variable name="chuck" select="substring-before(concat(substring-after(.,$prefix),'.')"/> <!-- this test is for grouping ROW with same prefix,to skip duplicates --> <xsl:if test="not(preceding-sibling::ROW[starts-with(.,concat($prefix,$chuck))])"> <xsl:variable name="new-prefix" select="concat($prefix,$chuck,'.')"/> <xsl:apply-templates select="../ROW[starts-with(.,$new-prefix) or . = concat($prefix,$chuck)]"> <xsl:sort select="substring-before(concat(substring-after(.,$new-prefix),'.')" data-type="number"/> <xsl:with-param name="prefix" select="$new-prefix"/> </xsl:apply-templates> </xsl:if> </xsl:otherwise> </xsl:choose> </xsl:template>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。