【问题标题】:XSLT sort by node name then attribute name and then by attribute valueXSLT 按节点名称、属性名称和属性值排序
【发布时间】:2016-11-28 02:04:25
【问题描述】:

我想对 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>

【问题讨论】:

  • 你的问题不清楚。 “按每个属性名称排序”是什么意思?您的示例显示您希望按属性名称对属性进行排序 - 但为什么 &lt;mychild id="456"&gt; 移动到 &lt;mychild id="789"&gt; 前面?
  • @michael.hor257k 如果有错误,请原谅我对术语的错误使用
  • @michael.hor257k 如果有错误,请原谅我对术语的错误使用。例如,id 是一个属性名称,而 456 是标签 mychild 的属性值。 id=456 向上移动,因为它大于 id 123 且小于 id 789
  • 逻辑还是不清楚。如果一个元素有多个属性,那么它应该按哪个属性的名称或值进行排序?请注意,根据定义,属性的顺序没有意义,因此您可以使用不同的处理器轻松获得不同的结果。
  • @michael.hor257k 如果一个元素具有多个属性,则必须对这些属性进行排序。逻辑是如果标签名称相同,则应比较第一个属性名称,如果属性名称相同,则应使用值,如果第一个属性及其值相同,则应考虑第二个属性,如果所有属性和值相同,则应考虑注意到的孩子。

标签: xml xslt


【解决方案1】:

我看到下面的 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>

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2011-10-07
      • 1970-01-01
      • 2011-05-04
      相关资源
      最近更新 更多