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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:20:33+00:00 2026-05-12T14:20:33+00:00

Basically, I want an exact copy of the XML, except I’d like to sort

  • 0

Basically, I want an exact copy of the XML, except I’d like to sort certain nodes by their ID attribute. Not all elements with the ID attribute should be sorted.

I’ve kludged together a working stylesheet, but it requires me to hard-code the <xsl:apply-template>’s in for all sibling nodes to the nodes I’m sorting on. I’ve no formal schema, and I can’t be certain that it won’t change. I’d like to be able to create a more generic stylesheet which will output the non-sorted nodes without these explicit calls.

Is this possible?

My Java code to transform the XML:

String input = "C:\\presort.xml";
String output = "C:\\postsort.xml";
String xsl = "C:\\sort.xsl"
Document dom;

try
{
 dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
 Element e = dom.getDocumentElement();

 Transformer transform = TransformerFactory.newInstance().newTransformer(
   new StreamSource( new File(xsl) ));
 StreamResult result = new StreamResult(new File(output));
 transform.transform(new DOMSource(dom), result);
} 
catch (Exception e)
{
 e.printStackTrace();
}    

My XML:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <Person id="1">
    <Base>true</Base>
    <Description>something</Description>
    <Name>Vin Vinigar</Name>
    <Privileges>
      <Privilige id="340">
        <Permission>2</Permission>
        <Job id="dontsort" />
      </Privilige>
      <Privilige id="11">
        <Permission>3</Permission>
        <Job id="dontsort" />
      </Privilige>
      <Privilige id="1011">
        <Permission>1</Permission>
        <Job id="2342" />
      </Privilige>
    </Privileges>
  </Person>

  <Person id="f32">
    <Base>true</Base>
    <Description>Here be dragons</Description>
    <Name>John Doe</Name>
    <Privileges>
      <Privilige id="23a">
        <Permission>2</Permission>
        <Job id="a2a" />
      </Privilige>
    </Privileges>
  </Person>

  <Person id="22">
    <PossibleUnknownTagHere>something</PossibleUnknownTagHere>
    <Name>Han Solo</Name>
    <Privileges>
      <Privilige id="23a">
        <Permission>3</Permission>
        <Job id="a2a" />
      </Privilige>
    </Privileges>
  </Person>

</root>

My stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="root|@*">
        <xsl:copy >
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="Person">
                <xsl:sort select="@id" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Person">
        <xsl:copy >
            <xsl:apply-templates select="@*" />

            <!-- Works but requies a template for each sibling element -->
            <xsl:apply-templates select="Base" />
            <xsl:apply-templates select="Description" />
            <xsl:apply-templates select="Name" />

            <!-- I'd like to do something like this -->
            <!--
            <xsl:choose>
                <xsl:when test="not(Privileges)">
                    <xsl:apply-templates select="." />
                </xsl:when>
            </xsl:choose>
            -->

            <xsl:apply-templates select="Privileges" />
        </xsl:copy>
    </xsl:template>

    <!-- I'd like to remove the need for these 3 explicity copies -->
    <xsl:template match="Base">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Description">
        <xsl:copy-of select="."/>
    </xsl:template>
    <xsl:template match="Name">
        <xsl:copy-of select="."/>
    </xsl:template>

    <xsl:template match="Privileges">
        <xsl:copy >
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="Privilege">
                <xsl:sort select="@id" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Privilege">
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>
  • 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-12T14:20:34+00:00Added an answer on May 12, 2026 at 2:20 pm
    <xsl:template match="node() | @*">
        <xsl:copy>
          <xsl:apply-templates />
        </xsl:copy>
    <xsl:template>
    
    <xsl:template match="root">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="Person">
                <xsl:sort select="@id" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Privileges">
        <xsl:copy>
            <xsl:apply-templates select="@*" />
            <xsl:apply-templates select="Privilege">
                <xsl:sort select="@id" order="ascending" />
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I basically want to do this in code: PersonList myPersonList; //populate myPersonList here, not
So, basically what I want is the exact opposite of httponly cookies. I want
Basically I want to take an image that the user chooses from their photo
I'm having trouble with my SQL SELECT statement. Basically I want to return all
I basically want to be able to deploy multiple versions of the same EAR
I basically want to make things easier by just looping LinkButtons instead of making
I basically want to open a browser window from Word using VBA that does
I basically want to know what the system's input language is currently on (for
I just basically want to add about 20 and sometimes 80 Proximity Alerts with
What i basically want to do is to get content from a website and

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.