【问题标题】:Remove specific attribute from entire xml in Python从 Python 中的整个 xml 中删除特定属性
【发布时间】:2022-01-09 19:11:56
【问题描述】:

我需要使用 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 多个包含 id 属性的不同 XML 标记。

预期结果是:

<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进行转换,但没有任何成功。

有人帮我解决这个问题吗?

【问题讨论】:

  • 为什么不发布您的尝试以便我们修复它,而不必从头开始为您编写代码。
  • 您好,欢迎来到 SO。您表明您正在努力解决您的问题,这对社区很重要。在我看来,最好的方法是包含你目前拥有的基于 text 的源代码版本,即使它运行不正确。
  • 在询问 XSLT 问题时,您需要提供 minimal reproducible example: (1) 输入 XML。 (2) 你的逻辑,以及试图实现它的 XSLT。 (3) 所需的输出,基于上面 #1 中的示例 XML。 (4) XSLT 处理器及其对 XSLT 标准的遵从性:1.0、2.0 或 3.0。
  • 抱歉信息缺失。我使用的 XSLT 有一千多行,我相信阅读它会浪费您的时间。 elementtree 方法会更好。
  • XSLT 有一个所谓的 Identity Transform 模式。对于你的情况,它是一行 XSLT 模板,可以完成这项工作。

标签: python xml xslt-1.0 elementtree


【解决方案1】:

我需要使用 Python 从 XML 中删除所有 id 属性。

类似下面的 - 循环遍历所有元素并删除 '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)

【讨论】:

  • 太棒了!它工作正常。谢谢!
猜你喜欢
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-30
相关资源
最近更新 更多