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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:53:54+00:00 2026-06-11T08:53:54+00:00

Please i want to know how i can merge two xml files into one

  • 0

Please i want to know how i can merge two xml files into one xml file using xslt.
I have these both xml files that i want to merge into an xml file as shown in the expected output.I want to include the each node Hn from the second file to the corresponding block with same number in the file 1.

file1:

<Test>
  <R1>
    <Th1>
        here are some instruction.
    </Th1>
  </R1>

  <R2>
    <Th2>
        here are some instruction.
    </Th2>
  </R2>

  <R3>
    <Th3>
        here are some instruction.
    </Th3>
  </R3>
</Test>

file 2:

<test1>
  <H1>
    here are some instruction.
  </H1>

  <H2>
    here are some instruction.
  </H2>

  <H3>
    here are some instruction.
  </H3>
</test1>    

and here is the expected output:

<test2>
  <R1>
    <H1>
        here are some instruction.
    </H1>
    <Th1>
        here are some instruction.
    </Th1>
  </R1>

  <R2>
    <H2>
        here are some instruction.
    </H2>
    <Th2>
        here are some instruction.
    </Th2>
  </R2>

  <R3>
    <H3>
        here are some instruction.
    </H3>
    <Th3>
        here are some instruction.
    </Th3>
  </R3>

</test2>

Thanks for your help.

  • 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-11T08:53:55+00:00Added an answer on June 11, 2026 at 8: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"/>
    
     <xsl:variable name="vDigits" select="'0123456789'"/>
     <xsl:variable name="vDoc2" select="document('file:///c:/temp/delete/file2.xml')"/> 
    
     <xsl:template match="/*">
         <test2><xsl:apply-templates/></test2>
     </xsl:template>
    
     <xsl:template match="/*/*">
      <xsl:copy>
       <xsl:text>&#xA;</xsl:text>
       <xsl:copy-of select=
        "$vDoc2/*/*
          [translate(name(),translate(name(),$vDigits,''),'')
          =
           translate(name(current()),translate(name(current()),$vDigits,''),'')
          ]"/>
        <xsl:copy-of select="node()"/>
       </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    When applied on the first XML document (file1.xml):

    <Test>
      <R1>
        <Th1>
            here are some instruction.
        </Th1>
      </R1>
    
      <R2>
        <Th2>
            here are some instruction.
        </Th2>
      </R2>
    
      <R3>
        <Th3>
            here are some instruction.
        </Th3>
      </R3>
    </Test>
    

    and having the second XML document reside at c:\temp\delete\file2.xml:

    <test1>
      <H1>
        here are some instruction.
      </H1>
    
      <H2>
        here are some instruction.
      </H2>
    
      <H3>
        here are some instruction.
      </H3>
    </test1>
    

    produces the wanted, correct result:

    <test2>
      <R1>
    <H1>
        here are some instruction.
      </H1>
        <Th1>
            here are some instruction.
        </Th1>
      </R1>
    
      <R2>
    <H2>
        here are some instruction.
      </H2>
        <Th2>
            here are some instruction.
        </Th2>
      </R2>
    
      <R3>
    <H3>
        here are some instruction.
      </H3>
        <Th3>
            here are some instruction.
        </Th3>
      </R3>
    </test2>
    

    Explanation:

    Proper use of the “double-translate” method first demoed by Michael Kay.


    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"/>
    
     <xsl:variable name="vDigits" select="'0123456789'"/>
     <xsl:variable name="vDoc2" select="document('file:///c:/temp/delete/file2.xml')"/> 
    
     <xsl:template match="/*">
         <test2><xsl:apply-templates/></test2>
     </xsl:template>
    
     <xsl:template match="/*/*">
      <xsl:if test="not(matches(name(), '^.+\d+$'))">
        <xsl:message terminate="yes">
         Element Name doesn't end with number: <xsl:sequence select="name()"/>
        </xsl:message>
      </xsl:if>
      <xsl:copy>
       <xsl:text>&#xA;</xsl:text>
       <xsl:copy-of select=
        "$vDoc2/*/*
          [replace(name(),'^.+(\d+)$', '$1')
          =
           replace(name(current()),'^.+(\d+)$', '$1')
          ]"/>
        <xsl:copy-of select="node()"/>
       </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    Explanation:

    Proper use of the standard XPath 2.0 functions matches() and replace()

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

Sidebar

Related Questions

Almighty Gurus, Please tell me, I want to know can comparison sm. set of
I'm developing a website using php, i want to know how can i protect
I want to know : How can I know that two imageViews are overlapping
I'm using Tomcat 6.0 as a webserver, and I want to know how can
if you please I just want to know how can I replay to sms
Please see following code I want to know how can I de-alloc cell .
I want to know how can I create mail alert using jQuery mobile (javascript,
please help me. I want know about what types of flags to run an
I want to change my question. please let me know what's happening; type is
I want to display NSE/BSE quote in my PHP website. please let me know

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.