Hi,
I am using XSLT 1 and MSXML
I have been trying to sort an xml for hours now but to no avail.
I have decided to use an Identity template so that I can output the same xml except sorted in a different order.
I have to sort based on the Ad/@date attribute. I have tried a couple of sort expressions but they do not sort the xml. I have started from the root of the xml and also using the //.
<xsl:template match="/">
<xsl:apply-templates>
<xsl:sort data-type="text" select="//Ad/Issues/Issues/@date"order="descending"/>
<!--<xsl:sort data-type="text" select="xml/Schedule/Media/Vehicle/RateCards...s/Issues/@date" order="descending"/>-->
</xsl:apply-templates>
</xsl:template>
Each Vehicle key in my xml has a collection of Ads. The @date attribute is inside the Ad/Issues/Issue. So, within my first vehicle key (="AMER_FAMILY_PHYSICIAN) if I have this xml as input:
<Ads>
<Ad key="3152:6261" manufac="">
<RateCardRate value="18280.1" />
<ManualRate value="" AdjustedValue="" />
<Position id="531" name="Cover 4" />
<Issues>
<Issue date="2010-02-01">
<Insertion />
</Issue>
<Issue date="2010-03-01">
<Insertion changed="0" />
</Issue>
</Issues>
</Ad>
<Ad key="3152:6261" manufac="">
<RateCardRate value="11806.5" />
<ManualRate value="" AdjustedValue="" />
<Position id="0" name="" />
<Issues>
<Issue date="2010-03-15">
<Insertion />
</Issue>
</Issues>
</Ad>
</Ads>
I need this outupt for descending and vice versa for ascending:
<Ads>
<Ad key="3152:6261" manufac="">
<RateCardRate value="11806.5" />
<ManualRate value="" AdjustedValue="" />
<Position id="0" name="" />
<Issues>
<Issue date="2010-03-15">
<Insertion />
</Issue>
</Issues>
</Ad>
<Ad key="3152:6261" manufac="">
<RateCardRate value="18280.1" />
<ManualRate value="" AdjustedValue="" />
<Position id="531" name="Cover 4" />
<Issues>
<Issue date="2010-02-01">
<Insertion />
</Issue>
<Issue date="2010-03-01">
<Insertion changed="0" />
</Issue>
</Issues>
</Ad>
</Ads>
I would appreciate your help very much.
Cheers
C
XML
-------
<xml>
<Schedule projectId="2345" name="Con" id="7632" xmlns="">
<Media detail="1">
<Vehicle key="AMER_FAMILY_PHYSICIAN" AgencyDiscount="15" CashDiscount="0" title="American Family Physician" insCount="1">
<RateCards>
<RateCard id="7897" name="2010" detail="1">
<EarnedBW value="1"></EarnedBW>
<EarnedColour value="1"></EarnedColour>
<Ads>
<Ad key="3152:6261" manufac="">
<RateCardRate value="18280.1"></RateCardRate>
<ManualRate value="" AdjustedValue=""></ManualRate>
<Position id="531" name="Cover 4"></Position>
<Issues>
<Issue date="2010-02-01">
<Insertion></Insertion>
</Issue>
<Issue date="2010-03-01">
<Insertion changed="0" freq="0" ad="0" manrate="0" pos="0" rcd="0" rate="0" oldRate="18280.10" grossOldRate="21506.00" ordered="18" orderedBy="40" materialFrom="Materials to follow" issueDate="2010-02-12 11:28:47 AM"></Insertion>
</Issue>
</Issues>
</Ad>
<Ad key="3152:6261" manufac="">
<RateCardRate value="11806.5"></RateCardRate>
<ManualRate value="" AdjustedValue=""></ManualRate>
<Position id="0" name=""></Position>
<Issues>
<Issue date="2010-03-15">
<Insertion></Insertion>
</Issue>
</Issues>
</Ad>
</Ads>
</RateCard>
</RateCards>
</Vehicle>
<Vehicle key="AAOS_NOW" AgencyDiscount="15" CashDiscount="0" title="AAOS Now" insCount="1">
<RateCards>
<RateCard id="7859" name="2010" detail="1">
<EarnedBW value="1"></EarnedBW>
<EarnedColour value="1"></EarnedColour>
<Ads>
<Ad key="3152:6261" manufac="">
<RateCardRate value="5984"></RateCardRate>
<ManualRate value="" AdjustedValue=""></ManualRate>
<Position id="531" name="Cover 4"></Position>
<Issues>
<Issue date="2010-03-01">
<Insertion changed="0" freq="0" ad="0" manrate="0" pos="0" rcd="0" rate="0" oldRate="5984.00" grossOldRate="7040.00" ordered="19" orderedBy="40" materialFrom="Materials to follow" issueDate="2010-02-12 11:28:47 AM"></Insertion>
</Issue>
</Issues>
</Ad>
</Ads>
</RateCard>
</RateCards>
</Vehicle>
</Media>
</Schedule>
</xml>
XSLT
-------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:apply-templates>
<xsl:sort data-type="text" select="//Ad/Issues/Issues/@date" order="descending"/>
</xsl:apply-templates>
</xsl:template>
<!--add nothing to the result for these-->
<xsl:template match="@*|node()">
<!--identity for all other nodes-->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>