基于子元素对XML文件进行排序

时间:2015-07-24 12:20:58

标签: xslt

我有以下xml文件,我想基于subs排序:使用XSLT的SuMSubscriberProfile元素。

<?xml version="1.0" encoding="ISO-8859-1"?>
<bulkCmConfigDataFile xmlns="a.xsd" xmlns:xn="b.xsd" xmlns:subs="c.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="a.xsd a.xsd">
<fileHeader fileFormatVersion="32.615 V5.0"/>
<configData dnPrefix="" log="0" mediation="false">
<xn:ManagedElement id="HSS1">
<subs:SubscriptionFunction id="1">
<subs:SuMSubscriberProfile id="23">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>1</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
</subs:SubscriptionFunction>
<subs:SubscriptionFunction id="1">
<subs:SuMSubscriberProfile id="22">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>1</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
</subs:SubscriptionFunction>
</xn:ManagedElement>
</configData>
<fileFooter dateTime="2015-03-14T10:10:10"/>
</bulkCmConfigDataFile>

我写了以下样式表,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="a.xsd"
    xmlns:xn="b.xsd" xmlns:subs="c.xsd">
    <xsl:output indent="yes" />
    <xsl:strip-space elements="*" />

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

    <xsl:template match="xn:ManagedElement">
        <xsl:apply-templates
            select="subs:SubscriptionFunction/subs:SuMSubscriberProfile">
            <xsl:sort select="@id" data-type="number" />
        </xsl:apply-templates>
    </xsl:template>
</xsl:stylesheet>

预期产出如下。基于标签Subscriberid进行排序。

<?xml version="1.0" encoding="ISO-8859-1"?>
<bulkCmConfigDataFile xmlns="a.xsd" xmlns:xn="b.xsd" xmlns:subs="c.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="a.xsd a.xsd">
<fileHeader fileFormatVersion="32.615 V5.0"/>
<configData dnPrefix="" log="0" mediation="false">
<xn:ManagedElement id="HSS1">
<subs:SubscriptionFunction id="1">
<subs:SuMSubscriberProfile id="22">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>1</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
</subs:SubscriptionFunction>
<subs:SubscriptionFunction id="1">
<subs:SuMSubscriberProfile id="23">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>1</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
</subs:SubscriptionFunction>
</xn:ManagedElement>
</configData>
<fileFooter dateTime="2015-03-14T10:10:10"/>
</bulkCmConfigDataFile>

但我得到了:

<?xml version="1.0" encoding="ISO-8859-1"?>
<bulkCmConfigDataFile xmlns="a.xsd" xmlns:xn="b.xsd" xmlns:subs="c.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="a.xsd a.xsd">
<fileHeader fileFormatVersion="32.615 V5.0"/>
<configData dnPrefix="" log="0" mediation="false">
<subs:SuMSubscriberProfile id="22">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>1</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
<subs:SubscriptionFunction id="1">
<subs:SuMSubscriberProfile id="23">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>1</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
</configData>
<fileFooter dateTime="2015-03-14T10:10:10"/>
</bulkCmConfigDataFile>

我没有得到和标签。

1 个答案:

答案 0 :(得分:1)

以这种方式尝试:

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

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

<xsl:template match="xn:ManagedElement">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select="subs:SubscriptionFunction">
            <xsl:sort select="subs:SuMSubscriberProfile/@id" data-type="number" order="ascending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
相关问题