I am writing a XSLT script and my objective is to insert the namespace ( http://www.COMP.com/upp/readxml/09) with the root element. I wrote several variations and 2 of my codes partially resolves it.
- XSLT Code AB -- This one inserts the namespace but all the attribute values are concatenated and no tags are provided.
- XSLT Code PQ -- This one inserts the namespace but change the hierarchy of second node and put it as a root element.
CODE AB:
<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:tns="http://www.COMP.com/upp/readxml/09">
<xsl:output method="xml"/>
<xsl:template match="*">
<xsl:variable name="elname">
<xsl:text disable-output-escaping="yes">tns:</xsl:text>
<xsl:value-of select="local-name()"/>
</xsl:variable>
<xsl:element name="tns:{local-name()}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Code PQ:
<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="xml"/>
<xsl:template match="*" priority="1">
<xsl:element name="{local-name()}" namespace="http://www.COMP.com/upp/readxml/09">
<xsl:copy-of copy-namespaces="no" select="*[local-name() != 'RootDocument']"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
INPUT XML:
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<RootDocument xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<System Id="GOLD" />
<Creation_Datetime Datetime="2019-02-19T17:53:38Z" />
<Timezone Id="UTC" />
<CorrelationID Id="" />
</Header>
<Channels>
<Channel StartDate="2019-01-01T00:00:00-05:00" EndDate="2019-02-01T00:00:00-05:00" TimeZone="EasternUS">
<ChannelID ID="LC:2A" />
</Channel>
</Channels>
</RootDocument>
EXPECTED Output XML:
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<RootDocument xmlns="http://www.COMP.com/upp/readxml/09" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<System Id="GOLD" />
<Creation_Datetime Datetime="2019-02-19T17:53:38Z" />
<Timezone Id="UTC" />
<CorrelationID Id="" />
</Header>
<Channels>
<Channel StartDate="2019-01-01T00:00:00-05:00" EndDate="2019-02-01T00:00:00-05:00" TimeZone="EasternUS">
<ChannelID ID="LC:2A" />
</Channel>
</Channels>
</RootDocument>
OR
<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<RootDocument xmlns="http://www.COMP.com/upp/readxml/09">
<Header>
<System Id="GOLD" />
<Creation_Datetime Datetime="2019-02-19T17:53:38Z" />
<Timezone Id="UTC" />
<CorrelationID Id="" />
</Header>
<Channels>
<Channel StartDate="2019-01-01T00:00:00-05:00" EndDate="2019-02-01T00:00:00-05:00" TimeZone="EasternUS">
<ChannelID ID="LC:2A" />
</Channel>
</Channels>
</RootDocument>
Can you please suggest how to get the attribute correctly in XML or any other way to just insert namespace with keeping rest of the XML same.