对特定的xml节点进行排序

时间:2017-02-15 21:58:18

标签: xslt xslt-1.0 xslt-2.0

在下面的XML中,我需要根据大孩子值对特定片段 - attrGroupMany name =“allergenRelatedInformation”进行排序。其余的XML应该按原样生成,只对这个段进行排序。

FDA              BIG 8

所有具有“ FDA ”和过敏原规格名称的“过敏原指定”应该在“ 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>

1 个答案:

答案 0 :(得分:1)

您可以xsl:perform-sortxsl:apply-templatesxsl:sort一起使用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