2017-02-15 173 views
0

在下面的XML中,我只需要排序一個特定的分段 - attrGroupMany name =「allergenRelatedInformation」基於的子級值爲。剩下的XML應該按照這個分段進行排序。排序特定的xml節點

FDA BIG 8

因此,所有allergenSpecificationAgency具有 「FDA」 和allergenSpecificationName具有 「BIG 8」 要來 「FDA」 和 「TREE_NUTS」 之前。請建議如何在XSLT中實現這一點。謝謝。

<ns:MT_TradeItemsExport xmlns:ns="test"> 
<Header version="2.1"> 
    <CreationDateTime>2017-02-09T14:19:03.566Z</CreationDateTime> 
    <MessageID>0072745000010_9f9cd85e-6d30-4152-a51f-d8491df45486</MessageID> 
</Header> 
<Payload> 
    <ItemRegistration> 
     <attr name="numberOfServingsPerPackage">4.0</attr> 
    </ItemRegistration> 
    <attrGroupMany name="organicClaim"> 
     <row> 
      <attr name="organicTradeItemCode">2</attr> 
      <attrMany name="organicClaimAgencyCode"> 
       <value>6</value> 
      </attrMany> 
     </row> 
    </attrGroupMany> 

    <attrGroupMany name="allergenRelatedInformation"> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AC</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AE</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AF</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AM</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AN</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AP</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">AY</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">TREE_NUTS</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">TN</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
     <row> 
      <attr name="allergenSpecificationAgency">FDA</attr> 
      <attr name="allergenSpecificationName">BIG 8</attr> 
      <attrGroupMany name="allergen"> 
       <row> 
        <attr name="allergenTypeCode">UW</attr> 
        <attr name="levelOfContainmentCode">FREE_FROM</attr> 
       </row> 
      </attrGroupMany> 
     </row> 
    </attrGroupMany> 
</Payload> 

+0

你想的'row'子元素進行排序?你有什麼嘗試?使用'xsl:apply-templates/xsl:sort'很簡單。 –

回答

1

您可以xsl:sort使用xsl:perform-sortxsl:apply-templates在一起,以排序row兒童使用

<?xml version="1.0" encoding="UTF-8" ?> 
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 


    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="attrGroupMany[@name ='allergenRelatedInformation']"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*"/> 
      <xsl:apply-templates select="row"> 
       <xsl:sort select="attr[@name = 'allergenSpecificationAgency']"/> 
       <xsl:sort select="attr[@name = 'allergenSpecificationName']"/> 
      </xsl:apply-templates> 
     </xsl:copy> 
    </xsl:template> 
</xsl:transform> 

http://xsltransform.net/ejivdHR

+0

謝謝你,馬丁。你救了我的命!! – user3092856