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>