Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
899 views
in Technique[技术] by (71.8m points)

xml - XSLT to sort nodes by name?

I'm unsure how the xsl:sort directive works. I need to sort elements by their tag name (for diffing), and I can't seem to come up with how to make this work. My first though was to modify the identity transform and just modify it to include a sort statement, but I'm not exactly sure how to do that.

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()">
              <!-- xsl:sort ? -->
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <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>

when applied on this XML document:

<t b="x" c="y" a="t">
  <c/>
  <b/>
  <a/>
</t>

produces the wanted sorted output:

<t a="t" b="x" c="y">
    <a></a>
    <b></b>
    <c></c>
</t>

Do note:

  1. Not only the elements but also the attributes are sorted (the latter is implementation dependent, but works OK with MSXML).

  2. Using sorted XML for diffs is unreliable, because converting an XML document to a sorted representation isn't 1:1 mapping.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...