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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:21:19+00:00 2026-06-02T06:21:19+00:00

If I have this xml file: <root> <node id=a> <section id=a_1> <item id=0> <attribute>

  • 0

If I have this xml file:

<root> 
        <node id="a">
            <section id="a_1">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>

            <section id="a_2">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>            
        </node>

        <node id="b">
            <section id="b_1">

                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

                <user id="b_1b">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_1" method="create">

                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

                <user id="b_1c">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_2">                
                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

            </section>
        </node>
 </root>

and I want the output to be like this:

    <root> 
        <node id="a">
            <section id="a_1">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>

            <section id="a_2">
               <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
               </item>
            </section>            
        </node>

        <node id="b">
            <section id="b_1">

                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

                <user id="b_1b">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_1" method="create">

                <user id="b_1c">
                    <attribute>a</attribute>
                </user>

            </section>

            <section id="b_2">                
                <user id="b_1a">
                    <attribute>
                        <name>John</name>
                    </attribute>
                </user>

            </section>
        </node>
 </root>

As we can see as long as the id is the same it will be considered as one section id even though it has additional method on it. So we delete user id (b_1a) in the second section id (b_1)that has “method create” in it.
This really frustrates me and i haven’t been able to omit the method.
so any help will be greatly appreciated.
If we have a look at section id b_2 it also has the same user id b_1 and the same ‘John’ but we don’t remove it because it is in different section id.
So basically we compare it based on the section id.

PS: the element can be anything not always user or section but as long as the id is the same.

Thanks very much.

kind regards,
John

  • 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-02T06:21:20+00:00Added an answer on June 2, 2026 at 6:21 am

    Although I am not entirely clear on the requirements, I think you may want to group elements by their id and the containing section id. This means you may be able to use an xsl:key to look-up elements

    <xsl:key 
       name="lookup" 
       match="section//*[@id]" use="concat(ancestor::section[1]/@id, '|', @id)" />
    

    Here we are looking up elements (any element) based on their section ID and their own ID. Then it is just a case of ignoring elements in sections where there is another element with a matching id that exists in the lookup.

    <xsl:template 
       match="section//*[@id]
         [generate-id() 
         != generate-id(key('lookup', concat(ancestor::section[1]/@id, '|', @id))[1])]" />
    

    (This is effectively saying, is this element the first element in the look-up. If not, ignore it)

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:key name="lookup" match="section//*[@id]" use="concat(ancestor::section[1]/@id, '|', @id)" />
    
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    
       <xsl:template match="section//*[@id][generate-id() != generate-id(key('lookup', concat(ancestor::section[1]/@id, '|', @id))[1])]" />
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <root>
       <node id="a">
          <section id="a_1">
             <item id="0">
                <attribute>
                   <color>Red</color>
                </attribute>
             </item>
          </section>
          <section id="a_2">
             <item id="0">
                <attribute>
                   <color>Red</color>
                </attribute>
             </item>
          </section>
       </node>
       <node id="b">
          <section id="b_1">
             <user id="b_1a">
                <attribute>
                   <name>John</name>
                </attribute>
             </user>
             <user id="b_1b">
                <attribute>a</attribute>
             </user>
          </section>
          <section id="b_1" method="create">
             <user id="b_1c">
                <attribute>a</attribute>
             </user>
          </section>
          <section id="b_2">
             <user id="b_1a">
                <attribute>
                   <name>John</name>
                </attribute>
             </user>
          </section>
       </node>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
If I have this input file in xml: <root> <node id=N1> <fruit id=1> <orange
I have this XML at http://localhost/file.xml : <?xml version=1.0 encoding=utf-8?> <val:Root xmlns:val=http://www.hw-group.com/XMLSchema/ste/values.xsd> <Agent> <Version>2.0.3</Version>
This is a sample of the XML file : <Nodes version=1> <Node name=root> <Node
I have this xml file <?xml version=1.0 encoding=UTF-8?> <bo:C837ClaimParent xsi:type=bo:C837ClaimParent xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:bo=http://somelongpathHere/process/bo> <claimAux> ...
I have this simple XML file : <?xml version=1.0 encoding=utf-8 ?> <Artists> <artist artistId=1>
I have this data from a xml file: <?xml version=1.0 encoding=utf-8 ?> <words> <id>...</id>
I have this inside my spring xml file: <object type=Test.Web.Utilities.TestClass, Test.Web singleton=false id=TestClass> <property
I have this script to generate an XML file for an RSS feed. Works
I have this use case of an xml file with input like Input: <abc

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.