2012-10-31 95 views
1

我熟悉XSLT的基础知识,但我遇到了一个奇怪的情况,我似乎无法弄清楚。我很抱歉这么久,但我会很感激你能提供的任何帮助。按多个元素排序

我想根据排序的品牌/价值/ @名称和文件/价值/ @名称的串联,从而易排序Level_5

XML unsorted: 
<Root att="x"> 
    <Level_1 Name="NEW Level">  
    <Level_2>  
     <Level_3 Name="nameLvl_3">   
     <Level_4>  
      <Level_5 Name="aa"> 
      <Brand> 
       <Value Name="Text" Value="1" /> 
      </Brand>    
     <Docs> 
       <Value Name="Pro" Value="2" /> 
       <Value Name="Numeric" Value="0.05" /> 
      </Docs>    
      </Level_5>    
      <Level_5 Name="aa">    
      <Text> 
       <Val Id="1"/> 
      </Text> 
      </Level_5> 
      <Level_5 Name="aa"> 
      <Brand> 
       <Value Name="Text" Value="2" /> 
       <Value Name="Number" Value="1" /> 
       <Value Name="Long" Value="3" /> 
      </Brand> 
      </Level_5> 
     </Level_4> 
     </Level_3> 
    </Level_2>  
    </Level_1> 
</Root> 

你做拼接后,您应该排序Level_5节点, “” LongNumberText,TextNumericPro 和预期输出是:

<Root att="x"> 
    <level_1 Name="NEW Level">  
    <level_2>  
     <Level_3 Name="nameLvl_3">   
     <Level_4> 
      <Level_5 Name="aa">    
      <Text> 
       <Val Id="1"/> 
      </Text> 
      </Level_5> 
      <Level_5 Name="aa"> 
      <Brand> 
      <Value Name="Long" Value="3" /> 
     <Value Name="Number" Value="1" /> 
       <Value Name="Text" Value="2" />    
      </Brand> 
      </Level_5> 
      <Level_5 Name="aa"> 
      <Brand> 
       <Value Name="Text" Value="1" /> 
      </Brand>    
    <Docs>   
       <Value Name="Numeric" Value="0.05" /> 
       <Value Name="Pro" Value="2" /> 
      </Docs>    
      </Level_5> 
     </Level_4> 
     </Level_3> 
    </Level_2>  
    </Level_1> 
</Root> 

我只能排序的/品牌/价值/ @名称的第一个元素,但我没有任何想法如何拼接的休息品牌和文档内的属性,这是代码即时NG:

<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="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Level_3/Level_4/Level_5/Brand|Docs"> 
    <xsl:copy> 
     <xsl:apply-templates> 
     <xsl:sort select="@Name" data-type="text"/> 
     <xsl:sort select="@Value" data-type="text"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

    <!--             --> 
    <xsl:template match="Level_2/Level_3/Level_4"> 
    <xsl:copy> 
     <xsl:apply-templates select="Level_5"> 
     <xsl:sort select="Brand|Docs/Value/@Name" data-type="text"/> 
     </xsl:apply-templates> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

但我只是排序再见品牌的内部或文档请任何帮助的第一个元素

回答

0

有可能是一个聪明的方式在XPath 1.0表达式要做到这一点,但我不看到一个。更直接的方法是分两步处理数据。

首先编写一个执行近似身份转换的样式表:输入只是按原样写回输出,但在每个Level_5元素上添加一个属性,并使用sort-key,通过处理所有合适的孩子进入make-sort-key模式。

然后编写其他样式表并使用sort-key属性来控制排序(如果您不想要它,请再次放下它)。

关于另一个话题:匹配模式Level_3/Level_4/Level_5/Brand | Docs不等于Level_3/Level_4/Level_5/Brand | Level_3/Level_4/Level_5/Docs。如果你的意思是后者,你需要一个不同的匹配模式。

+0

我已经做了两步,但我被限制在一步做到这一点 – user1789798