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

dsmrtnAsked:
Who is Participating?
 
abelCommented:
The following sorts only the TIER elements inside PRODUCT. Is that what you were after?

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

Open in new window

0
 
abelCommented:
What is the current code doing wrongly for you?
0
 
abelCommented:
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

0
 
dsmrtnAuthor Commented:
Is there a book that you can recommend?
0
 
abelCommented:
(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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.