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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:53:12+00:00 2026-06-02T14:53:12+00:00

I have this input XML which needs to be transformed with an xslt <root>

  • 0

I have this input XML which needs to be transformed with an xslt

<root>
    <node id="a">
        <section id="a_1" method="run">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>
        <section id="a_2">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>
        <section id="a_1" method="run">
            <item id="0">
                <attribute>
                    <color>Red</color>
                </attribute>
            </item>
        </section>
    </node>
    <node id="b">
        <section id="b_1" method="create">
            <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>

Expected 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" method="create">
            <user id="b_1a">
                <attribute>
                    <name>John</name>
                </attribute>
            </user>
            <user id="b_1b">
                <attribute>a</attribute>
            </user>
        </section>

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

It does not matter which node will be eliminated, as long as it has the same element name, id and method, one of them will be removed.
Any idea what the xsl looks like ?

Note: the element name can be anything doesn’t have to be and it has more than one element name in the whole file; as long as it has the same element name, id and attribute (ex. method=create) one of them will be eliminated.

Thanks very much.
cheers,
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-02T14:53:19+00:00Added an answer on June 2, 2026 at 2:53 pm

    I. Here is a short and efficient (using keys) XSLT 1.0 transformation:

    <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="kElemWithAttribs" match="*[@id and @method]"
      use="concat(name(), '+', @id, '+', @method)"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
      "*[@id and @method
        and
         not(generate-id()
            =
             generate-id(key('kElemWithAttribs',
                             concat(name(), '+', @id, '+', @method)
                             )[1]
                        )
             )
         ]"/>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <root>
        <node id="a">
            <section id="a_1" method="run">
                <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
                </item>
            </section>
            <section id="a_2">
                <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
                </item>
            </section>
            <section id="a_1" method="run">
                <item id="0">
                    <attribute>
                        <color>Red</color>
                    </attribute>
                </item>
            </section>
        </node>
        <node id="b">
            <section id="b_1" method="create">
                <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>
    

    the wanted, correct result is produced:

    <root>
       <node id="a">
          <section id="a_1" method="run">
             <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" method="create">
             <user id="b_1a">
                <attribute>
                   <name>John</name>
                </attribute>
             </user>
             <user id="b_1b">
                <attribute>a</attribute>
             </user>
          </section>
          <section id="b_2">
             <user id="b_1a">
                <attribute>
                   <name>John</name>
                </attribute>
             </user>
          </section>
       </node>
    </root>
    

    Explanation:

    Using the Muenchian method for grouping with a composite key. Here we ignore (delete) every node that isn’t the first in a group.


    II. XSLT 2.0 solution — even shorter and not less efficient:

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="node">
      <xsl:copy>
        <xsl:apply-templates select="@*"/>
    
        <xsl:for-each-group select="*"
             group-by="concat(name(), '+', @id, '+', @method)">
          <xsl:apply-templates select="."/>
        </xsl:for-each-group>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    Explanation:

    Proper use of xsl:for-each-group with a group-by attribute.

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

Sidebar

Related Questions

I have this use case of an xml file with input like Input: <abc
I have the following XML <ns0:Root xmlns:ns0=http://Map_Sample.Input_Schema> <Number1> </Number1> <Number2>11</Number2> <Number3>12</Number3> </ns0:Root> In this
I have this form which allows the input of any product quantity from 1-10:
I have a method which returns a String with a formatted xml. The method
I have xml input which looks like (simplified version used for example): <Student> <Subject>
I have this problem: I have a method private XmlElement ToXmlElement(string xml) { XmlDocument
<To> <Id>SERVICE</Id> <Role>Commuter</Role> </To> <BPD> <OrgNo>234</OrgNo> </BPD> <BON>123</BON> I have this Input XML in
I have this select input in my form that I want to access via
i have this tag as input tag: <a href=controller.jsp?sid=127490C88DB5&R=35144 class=11-link-dkred-bold><b>Mr. John Q. Anderson&nbsp;&nbsp;&nbsp;MBA 1977
I have this code for splitting input list into its halves. It seems to

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.