XSLT按节点名称排序,然后按属性名称排序,然后按属性值

时间:2016-11-28 02:04:26

标签: xml xslt

我想对XML进行排序

  • 首先按标签名称,然后按
  • 每个属性名称,然后
  • 应对属性值进行排序

例如:

首先按标签名称排序XML,然后按属性名称排序,然后按属性值排序。

例如对于以下XML:

<?xml version="1.0" encoding="UTF-8"?>    

<myroot>
        <mychild id="123">
            <fruit>apple</fruit>
            <test hello="world" testing="removed" brackets="angled" question="answers"/>
            <comment>This is a comment</comment>
        </mychild>

        <mychild id="789">

            <fruit>orange</fruit>
            <test brackets="round" hello="greeting">
                <number>111</number>
            </test>
            <dates>
                  <modified>123</modified>
                  <created>880</created>
                  <accessed>44</accessed>
            </dates>
        </mychild>


        <mychild id="456">
            <fruit>banana</fruit>
            <comment>This will be removed</comment>
        </mychild>
    </myroot>

XSLT应该产生以下输出

<?xml version="1.0" encoding="UTF-8"?>
<myroot>
   <mychild id="123">
      <comment>This is a comment</comment>
      <fruit>apple</fruit>
      <test brackets="angled"
         hello="world"
         question="answers"
         testing="removed"/>
   </mychild>
   <mychild id="456">
      <comment>This will be removed</comment>
      <fruit>banana</fruit>
   </mychild>
   <mychild id="789">
      <dates>
         <accessed>44</accessed>
         <created>880</created>
         <modified>123</modified>
      </dates>
      <fruit>orange</fruit>
      <test brackets="round" hello="greeting">
         <number>111</number>
      </test>
   </mychild>
</myroot>

2 个答案:

答案 0 :(得分:0)

我看到下面的xslt工作正常: - )

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

    <xsl:template match="*">

        <xsl:copy> 
            <xsl:apply-templates select="@*">        
                <xsl:sort select="name()"/>
                <xsl:sort select="." />
            </xsl:apply-templates>

            <xsl:apply-templates>
                <xsl:sort select="name()"/> 
                <xsl:sort select="." />
                <xsl:sort select="text()" />

            </xsl:apply-templates>


        </xsl:copy>
    </xsl:template>   



    <xsl:template match="@*|comment()|processing-instruction()">
        <xsl:copy />     
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

此代码还将根据属性进行排序:

       <?xml version="1.0" encoding="UTF-8"?>
        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          version="1.0">
          <xsl:output indent="yes" />
          <xsl:strip-space elements="*"/> 
          <xsl:template match="*">
            <xsl:copy> 
              <xsl:apply-templates select="@*">        
                <xsl:sort select="name()"/> 
            </xsl:apply-templates>
              <xsl:apply-templates>
                <xsl:sort select="name()"/>
                <xsl:sort select= "name(@*)"/>
              </xsl:apply-templates>
            </xsl:copy>
          </xsl:template>
         <xsl:template match="@*">
            <xsl:copy />     
          </xsl:template>
        </xsl:stylesheet>