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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:52:25+00:00 2026-05-28T03:52:25+00:00

I want to select nodes based on some variables. The XML code: <data> <prot

  • 0

I want to select nodes based on some variables.
The XML code:

<data>
    <prot seq="AAA">
        <node num="1">1345</node>
        <node num="1">11245</node>
        <node num="2">88885</node>
    </prot>
    <prot seq="BBB">
        <node num="1">678</node>
        <node num="1">456</node>
        <node num="2">6666</node>
    </prot>
    <prot seq="CCC">
        <node num="1">111</node>
        <node num="1">222</node>
        <node num="2">333</node>
    </prot>
</data>

The XML that I want

<output>
    <prot seq="AAA">
        <node num="1">1345</node>
        <node num="2">88885</node>
    </prot>
    <prot seq="BBB">
        <node num="1">678</node>
        <node num="2">6666</node>
    </prot>
    <prot seq="CCC">
        <node num="1">111</node>
        <node num="2">333</node>
    </prot>
</data>

So, my idea has been to group the nodes with a xsl:key element, and then do a for-each of them. For example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:key name="by" match="/data/prot" use="concat(@seq,'|',node/@num)"/>
    <xsl:template match="/">
        <root>
            <xsl:apply-templates select="/data/prot"/>
        </root>
    </xsl:template>
    <xsl:template match="/data/prot">
        <xsl:for-each select="./node">
            <xsl:for-each select="key('by',concat(current()/../@seq,'|',current()/@num))">
                node <xsl:value-of select="./node" />
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

but the output is not what I expected, and I cannot see what I am doing wrong. I would prefer to keep the for-each structure. It is just as if I was not using properly the xsl:key grouping features.

the output that I get, unwanted

<root>
    node 1345
    node 1345
    node 678
    node 678
    node 111
node 111</root>

And the code as it to be tested
http://www.xsltcake.com/slices/sgWUFu/20

Thanks!

  • 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-28T03:52:26+00:00Added an answer on May 28, 2026 at 3:52 am

    The main problem in your code is that the key indexes prot elements, but what we want to de-duplicate (and need to index) is the node elements.

    Here is a short and correct solution:

    <xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="nodeByParentAndNum" match="node"
      use="concat(generate-id(..), '+', @num)"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/*">
      <data>
       <xsl:apply-templates/>
      </data>
     </xsl:template>
    
     <xsl:template match=
     "node
       [not(generate-id()
           =
            generate-id(key('nodeByParentAndNum',
                            concat(generate-id(..), '+', @num)
                            )
                             [1]
                        )
           )
       ]
     "/> 
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <data>
        <prot seq="AAA">
            <node num="1">1345</node>
            <node num="1">11245</node>
            <node num="2">88885</node>
        </prot>
        <prot seq="BBB">
            <node num="1">678</node>
            <node num="1">456</node>
            <node num="2">6666</node>
        </prot>
        <prot seq="CCC">
            <node num="1">111</node>
            <node num="1">222</node>
            <node num="2">333</node>
        </prot>
    </data>
    

    the wanted, correct result is produced:

    <data>
       <prot seq="AAA">
          <node num="1">1345</node>
          <node num="2">88885</node>
       </prot>
       <prot seq="BBB">
          <node num="1">678</node>
          <node num="2">6666</node>
       </prot>
       <prot seq="CCC">
          <node num="1">111</node>
          <node num="2">333</node>
       </prot>
    </data>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code selects the nodes, I want to work on...: <xsl:variable name=rootTextpageNode select=$currentPage/ancestor-or-self::node [@level
I want to generate some XML in a stored procedure based on data in
I have a Masterpage that has Treeview. You can select some nodes there. Based
How can I use XPath to select an XML-node based on its content? If
I want to select data between 1 week ago data until today data: SELECT
When you want to recursively enumerate a hierarchical object, selecting some elements based on
How can I, with XSLT, select nodes based on a substring of the nodes'
I need some help. I have this xml document: <?xml version="1.0" encoding="utf-8"?> <MyItems> <Parent
i have users table and i have posts table i want select from users
I want to select the topmost element in a document that has a given

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.