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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:18:16+00:00 2026-05-24T14:18:16+00:00

I got a XML-document with several values, this is part of it: <result> <element>

  • 0

I got a XML-document with several values, this is part of it:

<result>
  <element>
    <title>Title 1</title>
    <alternative>
      <alternativeTitle>Title 5<alternativeTitle>
    </alternative>
    <data>
      ...
    </data>
  </element>

  <element>
    <title>Title 3</title>
    <alternative>
      <alternativeTitle>Title 2<alternativeTitle>
      <alternativeTitle>Title 4<alternativeTitle>
    </alternative>
    <data>
      ...
    </data>
  </element>
</result>

What I’m trying to accomplish is to sort each title, including the alternative titles. This means that the two elements above would produce five elements that should be sorted (result should have been Title 1, Title 2, Title 3, Title 4, Title 5). It’s also important that the data is accessible after the sort, so that it’s possible to make a list and present some of data as well. Any ideas on how to do that?

In advance, I’d like to thank you for your help!

EDIT: Sorry for not specifying my desired output at the time of creating this question. The structure is a set of articles (title-element is the title of the article and so on), I want to present them in a list which is sorted. It might seem counterintuitive to sort both title and alternative titles, but it has its purpose. 🙂

By using XSL, we produce an XHTML that will be displayed to the user. It will look something like:

TITLE 1 (which is also a link to the full article)
some data from title 1
TITLE 2 (which is also a link to the full article)
some data from title 2
TITLE 3 (which is also a link to the full article)
some data from title 3

and so on..

In this manner, articles are listed several times, but with different titles. I hope this clarifies things, and thanks again for the effort so far! I am unable to try Simon’s answer at the moment, but I guess I can tweek it to get my desired output?

  • 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-24T14:18:17+00:00Added an answer on May 24, 2026 at 2:18 pm

    I’ve corrected/completed your XML a bit, to the following:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <result>
      <element>
        <title>Title 1</title>
        <alternative>
          <alternativeTitle>Title 5</alternativeTitle>
        </alternative>
        <data>
          <foo>123</foo>
        </data>
      </element>
      <element>
        <title>Title 3</title>
        <alternative>
          <alternativeTitle>Title 2</alternativeTitle>
          <alternativeTitle>Title 4</alternativeTitle>
        </alternative>
        <data>
            <baz>321</baz>
        </data>
      </element>
    </result>
    

    It would have been more helpful if you’d provided an example of the output you were hoping for, but I think the following should do that you’re asking:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
        <xsl:output method="xml" indent="yes" />
    
        <xsl:template match="/">
            <xsl:apply-templates select="result" />
        </xsl:template>
    
        <xsl:template match="result">
            <entries>
                <xsl:apply-templates select="element/title|element/alternative/alternativeTitle">
                    <xsl:sort select="string(.)" />
                </xsl:apply-templates>
            </entries>
        </xsl:template>
    
        <xsl:template match="title">
            <entry>
                <xsl:copy-of select="." />
                <xsl:copy-of select="../data" />
            </entry>
        </xsl:template>
    
        <xsl:template match="alternative/alternativeTitle">
            <entry>
                <title><xsl:value-of select="(.)" /></title>
                <xsl:copy-of select="../../data" />
            </entry>
        </xsl:template>
    </xsl:stylesheet>
    

    This produces output (given the amended sample) of:

    <?xml version="1.0"?> 
    <entries> 
      <entry> 
        <title>Title 1</title> 
        <data> 
          <foo>123</foo> 
        </data> 
      </entry> 
      <entry> 
        <title>Title 2</title> 
        <data> 
            <baz>321</baz> 
        </data> 
      </entry> 
      <entry> 
        <title>Title 3</title> 
        <data> 
            <baz>321</baz> 
        </data> 
      </entry> 
      <entry> 
        <title>Title 4</title> 
        <data> 
            <baz>321</baz> 
        </data> 
      </entry> 
      <entry> 
        <title>Title 5</title> 
        <data> 
          <foo>123</foo> 
        </data> 
      </entry> 
    </entries>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an XML document containing news stories, and the body element of a
I'm trying to digitally sign an XML document using Java. I've got an implementation
I've got some XML, for example purposes it looks like this: <root> <field1>test</field1> <f2>t2</f2>
I've got some code which draws data from an xml file but it seems
I've got an XPathNavigator at the root of a document. Several levels down, there's
I have an XML document, several actually, that will be editable via a front-end
I've got the following structured XML file: <w:document> <w:body> <w:p> <w:r> <w:t/> </w:r> </w:p>
I've got some XML (valid XHTML) that looks like this: <html> <head> <script type=text/javascript>
I have a document that transforms xml to word. it starts like this: <?xml
I've got a 2-part question... I have some XML that looks like: <trans-unit id=70

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.