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.
I. This XSLT 1.0 transformation:
When applied on the first XML document (file1.xml):
and having the second XML document reside at
c:\temp\delete\file2.xml
:produces the wanted, correct result:
Explanation:
Proper use of the "double-translate" method first demoed by Michael Kay.
II. XSLT 2.0 solution:
Explanation:
Proper use of the standard XPath 2.0 functions
matches()
andreplace()