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 8250895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:03:36+00:00 2026-06-08T00:03:36+00:00

Ok so based on this question XSLT 1.0 sort elements I cannot figure out

  • 0

Ok so based on this question XSLT 1.0 sort elements I cannot figure out why the following is not working:

I have the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<viewentries>
    <viewentry>
        <entrydata name="Waste">
            <text>Bric-a-Brac</text>
        </entrydata>
        <entrydata name="Disposal">
            <text/>
        </entrydata>
    </viewentry>
    <viewentry>
        <entrydata name="Waste">
            <textlist>
                <text>Paper</text>
                <text>Glass</text>
            </textlist>
        </entrydata>
        <entrydata name="Disposal">
            <text/>
        </entrydata>
    </viewentry>
        <viewentry>
        <entrydata name="Waste">
            <textlist>
                <text>Paper</text>
                <text>Cans</text>
            </textlist>
        </entrydata>
        <entrydata name="Disposal">
                <text>Washing Machines</text>
                <text>Cars</text>
        </entrydata>
    </viewentry>
</viewentries>

And the following XSLT:

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

    <xsl:key name="k1" match="entrydata[@name = 'Waste' or @name = 'Disposal']//text" use="concat(ancestor::entrydata/@name, '|', .)"/>

    <xsl:template match="viewentries">
        <categories>
            <xsl:apply-templates/>
        </categories>
    </xsl:template>

    <xsl:template match="viewentry">
        <xsl:apply-templates select="entrydata[@name =  'Waste' or @name = 'Disposal']//text
      [generate-id() = generate-id(key('k1', concat(ancestor::entrydata/@name, '|', .))[1])]">
            <xsl:sort select="."/>
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="text[normalize-space() != '']">
        <category type="{ancestor::entrydata/@name}">
            <xsl:apply-templates/>
        </category>
    </xsl:template>

</xsl:stylesheet>

This gives the following output:

<?xml version="1.0" encoding="UTF-8"?>
<categories>
    <category type="Waste">Bric-a-Brac</category>
    <category type="Waste">Glass</category>
    <category type="Waste">Paper</category>
    <category type="Waste">Cans</category>
    <category type="Disposal">Cars</category>
    <category type="Disposal">Washing Machines</category>
</categories>

I need the output in sorted order:

<?xml version="1.0" encoding="UTF-8"?>
<categories>
    <category type="Waste">Bric-a-Brac</category>
    <category type="Waste">Cans</category>
    <category type="Disposal">Cars</category>
    <category type="Waste">Glass</category>
    <category type="Waste">Paper</category>
    <category type="Disposal">Washing Machines</category>
</categories>

What am I doing wrong ?

EDIT:

It seems to be sorting based on the first <text> value of <entrydata> only instead of all <text> values.

However this stylesheet works fine:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="k1" match="entrydata[@name = 'Waste' or @name = 'Disposal']//text" use="concat(ancestor::entrydata/@name, '|', .)"/>

    <xsl:template match="viewentries">
        <categories>
            <xsl:apply-templates select="viewentry/entrydata[@name =  'Waste' or @name = 'Disposal']//text
            [generate-id() = generate-id(key('k1', concat(ancestor::entrydata/@name, '|', .))[1])]">
                <xsl:sort select="."/>          
            </xsl:apply-templates>
        </categories>
    </xsl:template>

    <xsl:template match="text[normalize-space() != '']">
        <category type="{ancestor::entrydata/@name}">
            <xsl:value-of select="."/>
        </category>
    </xsl:template>

</xsl:stylesheet>

Can someone explain why the first example doesn’t work but the second example does.

  • 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-06-08T00:03:38+00:00Added an answer on June 8, 2026 at 12:03 am

    Can anyone explain why applying the sort to the first template works,
    but when applying it to the second template like in my original
    question it doesn’t ????

    Here is your “second template”:

    <xsl:template match="text[normalize-space() != '']">
        <category type="{ancestor::entrydata/@name}">
            <xsl:apply-templates>
                    <xsl:sort select="."/>            
            </xsl:apply-templates>
        </category>
    </xsl:template>
    

    Here you want to sort the children of the current node by their string value.

    However, in the provided XML document any text element has a single text-node child — therefore there isn’t anything to sort!

    In this and in the previous question you commit the same error — trying to sort the children of the elements that must be sorted — you are sorting too-late.

    Remember:

       <xsl:apply-templates/>
    

    is an abbreviation for:

       <xsl:apply-templates select="child::node()"/>
    

    So, this means: apply templates to my children” — not “apply templates to me”.

    Update: Here is a correct solution to the problem:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:key name="kCatVal" match="text[text()]" use="concat(../@name, '+', .)"/>
    
     <xsl:template match="/">
      <categories>
       <xsl:apply-templates select=
        "//text[generate-id()
                   =
                    generate-id(key('kCatVal',
                                     concat(../@name, '+', .)
                                     )[1]
                                )
                    ]">
              <xsl:sort/>
        </xsl:apply-templates>
      </categories>
     </xsl:template>
    
     <xsl:template match="text">
            <category type="{../@name}"><xsl:value-of select="."/></category>
      </xsl:template>
    </xsl:stylesheet>
    

    When applied on the provided XML document:

    <viewentries>
        <viewentry>
            <entrydata name="Waste">
                <text>Bric-a-Brac</text>
            </entrydata>
            <entrydata name="Disposal">
                <text/>
            </entrydata>
        </viewentry>
        <viewentry>
            <entrydata name="Waste">
                <textlist>
                    <text>Paper</text>
                    <text>Glass</text>
                </textlist>
            </entrydata>
            <entrydata name="Disposal">
                <text/>
            </entrydata>
        </viewentry>
        <viewentry>
            <entrydata name="Waste">
                <textlist>
                    <text>Paper</text>
                    <text>Cans</text>
                </textlist>
            </entrydata>
            <entrydata name="Disposal">
                <text>Washing Machines</text>
                <text>Cars</text>
            </entrydata>
        </viewentry>
    </viewentries>
    

    the wanted, correct result is produced:

    <categories>
       <category type="Waste">Bric-a-Brac</category>
       <category type="">Cans</category>
       <category type="Disposal">Cars</category>
       <category type="">Glass</category>
       <category type="">Paper</category>
       <category type="Disposal">Washing Machines</category>
    </categories>
    

    Explanation:

    This kind of grouping requires indexing based on a composite key that has two parts. Therefore the use attribute of xsl:key is specified as the concatenation of these two parts, joined together by a character that we know cannot be present in any of their values (to avoid false-positives when a value of the first key component is a prefix of a value of the second key component).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this question Fade in/out text based upon value of counter I have
based in this question: Jquery input value change detection i have input 1 and
I'm trying to do an xml trasformation with XSLT. I have the following xml:
Based on this question I have a couple of other questions: 1) the map
Working on a submit function from a jQuery dialog (based on this question). Using
Based on this question here Selecting NOT NULL columns from a table One of
Based on this question Group files and pipe to awk command I have a
I'm (sort of) reposting this question, which is based on this previous question: multiple
Based on this question I've created a small application which is catching all debug
Based on this question I understand the purpose of the construct in linking C

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.