基于两个属性排序

时间:2016-11-23 21:06:35

标签: xml xslt xslt-1.0

如何使用xsl使用模板基于InfoNo属性然后使用SeqNo属性对xml进行排序。我试过xsl,但我只能在一个属性上做

输入xml:

      <Customer>
          <Info InfoNo="2" SeqNo="1" >
                <LastName>Wilson</LastName>
                <GivenName>Kelley</GivenName>
          </Info>
          <Info InfoNo="4" SeqNo="1" >
                <LastName>Graham</LastName>
                <GivenName>Tom</GivenName>
          </Info>
          <Info InfoNo="1" SeqNo="3" >
                <LastName>Fisher</LastName>
                <GivenName>Elaine</GivenName>
          </Info>
          <Info InfoNo="1" SeqNo="2" ">
                <LastName>Gary</LastName>
                <GivenName>Jerry</GivenName>
          </Info>
          <Info InfoNo="1" SeqNo="1" >
                <LastName>Timothy</LastName>
                <GivenName>Kathy</GivenName>
          </Info>
          <Info InfoNo="3" SeqNo="1" >
                <LastName>Tim</LastName>
                <GivenName>Kerry</GivenName>
          </Info>
          <Info InfoNo="1" SeqNo="4" >
                <LastName>Rob</LastName>
                <GivenName>Tony</GivenName>
          </Info>
       </Customer>

预期输出xml:

    <Customer>
          <Info InfoNo="1" SeqNo="1" >
                <LastName>Timothy</LastName>
                <GivenName>Kathy</GivenName>
          </Info>
          <Info InfoNo="1" SeqNo="2" ">
                <LastName>Gary</LastName>
                <GivenName>Jerry</GivenName>
          </Info>
          <Info InfoNo="1" SeqNo="3" >
                <LastName>Fisher</LastName>
                <GivenName>Elaine</GivenName>
          </Info>   
          <Info InfoNo="1" SeqNo="4" >
                <LastName>Rob</LastName>
                <GivenName>Tony</GivenName>
          </Info>                 
          <Info InfoNo="2" SeqNo="1" >
                <LastName>Wilson</LastName>
                <GivenName>Kelley</GivenName>
          </Info>
          <Info InfoNo="3" SeqNo="1" >
                <LastName>Tim</LastName>
                <GivenName>Kerry</GivenName>
          </Info>                 
          <Info InfoNo="4" SeqNo="1" >
                <LastName>Graham</LastName>
                <GivenName>Tom</GivenName>
          </Info>
       </Customer>      

需要在xslt1.0中

1 个答案:

答案 0 :(得分:0)

为此使用两个<xsl:sort>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/Customer">
   <Customer>
     <xsl:for-each select="Info">
       <xsl:sort select="@InfoNo" data-type="number" />
       <xsl:sort select="@SeqNo" data-type="number" />
       <xsl:copy-of select="." />
     </xsl:for-each>
   </Customer>
 </xsl:template>

</xsl:stylesheet>