相关疑难解决方法(0)

使用 XSLT 按预定顺序对 XML 元素进行排序

我必须遵循 XML:

 <root>
       <a></a>
       <b></b>
       <a></a>
       <a></a>
       <b></b>
       <c></c>
</root>
Run Code Online (Sandbox Code Playgroud)

a、b 和 c 元素的顺序是随机的。现在我想以预定义的方式对元素进行排序(首先是 b,然后是 a,然后是 c)。

我尝试了以下 xslt:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="@*">
         <xsl:sort select="name()"/>
       </xsl:apply-templates>

       <xsl:apply-templates select="node()">
        <xsl:sort select="name()"/>
       </xsl:apply-templates>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

它按名称对元素进行排序,因此按预期进行 a、b、c。

除了降序/升序之外,还有其他方法可以定义排序顺序吗?

谢谢!

xml sorting xslt

2
推荐指数
1
解决办法
987
查看次数

标签 统计

sorting ×1

xml ×1

xslt ×1