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

The Archive Base Latest Questions

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

I need to transform this XML input: <root> <node id=a> <section id=a_1 method=run> <item

  • 0

I need to transform this XML input:

<root>
    <node id="a">
        <section id="a_1" method="run">
            <item id="0" method="a">
                <attribute>
                    <color>Red</color>
                    <status>1</status>
                    <condition>good</condition>
                </attribute>

            </item>

            <item id="0" method="a">
                <attribute>
                    <color>Red</color>
                    <status>1</status>
                    <condition>good</condition>
                </attribute>

            </item>
        </section>

        <section id="a_2" method="run">
            <item id="0" method="a">
                <attribute>
                    <color>Red</color>
                    <status>1</status>
                    <condition>good</condition>
                </attribute>

            </item>
        </section>

    </node>

    <node id="b">
        <section id="b_1" method="create">
            <user id="b_1a" method="x">
                <attribute>

                    <origin>us</origin>
                </attribute>

            </user>
            <user id="b_1a" method="x">
                <attribute> 
                    <origin>us</origin>
                </attribute>
            </user>
            <user id="b_1b">
                <attribute>a</attribute>
            </user>
        </section>

        <section id="b_2">
            <user id="b_1a" method="x">
                <attribute>
                    <name>John</name>
                    <origin>us</origin>
                </attribute>
            </user>
        </section>
    </node>
</root>

Here is the expected output:

<root>
    <node id="a">
        <section id="a_1" method="run">
            <item id="0" method="a">
                <attribute>
                    <color>Red</color>
                    <status>1</status>
                    <condition>good</condition>
                </attribute>                    
            </item>               
        </section>

        <section id="a_2" method="run">
            <item id="0" method="a">
                <attribute>
                    <color>Red</color>
                    <status>1</status>
                    <condition>good</condition>
                </attribute>

            </item>
        </section>
    </node>

    <node id="b">
        <section id="b_1" method="create">
            <user id="b_1a" method="x">
                <attribute>
                    <origin>us</origin>
                </attribute>

            </user>

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

        <section id="b_2">
            <user id="b_1a" method="x">
                <attribute>
                    <name>John</name>
                    <origin>us</origin>
                </attribute>
            </user>
        </section>
    </node>
</root>

Note: the duplicate means all the child/children is having the same value, the node can have 1 or more children as long as it is the same parent (id and method are the same) and we can assume that it always in the same section (id and method are the same).

is this possible to be done? please enlighten me

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-02T07:53:18+00:00Added an answer on June 2, 2026 at 7:53 am

    I. This 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(generate-id(..), '+', 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(generate-id(..),
                                 '+',name(), '+', @id, '+', @method)
                                 )[1]
                            )
                 )
             ]"/>
    </xsl:stylesheet>
    

    when applied to the provided source XML document:

    <root>
        <node id="a">
            <section id="a_1" method="run">
                <item id="0" method="a">
                    <attribute>
                        <color>Red</color>
                        <status>1</status>
                        <condition>good</condition>
                    </attribute>
                </item>
                <item id="0" method="a">
                    <attribute>
                        <color>Red</color>
                        <status>1</status>
                        <condition>good</condition>
                    </attribute>
                </item>
            </section>
            <section id="a_2" method="run">
                <item id="0" method="a">
                    <attribute>
                        <color>Red</color>
                        <status>1</status>
                        <condition>good</condition>
                    </attribute>
                </item>
            </section>
            <section id="a_2" method="run">
                <item id="0" method="a">
                    <attribute>
                        <color>Red</color>
                        <status>1</status>
                        <condition>good</condition>
                    </attribute>
                </item>
            </section>
        </node>
        <node id="b">
            <section id="b_1" method="create">
                <user id="b_1a" method="x">
                    <attribute>
                        <origin>us</origin>
                    </attribute>
                </user>
                <user id="b_1a" method="x">
                    <attribute>
                        <origin>us</origin>
                    </attribute>
                </user>
                <user id="b_1b">
                    <attribute>a</attribute>
                </user>
            </section>
            <section id="b_2">
                <user id="b_1a" method="x">
                    <attribute>
                        <name>John</name>
                        <origin>us</origin>
                    </attribute>
                </user>
            </section>
        </node>
    </root>
    

    produces the wanted, correct result:

    <root>
       <node id="a">
          <section id="a_1" method="run">
             <item id="0" method="a">
                <attribute>
                   <color>Red</color>
                   <status>1</status>
                   <condition>good</condition>
                </attribute>
             </item>
          </section>
          <section id="a_2" method="run">
             <item id="0" method="a">
                <attribute>
                   <color>Red</color>
                   <status>1</status>
                   <condition>good</condition>
                </attribute>
             </item>
          </section>
       </node>
       <node id="b">
          <section id="b_1" method="create">
             <user id="b_1a" method="x">
                <attribute>
                   <origin>us</origin>
                </attribute>
             </user>
             <user id="b_1b">
                <attribute>a</attribute>
             </user>
          </section>
          <section id="b_2">
             <user id="b_1a" method="x">
                <attribute>
                   <name>John</name>
                   <origin>us</origin>
                </attribute>
             </user>
          </section>
       </node>
    </root>
    

    Explanation: Proper use of the Muenchian method for grouping, using a composite key:

    1. The identity rule copies every node “as-is”.

    2. The xsl:key definition associates groups of elements with a string key-value. Any group so defined consists of all elements that have both an id and a method attributes and that (all in the group) have the same parent, the same name, the same string value of the id attribute and the same string value of the method attribute.

    3. There is a single template overriding the identity template. It matches any elements that have both an id and a method attributes and are not the first (in document order) element in their respective group). Because this template has no body, all such matched elements are not processed at all and aren’t copied to the output (we could say they are “deleted”).

    4. Because of 3. above, only elements that are the first element of their group aren’t matched by the overriding template. Thus these elements are matched by the identity template and copied to the output — exactly as required.


    II. XSLT 2.0 Solution:

    <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="*[@id]">
          <xsl:copy>
            <xsl:apply-templates select="@*"/>
    
            <xsl:for-each-group select="*" group-by=
            "concat(generate-id(..), '+', 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 the group-by attribute.

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

Sidebar

Related Questions

I need to convert specific XML attribute into XML element. The input XML: <?xml
I need to transform my nested sets structure (mysql) into json for this spacetree
I need to transform a valid XML document to the OFX v1.0.2 format .
I am developing an application were I need to transform XML documents that look
I am having some problems with an XML transform and need some help. The
My need is just to repalce the attribute value of name. If this attribute
I need to copy a subnode from a XML to a certain node of
my xml DATA IS like:(This is xmlstring not an xmlfile and i need to
I am developing an application were I need to transform XML documents that look
My code builds an xml output from mysql dataset. Then I need to transform

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.