With lxml
, I am not sure how to properly remove the namespace of an existing element and set a new one.
For instance, I'm parsing this minimal xml file:
<myroot xmlns="http://myxml.com/somevalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
... and I'd like it to become:
<myroot xmlns="http://myxml.com/newvalue">
<child1>blabla/child1>
<child2>blablabla</child2>
</myroot>
With lxml
:
from lxml import etree as ET
tree = ET.parse('myfile.xml')
root= tree.getroot()
If I inspect root
:
In [7]: root
Out[7]: <Element {http://myxml.com/somevalue}myroot at 0x7f6e13832588>
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/somevalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/somevalue}myroot'
Ideally, I would like to end up with:
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/newvalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/newvalue}myroot'
As for the tag, it's just a matter of setting the right string. How about nsmap
?
I agree with mzjn and Parfait; I'd use XSLT to change the namespace.
You can make the XSLT fairly reusable by having the old and new namespaces passed in as parameters.
Example...
XML Input (input.xml)
<myroot xmlns="http://myxml.com/somevalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
XSLT 1.0 (test.xsl)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="orig_namespace"/>
<xsl:param name="new_namespace"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*" priority="1">
<xsl:choose>
<xsl:when test="namespace-uri()=$orig_namespace">
<xsl:element name="{name()}" namespace="{$new_namespace}">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Python
from lxml import etree
tree = etree.parse("input.xml")
xslt = etree.parse("test.xsl")
orig_namespace = "http://myxml.com/somevalue"
new_namespace = "http://myxml.com/newvalue"
new_tree = tree.xslt(xslt, orig_namespace=f"'{orig_namespace}'",
new_namespace=f"'{new_namespace}'")
print(etree.tostring(new_tree, pretty_print=True).decode("utf-8"))
Output
<myroot xmlns="http://myxml.com/newvalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
Also, if you use the following input (that uses a namespace prefix)...
<ns1:myroot xmlns:ns1="http://myxml.com/somevalue">
<ns1:child1>blabla</ns1:child1>
<ns1:child2>blablabla</ns1:child2>
</ns1:myroot>
you get this output...
<ns1:myroot xmlns:ns1="http://myxml.com/newvalue">
<ns1:child1>blabla</ns1:child1>
<ns1:child2>blablabla</ns1:child2>
</ns1:myroot>
See https://lxml.de/xpathxslt.html for more info on using XSLT with lxml.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.