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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:21:09+00:00 2026-06-04T22:21:09+00:00

I am trying to select elements based on a number in their title element

  • 0

I am trying to select elements based on a number in their “title” element and then return them in a specific order. I’ve grouped them using a key, but when I access they key and apply-templates to the nodes, they’re not being returned in the order specified.

sample code:

<test>
    <anElement>
        <title>001 title</title>
    </anElement>
    <anElement>
        <title>002 title</title>
    </anElement>
    <anElement>
        <title>003 title</title>
    </anElement>
    <anElement>
        <title>004 title</title>
    </anElement>
    <anElement>
        <title>005 title</title>
    </anElement>
    <anElement>
        <title>006 title</title>
    </anElement>
    <anElement>
        <title>007 title</title>
    </anElement>
    <anElement>
        <title>008 title</title>
    </anElement>
    <anElement>
        <title>009 title</title>
    </anElement>
    <anElement>
        <title>010 title</title>
    </anElement>   
</test>

when transformed with:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>

<xsl:key name="keyNodes" match="//anElement" use="title/substring(., 1, 3)"/>

<xsl:template match="/">
    <groupsOfNodes>
       <aGroup>
           <title>group one</title>
        <members>
            <xsl:apply-templates select="key('keyNodes', ('003', '002', '001'))"/>
         </members>
       </aGroup>
        <aGroup>
            <title>group one</title>
            <members>
                <xsl:apply-templates select="key('keyNodes', ('010', '009', '008'))"/>
            </members>
        </aGroup>
    </groupsOfNodes>
</xsl:template>

<xsl:template match="anElement">
    <para><xsl:apply-templates/></para>
</xsl:template>

</xsl:stylesheet>

gives this result:

<groupsOfNodes>
   <aGroup>
  <title>group one</title>
  <members>
     <para>001 title</para>
     <para>002 title</para>
     <para>003 title</para>
  </members>
  </aGroup>
   <aGroup>
  <title>group one</title>
  <members>
     <para>008 title</para>
     <para>009 title</para>
     <para>010 title</para>
  </members>
   </aGroup>
  </groupsOfNodes>

Here’s an example of the desired result:

    <members>
     <para>003 title</para>
     <para>002 title</para>
     <para>001 title</para>
  </members>

Is there any way to specify the order they’re processed, or do I have to use separate “apply-template” rules for each node in the key?

  • 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-04T22:21:11+00:00Added an answer on June 4, 2026 at 10:21 pm

    You could change your code to

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    
    <xsl:key name="keyNodes" match="anElement" use="title/substring(., 1, 3)"/>
    
    <xsl:template match="/">
        <groupsOfNodes>
           <aGroup>
               <title>group one</title>
            <members>
                <xsl:apply-templates select="for $s in ('003', '002', '001') return key('keyNodes', $s)"/>
             </members>
           </aGroup>
            <aGroup>
                <title>group one</title>
                <members>
                    <xsl:apply-templates select="for $s in ('010', '009', '008') return key('keyNodes', $s)"/>
                </members>
            </aGroup>
        </groupsOfNodes>
    </xsl:template>
    
    <xsl:template match="anElement">
        <para><xsl:apply-templates/></para>
    </xsl:template>
    
    </xsl:stylesheet>
    

    As you are using XSLT 2.0 I also wonder why you use keys for grouping but that is a different issue.

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

Sidebar

Related Questions

I'm trying to select a number of elements from a mysql table based on
I'm trying to select elements based on the attributes of sub a element of
I'm trying to select an element based on its href within a table of
I'm trying to select an HTML element on my page that has a specific
Trying to select an element based on the value of one of it's childrens
I'm trying to look at using types to select elements that are subtypes, so
I'm trying to create a column-based list within a <select> element with multiple=multiple -
In trying to select elements that have any attributes, the following throws a jQuery
I have next problem, when I'm trying to retrieve value from xforms:select elements I
I'm trying to style all <select> elements that are a descendant of a <div>

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.