2016-11-24 49 views
0

輸入XML的XML:排序使用XSLT

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap-env:Header> 
     <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext"> 
      <wsse:Username>User</wsse:Username> 
      <wsse:Password>Password</wsse:Password> 
     </wsse:Security> 
    </soap-env:Header> 
    <soap-env:Body> 
     <soap-env:CustomerRs> 
      <Information> 
       <Identity Code="DFW"> 
        <User LoginID="123456" /> 
       </Identity> 
       <User> 
        <Customer Gender="Male"> 
         <Person> 
          <GivenName>Tester2</GivenName> 
          <LastName>Tester</LastName> 
         </Person> 
         <Telephone Type="Business" InfoNo="1" SeqNo="1"> 
          <Number Area="456" Phone="7878787" /> 
         </Telephone> 
         <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> 
         <Address Type="Business" InfoNo="1" SeqNo="1"> 
          <Line1>Menands Ln</Line1> 
          <City>Albany</City> 
         </Address> 
        </Customer> 
        <Order type="S"> 
         <Ref>ABC123</Ref> 
         <OrderedBy> 
          <Debtor code="BLABLA"></Debtor> 
         </OrderedBy> 
         <DeliveryMethod code="Barefoot"></DeliveryMethod> 
         <OrderLine line="1"> 
          <Item code="QQQ123456"></Item> 
          <Quantity>1</Quantity> 
         </OrderLine> 
        </Order> 
       </User> 
      </Information> 
     </soap-env:CustomerRs> 
    </soap-env:Body> 
</soap-env:Envelope> 

期望輸出XML響應:

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap-env:Header> 
     <wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext"> 
      <wsse:Username>User</wsse:Username> 
      <wsse:Password>Password</wsse:Password> 
     </wsse:Security> 
    </soap-env:Header> 
    <soap-env:Body> 
     <soap-env:CustomerRs> 
      <Information> 
       <Identity Code="DFW"> 
        <User LoginID="123456" /> 
       </Identity> 
       <User> 
        <Customer Gender="Male"> 
         <Person> 
          <GivenName>Tester2</GivenName> 
          <LastName>Tester</LastName> 
         </Person> 
         <Telephone Type="Business" InfoNo="1" SeqNo="1"> 
          <Number Area="456" Phone="7878787" /> 
         </Telephone> 
         <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> 
         <Address Type="Business" InfoNo="1" SeqNo="1"> 
          <Line1>Menands Ln</Line1> 
          <City>Albany</City> 
         </Address> 
        </Customer> 
        <Order type="S"> 
         <Ref>ABC123</Ref> 
         <OrderedBy> 
          <Debtor code="BLABLA"></Debtor> 
         </OrderedBy> 
         <DeliveryMethod code="Barefoot"></DeliveryMethod> 
         <OrderLine line="1"> 
          <Item code="QQQ123456"></Item> 
          <Quantity>1</Quantity> 
         </OrderLine> 
        </Order> 
       </User> 
      </Information> 
     </soap-env:CustomerRs> 
    </soap-env:Body> 
</soap-env:Envelope> 

我的XSLT是做轉型:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes" /> 
    <xsl:template match="node()|@*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node()|@*" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/*[local-name()='Envelope']"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"> 
       <xsl:sort select="/*[local-name()='Info']/@*[local-name()='InfoNo']" data-type="number" order="ascending" /> 
       <xsl:sort select="/*[local-name()='Info']/@*[local-name()='SeqNo']" data-type="number" order="ascending" /> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

我用這個樣式表進行排序並安排,但仍然生產相同的輸入。我做錯了什麼,我無法得到答覆。

回答

1

請考慮以下調整。對於排序,由於您使用標識模板按原樣複製文檔,因此無需遍歷整個樹。只需添加一個排序部分的模板,即父標籤,這裏是<Customer>,然後只排序其<Info>子女。

此外,InfoNo的SeqNo的屬性,所以必須用@引用。最後,最佳做法是嘗試在XSLT頂部添加<xsl:strip-space elements="*"/>以刪除空白文本節點,這樣可以增加一點效率,因爲這些節點會從內存中分離出來。

<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="Customer"> 
     <xsl:copy> 
      <xsl:copy-of select="Person|Telephone"/>    
      <xsl:apply-templates select="Info"> 
       <xsl:sort select="@InfoNo" data-type="number" order="ascending" /> 
       <xsl:sort select="@SeqNo" data-type="number" order="ascending" /> 
      </xsl:apply-templates> 
      <xsl:copy-of select="Address"/> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
0

您需要了解相對路徑和絕對路徑之間的區別。要計算元素E的排序關鍵字,您將(幾乎)總是要使用從E開始的路徑(即相對路徑)選擇某個值。以「/」開頭的路徑是絕對路徑,它始於文檔的頂部,因此計算相同的值,而不管E在哪裏。因此,使用絕對路徑計算的排序關鍵字(如<xsl:sort select="/*[.....]"/>)必須是錯誤的。