我需要自动排序Java列表或xml列表



我有许多xml结构与列表,如

<a>
<b>
<list1>
<item>Paris</item>
<item>London</item>
<item>Berlin</item>
<item>Zurich</item>
</list1>
</b>
<list2>
<item>Butter</item>
<item>Sugar</item>
<item>Flower</item>
<item>Eggs</item>
</list2>
<list2>
<item>0.1</item>
<item>20.0</item>
<item>10.0</item>
<item>0.01</item>
</list2>
</a>

我想要xslt函数,它自动排序每一个无论按名称还是按编号按升序排列。我不想具体提到这些名单的名字。它也不应该提及列表在层次结构中的位置。

我什么也没找到。我希望列表按升序排序。

通用样式表很难编写。可以试试这样写:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[* and not(*/*)]">
<xsl:copy>
<xsl:choose>
<xsl:when test="number(*[1]) = number(*[1])">
<xsl:apply-templates>
<xsl:sort select="." data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates>
<xsl:sort select="." data-type="text" order="ascending"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

查找只有叶节点作为子元素的元素。然后测试第一个子元素是否为数字;如果是,子元素按数字排序,否则按文本排序。

最新更新