'Insert XML with Namespace into source xml with using XSLT

I have scenario where I want to insert or append (insert.xml) into main xml (source.xml) using xslt. Namespace is one of the key constrain here the source.xml contain all the namespace that insert.xml has.

My source file look like this (see below)

source.xml :

<?xml version="1.0" encoding="UTF-8"?> 
<tns:Requests xmlns:tns="http://mynamespace.com/request/wrapper/2022"
             xmlns:tns1="http://mynamespace.com/request/2022"
             xmlns:tns2="http://mynamespace.com/clientresponse/2022"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tns:Request>
        <tns:Data>
            <tns:row1>Data1</tns:row1>
            <tns:row2>Data2</tns:row2>
        </tns:Data>
    </tns:Request> 
</tns:Requests> ```

my insert file looks like this (see below)

Insert.xml

<?xml version="1.0" encoding="UTF-8"?>
<tns:ServiceResponse xmlns:tns="http://mynamespace.com/request/2022"
                    xmlns:tns1="http://mynamespace.com/clientresponse/2022"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <tns:ClientList>
       <tns1:Client>
           <tns1:name>John</tns1:name>
           <tns1:address>11 Wall street </tns1:address>
           <tns1:pin>7895</tns1:pin>
           <tns1:contact>78945615</tns1:contact>
       </tns1:Client>
   </tns:ClientList> 
</tns:ServiceResponse>

I want the output like this (see below)

Expected output:


<?xml version="1.0" encoding="UTF-8"?> 
<tns:Requests xmlns:tns="http://mynamespace.com/request/wrapper/2022"
             xmlns:tns1="http://mynamespace.com/request/2022"
             xmlns:tns2="http://mynamespace.com/clientresponse/2022"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tns:Request>
        <tns:data>
            <tns:row1>Data1</tns:row1>
            <tns:row2>Data2</tns:row2>
        </tns:data>
        <tns1:ClientList>
            <tns2:Client>
                <tns2:name>John</tns2:name>
                <tns2:address>11 Wall street </tns2:address>
                <tns2:pin>7895</tns2:pin>
                <tns2:contact>78945615</tns2:contact>
            </tns2:Client>
        </tns1:ClientList>
    </tns:Request> 
</tns:Requests>

my xsl file looks like this (see below)

Xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
               xmlns:tns1="http://mynamespace.com/request/wrapper/2022"
               xmlns:tns="http://mynamespace.com/request/2022"
               version="2.0">
    
    <xsl:output method="xml" version="1.0" indent="yes"/>
    
    <xsl:template match="*">
        <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>    
    <xsl:template match="tns1:Requests/tns1:Request/tns1:Data">
        <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
        <xsl:copy-of select="document('insert.xml')/tns:ServiceResponse/tns:ClientList"/>

    </xsl:template>
</xsl:transform>

i am getting the output as below xslt output:

<?xml version="1.0" encoding="UTF-8"?>
<Requests xmlns="http://mynamespace.com/request/wrapper/2022">
    <Request>
        <Data xmlns="">
            <row1 xmlns="http://mynamespace.com/request/wrapper/2022">Data1</row1>
            <row2 xmlns="http://mynamespace.com/request/wrapper/2022">Data2</row2>
        </Data>
        <tns:ClientList xmlns:tns="http://mynamespace.com/request/2022"
                        xmlns:tns1="http://mynamespace.com/clientresponse/2022"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <tns1:Client>
                <tns1:name>John</tns1:name>
                <tns1:address>11 Wall street </tns1:address>
                <tns1:pin>7895</tns1:pin>
                <tns1:contact>78945615</tns1:contact>
            </tns1:Client>
        </tns:ClientList>
    </Request> 
</Requests>


Solution 1:[1]

I think you want

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xpath-default-namespace="http://mynamespace.com/request/wrapper/2022"
  xmlns:tns1="http://mynamespace.com/request/2022"
  xmlns:tns2="http://mynamespace.com/clientresponse/2022"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Requests/Request/Data">
      <xsl:next-match/>
      <xsl:apply-templates select="document('insert.xml')/tns1:ServiceResponse/tns1:ClientList"/>
  </xsl:template>
  
  <xsl:template match="tns1:*">
    <xsl:element name="tns1:{local-name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="tns2:*">
    <xsl:element name="tns2:{local-name()}" namespace="{namespace-uri()}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Honnen