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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:53:17+00:00 2026-05-28T20:53:17+00:00

I want to merge two files A.xml and map.xml with Node elements according to

  • 0

I want to merge two files A.xml and map.xml with “Node” elements according to the following rule (nodes are distinguished by @Name):

  1. If the element in map.xml has a Src attribute, the element from map should be copied to output
  2. If the element exists in A and map and does NOT have @Src, it should be copied from A
  3. If the element exists in A but not in map, it should be ignored (with warning)
  4. If the element exists in map but not in A, the (empty) element shall be generated

Example:

map.xml:

<?xml version="1.0"?>

<Node Name="ParentNode">
    <Node Name="Child1" Src="Child1/"/>
    <Node Name="Child2" Src="Child2/"/>
    <Node Name="Child3" Src="Child3/"/>

    <Node Name="Child4">
        <Node Name="Child4_Sub1" />
        <Node Name="Child4_Sub2" Src="Child4_Sub2/"/>
    </Node>

    <Node Name="Child5" />
</Node>

A.xml:

<Node Name="ParentNode">
    <Node Name="Child4">
        <Node Name="Child4">
            <Node Name="Child4_Sub1">
                <!-- Here are many other elements -->
            </Node>
        </Node>
    </Node> 
    <!-- Here are many other elements -->
    <Node Name="Child1">
        <!-- Here are many other elements -->
    </Node>
    <!-- Here are many other elements -->

    <Node Name="ChildFoo">
        <!-- Here are many other elements -->
    </Node>
</Node>

The result should be:

<Node Name="ParentNode">
    <Node Name="Child4">
        <Node Name="Child4">
            <Node Name="Child4_Sub1">
                <!-- Here are many other elements -->
            </Node>
            <Node Name="Child4_Sub2" />
        </Node>
    </Node> 
    <!-- Here are many other elements -->
    <Node Name="Child1" Src="Child1" />
    <!-- Here are many other elements -->

    <Node Name="Child2" Src="Child2" />
    <Node Name="Child3" Src="Child3" />
</Node>

My XSLT script is:

<?xml version="1.0"?>
<xsl:stylesheet version="2.0">
    <xsl:param name="mapFile" required="yes"/>

    <xsl:variable name="MapDiagram" select="document($mapFile,/*)"/>
    <xsl:variable name="CurrentDocument" select="/" />

    <!-- handle Node elements in A.xml -->
    <xsl:template match="Node">
        <xsl:variable name="MyName" select="@Name"/>
        <xsl:choose>
            <xsl:when test="$MapDiagram//Node[@Name = $MyName]">
                <xsl:choose>
                    <xsl:when test="$MapDiagram//Node[@Name = $MyName]/@Src">
                        <xsl:copy-of select="$MapDiagram//Node[@Name = $MyName]"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <Node Name="{@Name}" Type="{@Type}">
                            <xsl:apply-templates/>
                            <xsl:apply-templates select="$MapDiagram//Node[@Name = $MyName]" mode="MapDiagram" />
                        </Node>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
                <xsl:message terminate="no">WARNING: Node "<xsl:value-of select="@Name"/>" not found in map file, ignoring</xsl:message>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- handle Node elements from map file -->
    <xsl:template match="Node" mode="MapDiagram">
        <xsl:variable name="MyName" select="@Name"/>
        <xsl:choose>
            <xsl:when test="not($CurrentDocument//Node[@Name = $MyName])">
                <xsl:copy-of select="."/>
            </xsl:when>
            <xsl:otherwise>
                    <xsl:apply-templates mode="MapDiagram" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

    <!-- Copy all other elements in between -->
    <xsl:template match="*[name() != 'Node']">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

The script works fine. It processes A.xml and looks up each Node element in map.xml. Since @Src and non-@Src Nodes can be mixed, it is called recursively.

However, this script generates:

<Node Name="ParentNode">
    <Node Name="Child4">
        <Node Name="Child4">
            <Node Name="Child4_Sub1">
                <!-- Here are many other elements -->
            </Node>
            <Node Name="Child4_Sub2" />
        </Node>
    </Node> 
    <!-- Here are many other elements -->
    <Node Name="Child1" Src="Child1" />
    <!-- Here are many other elements -->

    <Node Name="Child2" Src="Child2" />
    <Node Name="Child3" Src="Child3" />
    <Node Name="Child4_Sub2" />
</Node>

So, the Child4_Sub2 is generated twice which is non-sense since Child4_Sub2 would need Child4 as parent anyway! But so far I found no way to prevent this element from being printed.

Do you have any hints?

Regards,
divB

  • 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-05-28T20:53:18+00:00Added an answer on May 28, 2026 at 8:53 pm

    Change:

          <xsl:apply-templates mode="MapDiagram" select=
            "$MapDiagram//Node[@Name = $MyName]"/>
    

    to:

        <xsl:if test="not(@Name = ancestor::Node/@Name)">
          <xsl:apply-templates mode="MapDiagram" select=
            "$MapDiagram//Node[@Name = $MyName]"/>
        </xsl:if>
    

    Here is a complete solution:

    <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:param name="mapFile"
            select="'file:///c:/temp/delete/map.xml'"/>
    
            <xsl:variable name="MapDiagram" select="document($mapFile,/*)"/>
            <xsl:variable name="CurrentDocument" select="/" />
    
            <!-- handle Node elements in A.xml -->
            <xsl:template match="Node">
                <xsl:variable name="MyName" select="@Name"/>
                <xsl:choose>
                    <xsl:when test="$MapDiagram//Node[@Name = $MyName]">
    
                        <xsl:choose>
                            <xsl:when test="$MapDiagram//Node[@Name = $MyName]/@Src">
                                <xsl:copy-of select="$MapDiagram//Node[@Name = $MyName]"/>
                            </xsl:when>
                            <xsl:otherwise>
                                <Node Name="{@Name}" Type="{@Type}">
                                    <xsl:apply-templates/>
    
                                    <xsl:if test="not(@Name = ancestor::Node/@Name)">
                                      <xsl:apply-templates mode="MapDiagram" select=
                                         "$MapDiagram//Node[@Name = $MyName]"
                                      />
                                    </xsl:if>
                                </Node>
                            </xsl:otherwise>
                        </xsl:choose>
    
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:message terminate="no">WARNING: Node "<xsl:value-of select="@Name"/>" not found in map file, ignoring</xsl:message>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:template>
    
            <!-- handle Node elements from map file -->
            <xsl:template match="Node" mode="MapDiagram">
                <xsl:variable name="MyName" select="@Name"/>
                <xsl:choose>
                    <xsl:when test="not($CurrentDocument//Node[@Name = $MyName])">
                        <xsl:copy-of select="."/>
                    </xsl:when>
                    <xsl:otherwise>
                            <xsl:apply-templates mode="MapDiagram" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:template>
    
            <!-- Copy all other elements in between -->
            <xsl:template match="*[name() != 'Node']">
                <xsl:copy-of select="."/>
            </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied to the provided XML document:

    <Node Name="ParentNode">
        <Node Name="Child4">
            <Node Name="Child4">
                <Node Name="Child4_Sub1"/>
            </Node>
        </Node>
    
        <Node Name="Child1"/>
    
        <Node Name="ChildFoo"/>
    </Node>
    

    and the provided “map.xml” is at C:\temp\delete\map.xml:

    <Node Name="ParentNode">
        <Node Name="Child1" Src="Child1/"/>
        <Node Name="Child2" Src="Child2/"/>
        <Node Name="Child3" Src="Child3/"/>
        <Node Name="Child4">
            <Node Name="Child4_Sub1" />
            <Node Name="Child4_Sub2" Src="Child4_Sub2/"/>
        </Node>
        <Node Name="Child5" />
    </Node>
    

    The wanted result (not containing the unwanted repetition) is produced:

    <Node Name="ParentNode" Type="">
       <Node Name="Child4" Type="">
          <Node Name="Child4" Type="">
             <Node Name="Child4_Sub1" Type=""/>
          </Node>
          <Node Name="Child4_Sub2" Src="Child4_Sub2/"/>
       </Node>
       <Node Name="Child1" Src="Child1/"/>
       <Node Name="Child2" Src="Child2/"/>
       <Node Name="Child3" Src="Child3/"/>
       <Node Name="Child4_Sub2" Src="Child4_Sub2/"/>
       <Node Name="Child5"/>
    </Node>
    

    General note: The code that is provided is quite complicated and messy — there might be other logical issues with it. No XSLT 2.0 language features are used — this is essentially an XSLT 1.0 solution. It would be a good idea to rewrite your code in a better form.

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

Sidebar

Related Questions

I have two STL containers that I want to merge, removing any elements that
I want to take advantage of the git-merge algorithm on two arbitrary files in
I have two XML files. They are similar, but there are two nodes of
I need to merge two XML documents, overwriting the overlapsed attributes and elements. For
I want to merge two CSV files that I have read into python using
I want to merge two branches that have been separated for a while and
I want to merge two cells in excel using pyExcelerator , ws.write_merge(r1=0,r2=1,c1=0, c2=0, label='test1',
I'm using classic ASP in my project. I want to merge two XMLs together.
I have two objects and I want to merge them: public class Foo {
I have two mysql tables and i want to merge the results of these

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.