2016-11-28 155 views
0

我想通過 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> 
    
  • +0

    你的問題不明確。 「*按其每個屬性名稱排序」是什麼意思?你的例子顯示你想按他們的名字對屬性進行排序 - 但爲什麼在<'之前移動了? –

    +0

    @ michael.hor257k請原諒我的錯誤使用術語 – user3626733

    +0

    @ michael.hor257k請原諒我錯誤地使用術語。在這個例如id是一個屬性名稱和456是標籤mychild的屬性值。 id = 456因爲其大於id 123而小於id 789而上移了。789 – user3626733

    回答

    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>