Link to home
Start Free TrialLog in
Avatar of dsmrtn
dsmrtnFlag for United States of America

asked on

VB.Net Sorting XML by ATTRIBUTE

I think have the vb.net xml transform code working right, just not doing well with the XSLT file.  My transform is copying the XML fine, just not sorting the TIER elements by RATE attribute.

I need an XSLT sort after my XML is built.  XML will have 1-10 products, products will have 1-50 tiers.

<RATES>
  <STATE ABBR="CA" NAME="California">
    <SUB1 CODE="001">
      <SUB2 NAME="sub2">
        <PRODUCT CODE="1234" TITLE="Product1">
          <TIER NUMBER="1" RATE="1.340" />
          <TIER NUMBER="2" RATE="5.080" />
        </PRODUCT>
        <PRODUCT CODE="4567" TITLE="Product2">
          <TIER NUMBER="1" RATE="1.340" />
          <TIER NUMBER="2" RATE="6.080" />
        </PRODUCT>
      </SUB2>
    </SUB1>
  </STATE>
</RATES>

Would like the above XML sorted as shown below.  In both cases, I'm sorting RATE in descending order to provide highest rate 1st.


RATE in TIER sorted with PRODUCT, PRODUCT retains orginal position
<RATES>
  <STATE ABBR="CA" NAME="California">
    <SUB1 CODE="001">
      <SUB2 NAME="sub2">
        <PRODUCT CODE="1234" TITLE="Product1">
          <TIER NUMBER="2" RATE="5.080" />
          <TIER NUMBER="1" RATE="1.340" />
        </PRODUCT>
        <PRODUCT CODE="4567" TITLE="Product2">
          <TIER NUMBER="2" RATE="6.080" />
          <TIER NUMBER="1" RATE="1.340" />
        </PRODUCT>
      </SUB2>
    </SUB1>
  </STATE>
</RATES>


<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
    <xsl:template match="RATES">
        <xsl:copy>
            <xsl:apply-templates select="STATE">
                <xsl:sort select="TIER/@RATE" order="descending"  data-type="text"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

Avatar of abel
abel
Flag of Netherlands image

What is the current code doing wrongly for you?
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I run that against an extended version of your data (I added a few TIER elements for testing) the output looks like this:

<RATES>
   <STATE ABBR="CA" NAME="California">
      <SUB1 CODE="001">
         <SUB2 NAME="sub2">
            <PRODUCT CODE="1234" TITLE="Product1">
               <TIER NUMBER="2" RATE="5.080"/>
               <TIER NUMBER="1" RATE="1.340"/>
            </PRODUCT>
            <PRODUCT CODE="4567" TITLE="Product2">
               <TIER NUMBER="5" RATE="8.340"/>
               <TIER NUMBER="3" RATE="6.340"/>
               <TIER NUMBER="1" RATE="2.340"/>
               <TIER NUMBER="4" RATE="1.370"/>
               <TIER NUMBER="2" RATE="1.080"/>
            </PRODUCT>
         </SUB2>
      </SUB1>
   </STATE>
</RATES>

Open in new window

Avatar of dsmrtn

ASKER

Is there a book that you can recommend?
(grading comment)
> Is there a book that you can recommend?

On XSLT 1.0, Jeni Tennison's introductory book is very good, and the XSLT Cookbook, which contains many recipes for XSLT 1 and 2 has saved me many times in the beginning.