xml – XSL递归排序
发布时间:2020-12-16 23:04:02 所属栏目:百科 来源:网络整理
导读:我面临一个问题,我需要对元素进行排序,具体取决于它们的值,它包含一个以句点分隔的数字.我需要根据第一个时段之前的数字值,然后是第一个和第二个时段之间的数字等对元素进行排序.我不知道,这种等级制度有多深,这是最大的问题. ?xml version="1.0" encoding="
我面临一个问题,我需要对元素进行排序,具体取决于它们的值,它包含一个以句点分隔的数字.我需要根据第一个时段之前的数字值,然后是第一个和第二个时段之间的数字等对元素进行排序.我不知道,这种等级制度有多深,这是最大的问题.
<?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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |