Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6743419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:57:31+00:00 2026-05-26T11:57:31+00:00

I am trying to sort some XML data, i have followed W3s tutorials, but

  • 0

I am trying to sort some XML data, i have followed W3s tutorials, but my code doesn’t work, whats wrong?

  <xsl:for-each select="garage/car[colour='red']">
  <xsl:apply-templates>

<xsl:sort select="number(price)" order="descending" data-type="number" /> 
</xsl:apply-templates>

    <tr>    
    <td><xsl:value-of select="make"/></td>
    <td><xsl:value-of select="model"/></td>
    <td><xsl:value-of select="price"/></td>
    </tr>


  </xsl:for-each>

XML example~:

  <garage>
  <car>
      <make>vw</make>
      <model>golf</model>
      <color>red</color>
      <price>5.99</price>
  </car>
    <car>
      <make>ford</make>
      <model>focus</model>
      <color>black</color>
      <price>3.66</price>
  </car>
    <car>
      <make>honda</make>
      <model>civic</model>
      <color>red</color>
      <price>15.99</price>
  </car>
</garage>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:57:32+00:00Added an answer on May 26, 2026 at 11:57 am

    So you want to show all the red cars in descending price order?

    It helps if you are consistent with your spelling of color/colour! I am British and feel your pain!

    Also you are mixing up the for-each and apply-templates (both work)

    apply-templates method:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" version="1.0" indent="yes" />
    
       <xsl:template match="/">
          <xsl:apply-templates select="garage/car[color='red']">
             <xsl:sort select="number(price)" order="descending" data-type="number" />
          </xsl:apply-templates>
       </xsl:template>
    
       <xsl:template match="car[color='red']">
          <tr>
             <td>
                <xsl:value-of select="make" />
             </td>
             <td>
                <xsl:value-of select="model" />
             </td>
             <td>
                <xsl:value-of select="price" />
             </td>
          </tr>
       </xsl:template>       
    </xsl:stylesheet>
    

    for-each method:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" version="1.0" indent="yes" />
    
       <xsl:template match="/">
          <xsl:for-each select="garage/car[color='red']">
             <xsl:sort select="number(price)" order="descending" data-type="number" />
             <tr>
                <td>
                   <xsl:value-of select="make" />
                </td>    
                <td>
                   <xsl:value-of select="model" />
                </td>
                <td>
                   <xsl:value-of select="price" />
                </td>
             </tr>
          </xsl:for-each>
       </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to sort some xml with jquery, but this code doesn't affect
I've written the following IComparer but I need some help. I'm trying to sort
I have inherited some code which involves a scheduled task that writes data (obtained
I am trying to sort an xml using xsl. Got some sample from xml.com.
I'm trying to canonicalize the representation of some XML data by sorting each element's
I am trying to load data from XML string into a structure of some
I am trying something very simple, but for some reason it does not work.
(Using the latest MVC 2 RC 2) I'm trying to sort some XML in
I have a piece of data. At the moment, it's an XML file, but
I'm trying to do some sort of strange Facebook App which notifies developers of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.