0
votes

I have this XML file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<NAXML-MaintenanceRequest version="3.4" xmlns="http://www.naxml.org/POSBO/Vocabulary/2003-10-16">
    <TransmissionHeader>
        <StoreLocationID>AB123</StoreLocationID>
        <VendorName>VeriFone</VendorName>
        <VendorModelVersion>Commander Site Controller</VendorModelVersion>
    </TransmissionHeader>
    <ComboMaintenance>
        <TableAction type="initialize"/>
        <RecordAction type="addchange"/>
    </ComboMaintenance>
</NAXML-MaintenanceRequest>

I would like to get here, simply removing the xmlns="" on the NAXML-MaintenanceRequest node:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<NAXML-MaintenanceRequest version="3.4">
    <TransmissionHeader>
        <StoreLocationID>AB123</StoreLocationID>
        <VendorName>VeriFone</VendorName>
        <VendorModelVersion>Commander Site Controller</VendorModelVersion>
    </TransmissionHeader>
    <ComboMaintenance>
        <TableAction type="initialize"/>
        <RecordAction type="addchange"/>
    </ComboMaintenance>
</NAXML-MaintenanceRequest>

I've seen ways to remove the default namespace on other elements, but never the root node. Could someone point me to a resource/provide a snippet that simply takes off the default namespace on the root node?

2

2 Answers

3
votes

First, stop and consider whether removing namespaces from your XML is really what you should be doing. XML namespaces perform an important role in vocabulary management. Although they introduce a bit of complication, being able to manage XML vocabularies is a great advantage and shouldn't be defeated without just cause.

Mechanically, yes, XSLT is an ideal tool for mapping from one XML document to another and can easily remove elements from namespaces...

Simply adapt the identify transformation to copy over all nodes as-is, except handle elements specially, removing them from any namespace:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|processing-instruction()|comment()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
1
votes
<xsl:template match="@*|processing-instruction()|comment()">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
       <xsl:element name="{local-name()}">
           <xsl:if test="@version">
               <xsl:attribute name="version">
                   <xsl:value-of select="@version"/>
               </xsl:attribute>
           </xsl:if>
           <xsl:apply-templates/>
       </xsl:element>
    </xsl:template>
    <xsl:template match="m:TableAction">
       <xsl:element name="{ local-name()}">
            <xsl:apply-templates select="@*"/>
           <xsl:apply-templates/>
       </xsl:element>
    </xsl:template>
    <xsl:template match="m:RecordAction">
        <xsl:element name="{ local-name()}">
            <xsl:apply-templates select="@*"/>
        </xsl:element>
    </xsl:template>
check it