使用lxml
,我不确定如何正确删除现有元素的名称空间并设置一个新元素。
例如,我正在解析这个最小的xml文件:
<myroot xmlns="http://myxml.com/somevalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
...并且我希望它成为:
<myroot xmlns="http://myxml.com/newvalue">
<child1>blabla/child1>
<child2>blablabla</child2>
</myroot>
使用lxml
:
from lxml import etree as ET
tree = ET.parse('myfile.xml')
root= tree.getroot()
如果我检查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'
理想情况下,我想得出以下结论:
In [8]: root.nsmap
Out[8]: {None: 'http://myxml.com/newvalue'}
In [11]: root.tag
Out[11]: '{http://myxml.com/newvalue}myroot'
至于标签,只需要设置正确的字符串即可。 nsmap
怎么样?
参考方案
我同意mzjn和Parfait;我将使用XSLT更改名称空间。
通过将新旧名称空间作为参数传入,可以使XSLT相当可重用。
例...
XML输入(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>
蟒蛇
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"))
输出量
<myroot xmlns="http://myxml.com/newvalue">
<child1>blabla</child1>
<child2>blablabla</child2>
</myroot>
另外,如果您使用以下输入(使用名称空间前缀)...
<ns1:myroot xmlns:ns1="http://myxml.com/somevalue">
<ns1:child1>blabla</ns1:child1>
<ns1:child2>blablabla</ns1:child2>
</ns1:myroot>
你得到这个输出...
<ns1:myroot xmlns:ns1="http://myxml.com/newvalue">
<ns1:child1>blabla</ns1:child1>
<ns1:child2>blablabla</ns1:child2>
</ns1:myroot>
有关将XSLT与lxml结合使用的更多信息,请参见https://lxml.de/xpathxslt.html。
Python ElementTree:在循环中替换元素 - python我正在尝试创建一个脚本,该脚本循环创建一个xml文件,并为两个元素增加值。 (使用netaddr的IP地址,以及递增的tag / member元素,tag01-tag10)from netaddr import IPNetwork import xml.dom.minidom import lxml.etree as etree import xml.etr…
Python Pandas导出数据 - python我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…
将python scikit学习模型导出到pmml - python我想将python scikit-learn模型导出到PMML。哪个python软件包最合适?我阅读了有关Augustus的内容,但是我无法使用scikit-learn模型找到任何示例。 python大神给出的解决方案 SkLearn2PMML是 JPMML-SkLearn命令行应用程序周围的薄包装。有关受支持的Scikit-Learn Estimator和…
按列名称显示的Python Selenium复制表列 - python我有一个包含这些标题的表,如下所示:如何使用xpath选择整列存储在数组中。我希望使用不同的数组,例如:courses = [] teacher = [] avg = [] 请记住,这些列没有任何ID或类,因此我需要一种仅使用列名进行选择的方法。这是表格的代码:<table border="0"> <tbody> …
如何使用BeautifulSoup在<tr>中捕获特定的<td> - python尝试从nyc Wiki页面中的高中列表中获取所有高中名称。我已经写了足够多的脚本,可以让我获取包含在高中,学业和入学条件列表的表的<tr>标记中的所有信息-但是我如何才能缩小到我认为的范围内在td[0]内休息(会弹出KeyError)-只是学校的名称?到目前为止我写的代码:from bs4 import BeautifulSoup from ur…