Skip to content Skip to sidebar Skip to footer

Suppressing Namespace Prefix In Xml File

Input File:

Solution 1:

Here is how to use XSLT for the task.

The XSLT is following so called Identity Transform pattern.

Input XML

<?xml version="1.0" encoding="utf-8"?><Typesxmlns="http://schemas.openxmlformats.org/package/2006/content-types"><DefaultExtension="json"ContentType=""/><OverridePartName="/Version"ContentType=""/></Types>

XSLT

<?xml version="1.0"?><xsl:stylesheetversion="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:ns1="http://schemas.openxmlformats.org/package/2006/content-types"exclude-result-prefixes="ns1"><xsl:outputmethod="xml"encoding="utf-8"indent="yes"omit-xml-declaration="no"/><xsl:strip-spaceelements="*"/><xsl:templatematch="@*|node()"><xsl:copy><xsl:apply-templatesselect="@*|node()"/></xsl:copy></xsl:template><xsl:templatematch="ns1:Override"><xsl:copy-ofselect="."/><DefaultExtension="png"ContentType=""/></xsl:template></xsl:stylesheet>

Output XML

<?xml version='1.0' encoding='utf-8' ?><Typesxmlns="http://schemas.openxmlformats.org/package/2006/content-types"><DefaultExtension="json"ContentType=""/><OverridePartName="/Version"ContentType=""/><DefaultExtension="png"ContentType=""/></Types>

Solution 2:

register_namespace() sets the prefix to be used when serializing the XML. Since you don't want any prefix, use an empty string.

All you need to do is to change

ET.register_namespace('xmlns',"http://schemas.openxmlformats.org/package/2006/content-types")` 

to

ET.register_namespace('',"http://schemas.openxmlformats.org/package/2006/content-types")

Post a Comment for "Suppressing Namespace Prefix In Xml File"