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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:28:40+00:00 2026-05-26T12:28:40+00:00

I have a XML like this: <?xml version=1.0 encoding=UTF-8?> <nodes> <n c=value2/> <n>Has a

  • 0

I have a XML like this:

<?xml version="1.0" encoding="UTF-8"?>

<nodes>
    <n c="value2"/>
    <n>Has a relation to node with value2</n>
    <n>Has a relation to node with value2</n>
    <n c="value"/>
    <n>Has a relation to node with value</n>
    <n c="value1"/>
    <n>Has a relation to node with value1</n>
</nodes>

I sort all elements which have attributes in variable, then I iterate over this variable in for-each loop. But at the end of each loop, I need to print value of those elements which are below the currently processed element(in original XML) and have no atrribute.

That means: call apply-templates on <n> without attribute, but the “select” attr. in apply-templates does not work, probably because I´m now in variable loop.

Is there a solution for that?
Thanks

Here is the XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="nodes">

        <xsl:variable name="sorted">
            <xsl:for-each select="n[@c]">
                <xsl:sort select="@c"></xsl:sort>
                <xsl:copy-of select="."></xsl:copy-of>
            </xsl:for-each>
        </xsl:variable>

        <xsl:for-each select="$sorted/n">
            <xsl:value-of select="@c"></xsl:value-of>

            <xsl:apply-templates select="/nodes/n[2]"></xsl:apply-templates>
        </xsl:for-each>

    </xsl:template>

    <xsl:template match="n[not(@c)]">
        <xsl:value-of select="."></xsl:value-of>
    </xsl:template>

</xsl:stylesheet>

This is just example,all this is a part of bigger project:)

Desired output with a more complicated XPAth(now even the simple one does not work) is:

Value
Has a relation to node with value
Value1
Has a relation to node with value1
Value2
Has a relation to node with value2
Has a relation to node with value2

Is it a bit clearer now?

  • 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-26T12:28:41+00:00Added an answer on May 26, 2026 at 12:28 pm

    Some thoughts: apply-templates without a select processes the child node of the current context node; in your input sample the n elements do not have any children at all. Furthermore in your variable you do a copy-of meaning you create new nodes that have no relation to the nodes in the input sample. So while I am not sure what you want to achieve your construction with apply-templates inside the for-each does not make sense, given the input sample you have posted and the variable you use.

    I suspect you could use the XSLT 2.0 for-each-group group-starting-with as in

    <xsl:template match="nodes">
      <xsl:for-each-group select="n" group-starting-with="n[@c]">
        <xsl:sort select="@c"/>
        <xsl:value-of select="@c"/>
        <xsl:apply-templates select="current-group() except ."/>
      </xsl:for-each-group>
    </xsl:template>
    

    If that does not help then consider to post a small input sample with sample data and the corresponding output sample you want to create with XSLT 2.0, then we can make suggestions on how to achieve that.

    [edit] Now that you have posted an output sample I post an enhanced version of my previous suggestion:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    
    <xsl:output method="text"/>
    
    <xsl:template match="nodes">
      <xsl:for-each-group select="n" group-starting-with="n[@c]">
        <xsl:sort select="@c"/>
        <xsl:value-of select="@c"/>
        <xsl:text>&#10;</xsl:text>
        <xsl:apply-templates select="current-group() except ."/>
      </xsl:for-each-group>
    </xsl:template>
    
    <xsl:template match="n[not(@c)]">
       <xsl:value-of select="."/>
       <xsl:text>&#10;</xsl:text>
    </xsl:template>
    
    </xsl:stylesheet>
    

    When I use Saxon 9.3 and run the stylesheet against your latest input sample the result is as follows:

    value
    Has a relation to node with value
    value1
    Has a relation to node with value1
    value2
    Has a relation to node with value2
    Has a relation to node with value2
    

    That is what you asked for I think so try that approach with your more complex real input.

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

Sidebar

Related Questions

I have an xml like this <?xml version=1.0 encoding=UTF-8 ?> <product> <productname> ----->Dynamic node
So i have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <gesmes:Envelope
I have an XML file like this: <?xml version=1.0 encoding=utf-8?> <RootElement> <Achild> ..... </Achild>
have an xml file like this. <?xml version =1.0 encoding =utf-8?> <menu> <menuNode title=Register
I have a simple xml file that looks like this: <?xml version=1.0 encoding=UTF-8 standalone=yes
I have a XML code like this: <?xml version=1.0 encoding=utf-8 ?> <Window xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml>
I have an XML feed that looks like this: <?xml version=1.0 encoding=UTF-8?> <productFeed version=1.0
Greetings! I have some XML like this: <Root> <MainSection> <SomeNode>Some Node Value</SomeNode> <SomeOtherNode>Some Other
I have XML shaped like the following: <?xml version=1.0 encoding=UTF-8?> <feed xmlns=http://www.w3.org/2005/Atom xmlns:openSearch=http://a9.com/-/spec/opensearch/1.1/ xmlns:docs=http://schemas.google.com/docs/2007
I've got XML feed from public google calendar. Looks like this: <?xml version='1.0' encoding='UTF-8'?>

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.