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

  • Home
  • SEARCH
  • 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 7701777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:04:31+00:00 2026-05-31T23:04:31+00:00

I am looking to sort an XML structure using XSLT using xsltproc: The xml

  • 0

I am looking to sort an XML structure using XSLT using xsltproc:

The xml needs to be sorted from CompanyLocation according to the salesman’s standard sales inside Usercontent. My XSLT however never seems to sort the data, only copy it.

Below is one CompanyLocation XML tree structure I have about 400 others.

XML data:

<Company>
<CompanyStats>
    <CompanyLocation="London">
        <OfficeID>1</OfficeID>
        <Totalworkers>20
        <NoCleaners>2
        <TopSales>
            <UserID>4<UserID>
            <Sales>43</Sales>
            <Description> Highest sales this quater</Description>
        </TopSales>
        <LowestSales>
            <UserID>12<UserID>
            <Sales>26</Sales>
            <Description> Lowest sales this quater</Description>
        </LowestSales>
        <UserContent>
            <ID>4
            <FirstName>Jack</FirstName>
            <Surname>Black</Surname>
            <StartDate>11/11/2011</StartDate>
            <StandardSales>
                <SSID>0<SSID>
                <Sales>64</Sales>
                <SalesManager>Steve Hewitt<SalesManager>
            </StandardSales>
            <BusinessSales>
                <BSID>0<BSID>
                <Sales>64</Sales>
                <SalesManager>Steve Hewitt<SalesManager>                    
            </BusinessSales>
        </UserContent>
    </CompanyLocation>
</CompanyStats>

Here is my attached XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="node()|@*">
        <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
        </xsl:copy> 
    </xsl:template>
<xsl:template match="CompanyStats">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()">
            <xsl:sort select="CompanyLocation/UserContent/StandardSales/Sales" order="descending"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

As i have about 400 CompanyLocation Trees I wish to have the whole tree for each CompanyLocaiton ordered by sales:

<CompanyLocation="London">
<.....>
    <StandardSales>
      <Sales>4</Sales>
    <StandardSales>
<.....>
</CompanyLocation>
<CompanyLocation="Birmingham">
<.....>
    <StandardSales>
     <Sales>25</Sales>
<StandardSales>
    <.....>
</CompanyLocation>
<CompanyLocation="Norwich">
<.....>
    <StandardSales>
     <Sales>35</Sales>
    <StandardSales>
<.....>
</CompanyLocation>
  • 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-31T23:04:33+00:00Added an answer on May 31, 2026 at 11:04 pm

    That is sorting the attributes and children of CompanyStats, but there are no attributes and only one element child (and two white space node) children of that element so all it is doing is bringing the white space before or after the element. I suspect you want to apply the sorting a level down:

    <xsl:template match="CompanyLocation">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="*">
                <xsl:sort select="UserContent/StandardSales/Sales" order="descending"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    

    But actually I’m not clear what you do want, perhaps you could edit your question to add the result that you want in this case.

    UPDATE even after clarification in comments, you don’t make it easy to help:

     <CompanyLocation="London">
    

    not well formed, presumably an attribute name missing?

     <Totalworkers>20
    

    not well formed, missing end tag,

    And assorted other errors in the file. I think a corrected input and working stylesheet are below.

    <Company>
     <CompanyStats>
      <CompanyLocation id="London">
       <OfficeID>1</OfficeID>
       <Totalworkers>20</Totalworkers>
       <NoCleaners>2</NoCleaners>
       <TopSales>
        <UserID>4</UserID>
        <Sales>43</Sales>
        <Description> Highest sales this quater</Description>
       </TopSales>
       <LowestSales>
        <UserID>12</UserID>
        <Sales>26</Sales>
        <Description> Lowest sales this quater</Description>
       </LowestSales>
       <UserContent>
        <ID>4</ID>
        <FirstName>Jack</FirstName>
        <Surname>Black</Surname>
        <StartDate>11/11/2011</StartDate>
        <StandardSales>
         <SSID>0</SSID>
         <Sales>64</Sales>
         <SalesManager>Steve Hewitt</SalesManager>
        </StandardSales>
        <BusinessSales>
         <BSID>0</BSID>
         <Sales>64</Sales>
         <SalesManager>Steve Hewitt</SalesManager>                    
        </BusinessSales>
       </UserContent>
      </CompanyLocation>
      <CompanyLocation id="Paris">
       <OfficeID>1</OfficeID>
       <Totalworkers>20</Totalworkers>
       <NoCleaners>2</NoCleaners>
       <TopSales>
        <UserID>4</UserID>
        <Sales>43</Sales>
        <Description> Highest sales this quater</Description>
       </TopSales>
       <LowestSales>
        <UserID>12</UserID>
        <Sales>26</Sales>
        <Description> Lowest sales this quater</Description>
       </LowestSales>
       <UserContent>
        <ID>4</ID>
        <FirstName>Jack</FirstName>
        <Surname>Black</Surname>
        <StartDate>11/11/2011</StartDate>
        <StandardSales>
         <SSID>0</SSID>
         <Sales>122</Sales>
         <SalesManager>Steve Hewitt</SalesManager>
        </StandardSales>
        <BusinessSales>
         <BSID>0</BSID>
         <Sales>64</Sales>
         <SalesManager>Steve Hewitt</SalesManager>                    
        </BusinessSales>
       </UserContent>
      </CompanyLocation>
     </CompanyStats>
    </Company>
    

    and stylesheet, the main change being the xsl:sort select attribute and datatype=”number” to get numeric sorting.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="node()|@*">
     <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
     </xsl:copy> 
    </xsl:template>
    
    <xsl:template match="CompanyStats">
     <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates select="CompanyLocation">
       <xsl:sort data-type="number" select="UserContent/StandardSales/Sales" order="descending"/>
      </xsl:apply-templates>
     </xsl:copy>
    </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'm looking do some sort of transform from INI to XML, the INI syntax
I'm looking to sort the start menu alphabetically using C#. I've read about deleting
I'm using C# on Framework 3.5. I'm looking to quickly sort a Generic List<T>
I am looking for a way to sort my xml data with javascript, and
I am looking for a simple xml plumbing tool - some sort of notepad
I am looking for a way to pull XML from a SQL database and
I have an array that is created dynamic from an xml document looking something
I am looking to sort my divs horizontally in a container div. I found
I am looking at some sort of existing filter which can sanitize the user
Im looking to group and sort a Generic List<>. I have a list 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.