(XML version="1.0") I'm quite new to XSLT. I have been given the the following XML:
<SNAPSHOTS>
<SNAPSHOT>
<ID>1</ID>
<STATUS>COM</STATUS>
</SNAPSHOT>
<SNAPSHOT>
<ID>2</ID>
<STATUS>ACC</STATUS>
</SNAPSHOT>
</SNAPSHOTS>
As of my requirements I've managed to transformed the provided XML to be contained within a <DC> root element and include a header before the <SNAPSHOTS> element.
<?xml version="1.0" encoding="utf-8"?>
<DC>
<Header>
<FileName>TEST.xml</FileName>
<NoOfRecords>2</NoOfRecords>
</Header>
<SNAPSHOTS>
<SNAPSHOT>
<ID>1</ID>
<STATUS>COM</STATUS>
</SNAPSHOT>
<SNAPSHOT>
<ID>2</ID>
<STATUS>ACC</STATUS>
</SNAPSHOT>
</SNAPSHOTS>
<DC>
This is the XSD I've created:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<DC>
<Header>
<FileName>TEST.xml</FileName>
<NoOfRecords><xsl:value-of select="count(SNAPSHOTS/SNAPSHOT)"/></NoOfRecords>
</Header>
<xsl:apply-templates select="node()"/>
</DC>
</xsl:template>
<xsl:strip-space elements="*"/>
<xsl:template match="node()" name="removeNode">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When I include namespaces into the <DC> the namespace gets copied to the node also.
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<DC xmlns="http://TEST/TEST.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"> <<-- NAMESPACE ADDED
<Header>
<FileName>TEST.xml</FileName>
<NoOfRecords><xsl:value-of select="count(SNAPSHOTS/SNAPSHOT)"/></NoOfRecords>
</Header>
<xsl:apply-templates select="node()"/>
</DC>
Output:
<?xml version="1.0" encoding="utf-8"?>
<DC xmlns="http://TEST/TEST.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Header>
<FileName>TEST.xml</FileName>
<NoOfRecords>2</NoOfRecords>
</Header>
<SNAPSHOTS xmlns=""> <<--INCORRECT
<SNAPSHOT>
<ID>1</ID>
<STATUS>COM</STATUS>
</SNAPSHOT>
<SNAPSHOT>
<ID>2</ID>
<STATUS>ACC</STATUS>
</SNAPSHOT>
</SNAPSHOTS>
<DC>
I've tried many solutions (Googling 'XSLT copy node without namespace' etc.). One of the way's I've tried is the following (xsl copy nodes without xmlns) but It does not remove the xmlns=""
<xsl:apply-templates select="*" mode="copy-no-namespaces"/>
<xsl:template match="*" mode="copy-no-namespaces">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
<xsl:apply-templates select="node()" mode="copy-no-namespaces"/>
</xsl:element>
</xsl:template>
I've also read the following but don't fully understand the accepted answer. enter link description here
I have been working on this for hours and there is obviously something I just don't get. I sorry if I'm missing something simple considering similar questions have been asked before!! Many thanks your any help you can provide! Thomas