从 Python 中的整个 xml 中删除特定属性
Remove specific attribute from entire xml in Python
我需要使用 Python 从 XML 中删除所有 id 属性。它将成为更大应用程序的一部分,并将成为之后一些转换的输入。
示例代码:
<body>
<r1 format="bold" id="NODE1">
<r2 title="Test" id="NODE2">
<r3 group="123" type="Operation" id="NODE3">
<rtit id="NODE4">Evaluate the temperature</rtit>
<procedure id="NODE5">
<procstep id="NODE6">
<graphelem id="NODE7">
<graphic graphicname="T123456" res_width="3.58in" scale="70" id="NODE8"/>
</graphelem>
<proct>Remove the screws. Remove the plates.</proct>
</procstep>
<procstep id="NODE9">
<graphelem id="NODE10">
<graphic graphicname="T654321" res_width="3.58in" scale="70" id="NODE11"/>
</graphelem>
<proct>Fix the thermocouple in the cover.</proct>
</procstep>
</procedure>
</r3>
</r2>
</r1>
</body>
源文件有 1000 多行,以及 30 多个不同的 XML 标签,其中包含 id 属性。
预期结果是:
<body>
<r1 format="bold">
<r2 title="Test">
<r3 group="123" type="Operation">
<rtit>Evaluate the temperature</rtit>
<procedure>
<procstep>
<graphelem>
<graphic graphicname="T2093978" res_width="3.58in" scale="70"/>
</graphelem>
<proct>Remove the screws. Remove the plates.</proct>
</procstep>
<procstep>
<graphelem>
<graphic graphicname="T654321" res_width="3.58in" scale="70"/>
</graphelem>
<proct>Fix the thermocouple in the cover.</proct>
</procstep>
</procedure>
</r3>
</r2>
</r1>
</body>
除了 id 属性之外,我尝试使用 xslt 进行转换,但没有任何成功。
有人帮我解决这个问题吗?
I need to remove all id attributes from XML using Python.
类似于下面的内容 - 遍历所有元素并删除 'id' 属性
import xml.etree.ElementTree as ET
xml = '''<body><r1 format="bold" id="NODE1">
<r2 title="Test" id="NODE2">
<r3 group="123" type="Operation" id="NODE3">
<rtit id="NODE4">Evaluate the temperature</rtit>
<procedure id="NODE5">
<procstep id="NODE6">
<graphelem id="NODE7">
<graphic graphicname="T123456" res_width="3.58in" scale="70" id="NODE8"/>
</graphelem>
<proct>Remove the screws. Remove the plates.</proct>
</procstep>
<procstep id="NODE9">
<graphelem id="NODE10">
<graphic graphicname="T654321" res_width="3.58in" scale="70" id="NODE11"/>
</graphelem>
<proct>Fix the thermocouple in the cover.</proct>
</procstep>
</procedure>
</r3>
</r2>
</r1>
</body>'''
root = ET.fromstring(xml)
for elem in root.iter():
if 'id' in elem.attrib:
del elem.attrib['id']
ET.dump(root)
我需要使用 Python 从 XML 中删除所有 id 属性。它将成为更大应用程序的一部分,并将成为之后一些转换的输入。
示例代码:
<body>
<r1 format="bold" id="NODE1">
<r2 title="Test" id="NODE2">
<r3 group="123" type="Operation" id="NODE3">
<rtit id="NODE4">Evaluate the temperature</rtit>
<procedure id="NODE5">
<procstep id="NODE6">
<graphelem id="NODE7">
<graphic graphicname="T123456" res_width="3.58in" scale="70" id="NODE8"/>
</graphelem>
<proct>Remove the screws. Remove the plates.</proct>
</procstep>
<procstep id="NODE9">
<graphelem id="NODE10">
<graphic graphicname="T654321" res_width="3.58in" scale="70" id="NODE11"/>
</graphelem>
<proct>Fix the thermocouple in the cover.</proct>
</procstep>
</procedure>
</r3>
</r2>
</r1>
</body>
源文件有 1000 多行,以及 30 多个不同的 XML 标签,其中包含 id 属性。
预期结果是:
<body>
<r1 format="bold">
<r2 title="Test">
<r3 group="123" type="Operation">
<rtit>Evaluate the temperature</rtit>
<procedure>
<procstep>
<graphelem>
<graphic graphicname="T2093978" res_width="3.58in" scale="70"/>
</graphelem>
<proct>Remove the screws. Remove the plates.</proct>
</procstep>
<procstep>
<graphelem>
<graphic graphicname="T654321" res_width="3.58in" scale="70"/>
</graphelem>
<proct>Fix the thermocouple in the cover.</proct>
</procstep>
</procedure>
</r3>
</r2>
</r1>
</body>
除了 id 属性之外,我尝试使用 xslt 进行转换,但没有任何成功。
有人帮我解决这个问题吗?
I need to remove all id attributes from XML using Python.
类似于下面的内容 - 遍历所有元素并删除 'id' 属性
import xml.etree.ElementTree as ET
xml = '''<body><r1 format="bold" id="NODE1">
<r2 title="Test" id="NODE2">
<r3 group="123" type="Operation" id="NODE3">
<rtit id="NODE4">Evaluate the temperature</rtit>
<procedure id="NODE5">
<procstep id="NODE6">
<graphelem id="NODE7">
<graphic graphicname="T123456" res_width="3.58in" scale="70" id="NODE8"/>
</graphelem>
<proct>Remove the screws. Remove the plates.</proct>
</procstep>
<procstep id="NODE9">
<graphelem id="NODE10">
<graphic graphicname="T654321" res_width="3.58in" scale="70" id="NODE11"/>
</graphelem>
<proct>Fix the thermocouple in the cover.</proct>
</procstep>
</procedure>
</r3>
</r2>
</r1>
</body>'''
root = ET.fromstring(xml)
for elem in root.iter():
if 'id' in elem.attrib:
del elem.attrib['id']
ET.dump(root)