I am sorting the xml file using xslt and i am facing small problem here..
My XML file is given below:
<?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="378466">
<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:SuMSubscriberProfile id="378460">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>2</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
</configData>
<fileFooter dateTime="2015-03-14T10:10:10"/>
</bulkCmConfigDataFile>
and I am using following style sheet:
<?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 method="xml" indent="yes" version="1.0"
encoding="ISO-8859-1" />
<xsl:strip-space elements="*" />
<xsl:param name="outerMatchElement" />
<xsl:param name="innerMatchElement" />
<xsl:param name="sortBy" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="*[name()=$outerMatchElement]">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:choose>
<xsl:when test="count(*[name()=$innerMatchElement]) = 0">
<xsl:apply-templates select="*[name()=$sortBy]">
<xsl:sort select="@id" data-type="number" />
</xsl:apply-templates>
</xsl:when>
<xsl:when test="count(*[name()=$innerMatchElement]) = 1">
<xsl:apply-templates select="*[name()=$innerMatchElement]/*[name()=$sortBy]">
<xsl:sort select="@id" data-type="number" />
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*[name()=$innerMatchElement]">
<xsl:sort select="*[name()=$sortBy]/@id" data-type="number" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
where outerMatchElement = bulkCmConfigDataFile, innerMatchElement=configData , sortBy=subs:SuMSubscriberProfile
I am getting following result using this stylesheet:
<?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="configData.xsd configData.xsd">
<subs:SuMSubscriberProfile id="378460">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>2</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
<subs:SuMSubscriberProfile id="378466">
<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>
</bulkCmConfigDataFile>
Expected output is following:
<?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="configData.xsd configData.xsd">
<fileHeader fileFormatVersion="32.615 V5.0"/>
<configData dnPrefix="" log="0" mediation="false">
<subs:SuMSubscriberProfile id="378460">
<subs:SuMSubscriptionProfile id="1">
<subs:ImsServiceProfile id="1" modifier="create">
<subs:attributes>
<subs:chargingIdx>2</subs:chargingIdx>
</subs:attributes>
</subs:ImsServiceProfile>
</subs:SuMSubscriptionProfile>
</subs:SuMSubscriberProfile>
<subs:SuMSubscriberProfile id="378466">
<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>
You will notice difference is File Header and File footer tags are missing in output i am getting, please guide me how to resolve this issue?I am trying to make sorting process generic for my xml files.