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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:15:20+00:00 2026-05-24T08:15:20+00:00

I am having trouble implementing a sort. I have an XML document that I

  • 0

I am having trouble implementing a sort. I have an XML document that I want to do a 1 for 1 copy except sort certain elements into order. The structure of the document must be the same after the copy in order to validate against the schema. I am not in control of the schema so I cannot modify it. Here is a simplified version of my XML data:

<ResponseDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <resHead>
        <id>R1983Rs</id>
    </resHead>
    <item>
        <objRef>
            <objId>100</objId>
            <sysId>xyz</sysId>
        </objRef>
        <!-- Additional data here -->
    </item>
    <item>
        <objRef>
            <objId>140</objId>
            <sysId>abc</sysId>
        </objRef>
        <!-- Additional data here -->
    </item>
    <resFoot>
        <id>1234</id>
    </resFoot>
</ResponseDoc>

I want to sort the <item> elements into a specific order. The resulting XML file should be:

<ResponseDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <resHead>
        <id>R1983Rs</id>
    </resHead>
    <item>
        <objRef>
            <objId>140</objId>
            <sysId>abc</sysId>
        </objRef>
        <!-- Additional data here -->
    </item>
    <item>
        <objRef>
            <objId>100</objId>
            <sysId>xyz</sysId>
        </objRef>
        <!-- Additional data here -->
    </item>
    <resFoot>
        <id>1234</id>
    </resFoot>
</ResponseDoc>

I was successful in sorting with the following XSL stylesheet:

<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
    <xsl:template match="ResponseDoc">
        <xsl:copy>
            <xsl:apply-templates select="resHead" />
            <xsl:apply-templates select="item">
                <xsl:sort select="number(objRef/objId)" order="descending" />
            </xsl:apply-templates>
            <xsl:apply-templates select="resFoot" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

But this is unsatisfying since I need to explicitly handle <resHead> and <resFoot> and if the schema is expanded to include additional sibling(s) to <item>, I will need to search out this XSL and modify it. I have done a great deal of research but only find examples that either don’t include any sibling elements OR contain the “sorted” elements within some “container” element, i.e.:

...
<items>
    <item>...</item>
    <item>...</item>
    <item>...</item>
</items>
...

I’d like to find more generic way of accomplishing the sort. I’ve attempted many varients of the working style sheet but I either lose or reposition the sibling nodes or break the sort. Can anyone help out?

  • 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-24T08:15:21+00:00Added an answer on May 24, 2026 at 8:15 am

    Here’s a solution that will work if all your item elements are contiguous:

    <xsl:template match="ResponseDoc">
        <xsl:copy>
            <xsl:apply-templates select="item[1]/preceding-sibling::*" />
            <xsl:apply-templates select="item">
                <xsl:sort select="number(objRef/objId)" order="descending" />
            </xsl:apply-templates>
            <xsl:apply-templates select="item[last()]/following-sibling::*" />
        </xsl:copy>
    </xsl:template>
    

    It simply species any siblings that come before the first item element instead of resHead, and any siblings that come after the last item element instead of resFoot.

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

Sidebar

Related Questions

I have a small class hierarchy that I'm having trouble implementing copyWithZone: for. I've
I'm having trouble implementing a QListWidget with custom items that can be reordered by
I have been having quite a bit of trouble implementing unit testing on the
So I have an app where I'm implementing a universal tool bar that sort
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function.
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
I having trouble in dividing the HTML frames. I have been using the following
I am having trouble with IE7. I have a header, which is an IMG.
I've been having trouble getting my ASP.NET application to automatically log users into the
I'm having trouble with global variables in php. I have a $screen var set

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.