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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:36:17+00:00 2026-06-03T05:36:17+00:00

If I have this input file in xml: <root> <node id=N1> <fruit id=1> <orange

  • 0

If I have this input file in xml:

<root> 
    <node id="N1">
        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>
                    <year>2000</year>
                </attribute>
            </orange>                        
        </fruit>        

        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>
                    <condition>good</condition>
                </attribute>
            </orange>                        
        </fruit>        
    </node>
</root>

and here is the expected output:

<root> 
    <node id="N1">
        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>
                    <year>2000</year>
                    <condition>good</condition>
                </attribute>
            </orange>                        
        </fruit>        

        <fruit id="1">                                 
        </fruit>
    </node>
</root>

How to simplify between two sibling:

  1. check if the parent is the same (fruit id=1)
  2. check if the node id and action is the same (orange id=x action=create)
  3. if the child element is already defined previously and the value is the same (color-orange) , we remove it.
  4. If the child element of the second sibling is not defined perviously we add that second node to the first node. (condition-good)
  5. If the node is already defined previously but different value (say color-red), we leave the node as it is.

Another scenario:
input2:

<root> 
    <node id="N1">
        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>                   
                </attribute>
            </orange>                        
        </fruit>        

        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Red</color>
                    <condition>good</condition>
                </attribute>
            </orange>                        
        </fruit>        
    </node>
</root>

Expected ouput:

<root> 
    <node id="N1">
        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>
                    <condition>good</condition>
                </attribute>
            </orange>                        
        </fruit>        

        <fruit id="1">
            <orange id="x" action="create">
                <attribute>
                    <color>Red</color>
                </attribute>
            </orange>                        
        </fruit>        
    </node>
</root>

Another scenario:

<root> 
    <nodeA id="A">
        <fruit id="1">
            <orange id="x" action="delete" />    <!-- no attributes here -->                                         
        </fruit>        

        <fruit id="1">
            <orange id="x" action="delete"/>   
            <orange id="y" action="delete" />                                            
        </fruit>        
    </nodeA>
</root>

Expected output:

<root> 
    <nodeA id="A">
        <fruit id="1">
            <orange id="x" action="delete" />   
        </fruit>        

        <fruit id="1"> 
            <orange id="y" action="delete" />                                         
        </fruit>        
    </nodeA>
</root>

I hope the example give the clear idea and please help me with the transformation file.
Thanks.

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-03T05:36:19+00:00Added an answer on June 3, 2026 at 5:36 am

    John, here is a version that works. It’s somewhat brutal and procedural and so I wonder if you really want to do this kind of logic in XSLT. Here you go:

    The following stylesheet:

    <xsl:stylesheet version="2.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="entity" match="node/*/*" use="concat(parent::*/@id, '_', @id, '_', @action)"/>
    
        <xsl:template match="@* | node()">
            <xsl:copy>
                <xsl:apply-templates select="@* | node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="node/*/*[not(attribute)][generate-id() != generate-id(key('entity', concat(parent::*/@id, '_', @id, '_', @action))[1])]"/>
    
        <xsl:template match="node/*/*[attribute]">
            <xsl:variable name="attributes">
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()">
                        <xsl:with-param 
                                name="mode" 
                                select="generate-id() = generate-id(key('entity', concat(../@id, '_', @id, '_', @action))[1])"/>
                    </xsl:apply-templates>
                </xsl:copy>
            </xsl:variable>
            <xsl:if test="$attributes/*/attribute/*">
                <xsl:copy-of select="$attributes"/>
            </xsl:if>
        </xsl:template>
    
        <xsl:template match="node/*/*/attribute">
            <xsl:param name="mode"/>
            <xsl:variable name="all-attributes" select="key('entity', concat(../../@id, '_', ../@id, '_', ../@action))/attribute/*"/>
            <xsl:copy>
                <xsl:if test="$mode = true()">
                    <xsl:for-each-group select="$all-attributes" group-by="local-name()">
                        <xsl:copy>
                            <xsl:apply-templates select="@* | node()"/>
                        </xsl:copy>
                    </xsl:for-each-group>
                </xsl:if>
                <xsl:if test="$mode = false()">
                    <xsl:for-each select="*">
                        <xsl:variable 
                            name="same-name-attr" 
                            select="$all-attributes[local-name() = current()/local-name()][count(. | current()/preceding::*) = count(current()/preceding::*)]"/>
                        <xsl:if test="$same-name-attr and not(. = $same-name-attr)">
                            <xsl:copy-of select="."/>
                        </xsl:if>
                    </xsl:for-each>
                </xsl:if>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    produces the following result:

    <root>
       <node id="N1">
          <fruit id="1">
             <orange id="x" action="create">
                <attribute>
                   <color>Orange</color>
                   <year>2000</year>
                   <condition>good</condition>
                   <new>!!</new>
                </attribute>
             </orange>
          </fruit>
          <fruit id="1">
             <orange id="x" action="create">
                <attribute>
                   <color>Red</color>
                </attribute>
             </orange>
          </fruit>
          <fruit id="1">
             <orange id="x" action="create">
                <attribute>
                   <color>Blue</color>
                </attribute>
             </orange>
          </fruit>
          <fruit id="1">
             <orange id="x" action="create">
                <attribute>
                   <condition>ugly</condition>
                </attribute>
             </orange>
          </fruit>
          <fruit id="1"/>
       </node>
    </root>
    

    All unique attributes are pulled up into the first occurrence of create action, only those that have attributes of different values stay in following:: nodes. If the node has nothing new to add it’s left behind. Here’s how I figure out if the attribute is worth keeping for subsequent occurrences. If the attribute has not been seen before then it’s been already pulled up into the first occurrence so we skip it. If it’s been seen before (= it’s in the collection of same-name attributes on the preceding axes) and has a different text value then and only then we keep it.

    The selectors for what you want to do are getting progressively more complex so I had to use the temporary variables to basically let the templates take a shot at it and then examine if there’s any result to then decide if it’s worth copying into the result tree. There may be a way to convert this logic into match predicates but I am not sure it would be more readable. I hope it makes sense.

    UPDATE I updated the solution to also work for your no-attributes corner case. I basically had to silent the no-attribute nodes that are repetitive and also make the main template a little more specific to only work on nodes with attributes. The no-attribute nodes that would “repeat” a with-attribute node will be silent using the main attributes merge logic. The no-attributes nodes that need to stay will be copied over using the default identity transform.

    • 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 this HTML: <form action='uploadhandle.php' method='POST' enctype=multipart/form-data> <input type='file' class='fileinput' id='photo1' name='photo1'> <input
I have this simple piece of HTML code: <div> <input type=file name=english-file /> </div>
I have created a JAR file in this way jar cf jar-file input-files .
I have a checkbox in my vm file like this : <input name=ISPOperatorList[0].ISPOperatorAccessStatus id=OPERATORAccessDeny0
this is an example of my input file. <root> <!-- [...] --> <bbb>Foo 1</bbb>
I have this XML file: <movie id = 0> <Movie_name>The Shawshank Redemption </Movie_name> <Address>http://www.imdb.com/title/tt0111161/
I have an XML file that has a set of entries like this: <attr
I have a input file Main.xml <?xml version=1.0 encoding=utf-8 ?> <Employees> <Employee> <id name=id>1</id>
I have and input XML file that is not correctly formatted ( ie. it

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.