I have an XML that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2020-08-28T20:55:05">
<BISTA>
<ID>6</ID>
<version>1.0</version>
</BISTA>
</dataroot>
I need to delete the 'dataroot' element, make the 'version' element an attribute and define a custom namespace for 'BISTA'. In the end it should look like this:
<BISTA xmlns="http://www.test.com" version="1.0">
<ID>6</ID>
</BISTA>
The XSLT I am using is to achieve this is
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<!-- template for the document element (omit root) -->
<xsl:template match="/*">
<xsl:apply-templates select="node()" />
</xsl:template>
<!-- identity template -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<!-- elements of root to attributes -->
<xsl:template match="BISTA">
<xsl:element name="{name()}" namespace="http://www.test.com">
<xsl:for-each select="version">
<xsl:attribute name="{name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<!-- delete version element -->
<xsl:template match="BISTA/version"/>
</xsl:stylesheet>
What I get from this is
<?xml version="1.0" encoding="UTF-8"?>
<BISTA xmlns="http://www.test.com" version="1.0">
<ID xmlns="" xmlns:od="urn:schemas-microsoft-com:officedata">6</ID>
</BISTA>
Issues with this:
- the indentation looks off
- there is an empty namespace in 'ID'
- the unwanted xmlns:od attribute
What am I doing wrong? How can I achieve the desired output?