通过XSLT进行XML排序

时间:2016-12-02 09:46:30

标签: xml sorting xslt grouping

我有这个XML输出

<root>
    <row>
        <Additional_Information>
            <OrgID>SO_000048333</OrgID>
            <ParentOrgID>SO_000337522</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000001780</OrgID>
            <ParentOrgID>TOP_MOST_ORG</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000041599</OrgID>
            <ParentOrgID>SO_000001780</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_3-23773</OrgID>
            <ParentOrgID>SO_000253386</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000305947</OrgID>
            <ParentOrgID>SO_000001780</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000121212</OrgID>
            <ParentOrgID>SO_000046509</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000034169</OrgID>
            <ParentOrgID>SO_000305947</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000033807</OrgID>
            <ParentOrgID>SO_000048333</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000337522</OrgID>
            <ParentOrgID>TOP_MOST_ORG</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000030934</OrgID>
            <ParentOrgID>SO_000033807</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000253386</OrgID>
            <ParentOrgID>SO_000030934</ParentOrgID>
        </Additional_Information>
        <Additional_Information>
            <OrgID>SO_000046509</OrgID>
            <ParentOrgID>SO_000001780</ParentOrgID>
        </Additional_Information>
    </row>
</root>

OrgIDs / ParentOrgIDs有两种格式:SO_XXXXXXXXX和SO_3-XXXXX

我正在使用XSLT 2.0

我希望它像这样排序 SO_000001780(层次结构中的第2级,具有ParentOrgID = TOP_MOST_ORG的那些)
-SO_000041599(二级孩子)
-SO_000046509(二级别的另一个孩子)
--SO_000121212(层级中第3级的孩子)
-SO_000305947(二级别的另一个孩子)
--SO_000034169(层次结构中第3级的子级)
SO_00033752
-SO_000048333
--SO_000033807
--- SO_000030934
---- SO_000253386
----- SO_3-23773

非常感谢任何帮助!我还在学习XSLT。

1 个答案:

答案 0 :(得分:0)

这不是排序的问题,而是根据其层次结构嵌套记录 - 即使您已将平面文件指示为预期输出。

以这种方式尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="child" match="Additional_Information" use="ParentOrgID" />

<xsl:template match="/root">
    <root>
        <xsl:apply-templates select="key('child', 'TOP_MOST_ORG')"/>
    </root>
</xsl:template>

<xsl:template match="Additional_Information">
    <xsl:copy-of select="."/>
    <xsl:apply-templates select="key('child', OrgID)"/>
</xsl:template>

</xsl:stylesheet>