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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:59:59+00:00 2026-05-12T17:59:59+00:00

I’ve already created my XSLT but id like to be able to sort the

  • 0

I’ve already created my XSLT but id like to be able to sort the data, also add some kind of index so i can group the items together, the difficulty Im having is the the node i want to sort by contains multiple values – values id like to sort by.

For example here is my XML:

<item>
 <title>Item 1</title>
 <subjects>English,Maths,Science,</subjects>
 <description>Blah Blah Bah...</description>
</item>
<item>
 <title>Item 2</title>
 <subjects>Geography,Physical Education</subjects>
 <description>Blah Blah Bah...</description>
</item>
<item>
 <title>Item 3</title>
 <subjects>History, Technology</subjects>
 <description>Blah Blah Bah...</description>
</item>
<item>
 <title>Item 4</title>
 <subjects>Maths</subjects>
 <description>Blah Blah Bah...</description>
</item>

So if i sort by <subjects> I get this order:

English,Maths,Science,
Geography,Physical Education
History, Technology
Maths

But I would like this kind of output:

English
Geography
History
Maths
Maths
Physical Education
Science
Technology

Outputting the XML for each subject contained in <subjects>, so Item1 contains subjects Maths, English & Science so I want to output that Title and Description 3 times because its relevant to all 3 subjects.

Whats the best way in XSLT to do this?

  • 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-12T17:59:59+00:00Added an answer on May 12, 2026 at 5:59 pm

    I think one way to do this would be by using the node-set extenstion function to do multi-pass processing. Firstly you would loop through the existing subject nodes, splitting them by commas, to create a new set of item nodes; one per subject.

    Next, you would loop through this new node set in subject order.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="urn:schemas-microsoft-com:xslt" extension-element-prefixes="exsl" version="1.0">
    
       <xsl:output method="text"/>
    
       <xsl:template match="/">
          <xsl:variable name="newitems">
             <xsl:for-each select="items/item">
                <xsl:call-template name="splititems">
                   <xsl:with-param name="itemtext" select="subjects"/>
                </xsl:call-template>
             </xsl:for-each>
          </xsl:variable>
          <xsl:for-each select="exsl:node-set($newitems)/item">
             <xsl:sort select="text()"/>
             <xsl:value-of select="text()"/>
             <xsl:text> </xsl:text>
          </xsl:for-each>
       </xsl:template>
    
       <xsl:template name="splititems">
          <xsl:param name="itemtext"/>
          <xsl:choose>
             <xsl:when test="contains($itemtext, ',')">
                <item>
                   <xsl:value-of select="substring-before($itemtext, ',')"/>
                </item>
                <xsl:call-template name="splititems">
                   <xsl:with-param name="itemtext" select="substring-after($itemtext, ',')"/>
                </xsl:call-template>
             </xsl:when>
             <xsl:when test="string-length($itemtext) &gt; 0">
                <item>
                   <xsl:value-of select="$itemtext"/>
                </item>
             </xsl:when>
          </xsl:choose>
       </xsl:template>
    
    </xsl:stylesheet>
    

    Note that the above example uses Microsoft’s Extension functions. Depending on what XSLT processor you are using, you may have to specify another namespace for the processor.

    You may also need to do some ‘trimming’ of the subjects, because in your XML sample above, there is a space before one of the subjects (Technology) in the comma-delimited list.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I want to construct a data frame in an Rcpp function, but when I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have a French site that I want to parse, but am running into

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.