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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:50:23+00:00 2026-05-26T11:50:23+00:00

I have a for-each I want to sort by some value. But the thing

  • 0

I have a for-each I want to sort by some value. But the thing I loop over only has a key that allows a connection to the value. A simple example for a document:

<foo>
  <keys>
    <key id="foo"/>
    <key id="bar"/>
  </keys>

  <things>
    <thing name="foo"><desc>some description</desc></thing>
    <thing name="bar"><desc>another description</desc></thing>
  </things>
</foo>

And a style sheet:

<xsl:for-each select="/foo/keys/key">
  <xsl:sort select="/foo/things/thing[@name=@id]"/>
  <xsl:value-of select="@id"/>
</xsl:for-each>

This doesn’t seem to work. @id relates to the key element from the loop; @name relates to thing from the predicate. How do I solve this? I tried assigning /foo/keys/key/@id to a variable and use that but <sort> must be the first element in a for-each…

  • 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-26T11:50:24+00:00Added an answer on May 26, 2026 at 11:50 am

    Use current() function:

    <xsl:sort select="/foo/things/thing[@name = current()/@id]"/>
    

    Reference: http://www.w3.org/TR/xslt#misc-func

    XML:

    <foo>
        <keys>
            <key id="1"/>
            <key id="2"/>
            <key id="3"/>
            <key id="4"/>
        </keys>
    
        <things>
            <thing name="2">
                <desc>a</desc>
            </thing>
            <thing name="4">
                <desc>b</desc>
            </thing>
            <thing name="3">
                <desc>c</desc>
            </thing>
            <thing name="1">
                <desc>d</desc>
            </thing>
        </things>
    </foo>
    

    XSLT:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" />
    
    
        <xsl:template match="/">
            <xsl:for-each select="/foo/keys/key">
                <xsl:sort select="/foo/things/thing[@name = current()/@id]"/>
                <xsl:value-of select="@id"/>
            </xsl:for-each>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Output:

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

Sidebar

Related Questions

I have an .each loop in jQuery. I want to iterate the loop one
First of all i want to say that i have searched each and every
Assume I have an enum, and I want each enum value to be associated
I have a data structure in my C++ program that has some attributes of
I have a list, that each and one of them have a value. I
I want to sort a list of objects using a value that can take
I have a DevExpress GridControl and i want each cell to be multiline. I
I have a list view with 10 columns and I want each row to
I want to use two different Spring web contexts , each have own contextConfig,
I have about 1 million data in users table. Now I want each user

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.