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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:28:32+00:00 2026-06-04T00:28:32+00:00

Input file: <myroot> <nodeA id=a> <section id=i> <item id=0 method=a> <!– parent section id=i

  • 0

Input file:

    <myroot>
        <nodeA id="a">
            <section id="i">  
                <item id="0" method="a"> <!-- parent section id="i" , keep this node-->
                    <somechild>a</somechild>
                </item>

                <item id="1" method="a">
                    <otherchild>a</otherchild>
                </item>
            </section>        

            <cell id="i">
                <part id="1" method="b"> <!-- parent cell id="i", keep this node-->
                    <attr>u</attr>
                </part>
            </cell>

            <section id="i">
                <item id="0" method="a"> <!-- parent section id="i", remove this node-->
                    <type>blah</type>
                </item>
                <item id="3" method="a">
                   <other>xx</other>    
                </item>

                <item id="0" method="b"> <!-- this has same id but different method, so we keep this -->
                    <otherchild>a</otherchild>
                </item>
            </section>

            <cell id="i">
                <part id="1" method="b"> <!-- parent cell id="i", remove this node -->
                    <attr>y</attr>
                </part>
            </cell>
        </nodeA>

        <nodeA id="b">
            <section id="i">
                <item id="1" method="a">
                    <otherchild>a</otherchild>
                </item>
            </section>        

            <section id="i">
                <item id="0" method="a">
                    <type>blah</type>
                </item>
                <item id="1" method="a">
                   <other>xx</other>    
                </item>
            </section>
           </nodeA>

        <nodeB id="a">
            <cell id="i">
                <part id="1" method="b">
                    <attr>u</attr>
                </part>
            </cell>

            <section id="i">
                <item id="0" method="a">
                    <type>blah</type>
                </item>                    
            </section>

            <cell id="i">
                <part id="1" method="b">
                    <attr>y</attr>
                </part>
            </cell>
        </nodeB>
</myroot>

output:

<myroot>
        <nodeA id="a">
            <section id="i">
                <item id="0" method="a">
                    <somechild>a</somechild>
                </item>

                <item id="1" method="a">
                    <otherchild>a</otherchild>
                </item>
            </section>

            <cell id="i">
                <part id="1" method="b">
                    <attr>u</attr>
                </part>
            </cell>

            <section id="i">
                <item id="3" method="a">
                   <other>xx</other>    
                </item>

              <item id="0" method="b"> <!-- this has same id but different method, so we keep this -->
                    <otherchild>a</otherchild>
                </item>
                </section>
        </nodeA>

        <nodeA id="b">
            <section id="i">
                <item id="1" method="a">
                    <otherchild>a</otherchild>
                </item>
            </section>        

            <section id="i">
                <item id="0" method="a">
                    <type>blah</type>
                </item>
            </section>
           </nodeA>

        <nodeB id="a">
            <cell id="i">
                <part id="1" method="b">
                    <attr>u</attr>
                </part>
            </cell>

            <section id="i">
                <item id="0" method="a">
                    <type>blah</type>
                </item>                    
            </section>
        </nodeB>

</myroot>

Can anyone help me with the transformation, so that if one node occur two or more times and have the same parent id, we only keep the first occurrence and disregard the others.
Also there is another element in the file namely <nodeB></nodeB>, <nodeC></nodeC>. etc.
Thanks very much.
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-04T00:28:34+00:00Added an answer on June 4, 2026 at 12:28 am

    I think you need to define a key to ‘group’ the duplicates. It seems that they are grouped according to the node name, the @id and @method attributes, and the parent node and @id. Therefore you would define the key like so:

    <xsl:key 
       name="duplicates" 
       match="*" 
        use="concat(local-name(), '|', @id, '|', @method, '|', local-name(..), '|', ../@id, '|', local-name(../..), '|', ../../@id)"/> 
    

    Then, you need to ignore elements that are not first in the key. I think you also need a clause to only match elements that are the ‘child’ elements (otherwise who section elements would be ignored)

    <xsl:template 
       match="*
          [@id!='']
          [not(.//*[@id!=''])]
          [generate-id() != generate-id(key('duplicates', concat(local-name(), '|', @id, '|', @method, '|', local-name(..), '|', ../@id, '|', local-name(../..), '|', ../../@id))[1])]/>
    

    To add to the complexity, it looks like you don’t want to output elements where all the child elements are duplicates.

    <xsl:template 
       match="*
         [@id!='']
         [.//*[@id!='']]
         [not(.//*
            [not(.//*[@id!=''])]
            [generate-id() = generate-id(key('duplicates', concat(local-name(), '|', @id, '|', @method, '|', local-name(..), '|', ../@id, '|', local-name(../..), '|', ../../@id))[1])])
         ]" />
    

    Try the following XSLT

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

    When applied to your sample XML, the following is output:

    <myroot>
       <nodeA id="a">
          <section id="i">
             <item id="0" method="a"><!-- parent section id="i" , keep this node-->
                <somechild>a</somechild>
             </item>
             <item id="1" method="a">
                <otherchild>a</otherchild>
             </item>
          </section>
          <cell id="i">
             <part id="1" method="b"><!-- parent cell id="i", keep this node-->
                <attr>u</attr>
             </part>
          </cell>
          <section id="i">
             <item id="3" method="a">
                <other>xx</other>
             </item>
             <item id="0" method="b"><!-- this has same id but different method, so we keep this -->
                <otherchild>a</otherchild>
             </item>
          </section>
       </nodeA>
       <nodeA id="b">
          <section id="i">
             <item id="1" method="a">
                <otherchild>a</otherchild>
             </item>
          </section>
          <section id="i">
             <item id="0" method="a">
                <type>blah</type>
             </item>
          </section>
       </nodeA>
       <nodeB id="a">
          <cell id="i">
             <part id="1" method="b">
                <attr>u</attr>
             </part>
          </cell>
          <section id="i">
             <item id="0" method="a">
                <type>blah</type>
             </item>
          </section>
       </nodeB>
    </myroot>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is one line of the input file: FOO BAR 0.40 0.20 0.40 0.50
My input file is going to be something like this key value key value
This is the new input file format. I need to automate the process of
how to alert if input type file is empty !? this is my jquery
Hi I have an input file in this format. [Header A] key1 value1 key2
I have this input file (space is the separator for the two elements in
Given a large input file that looks like this: 02/26/2012 08:54:38 Error:java.sql.Exception 02/26/2012 08:54:48
I have an input file something like this: some line some other line another
This code receives a input file with 10 filenames, stores them into an 2d
Input file (test): 123456<a id=id1 name=name1 href=link1>This is link1</a>789<a id=id2 href=link2>This is link2</a>0123 Desired

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.