查找并删除XML文件中的子元素

问题描述 投票:0回答:1

我是 Python 新手,所以这是我的问题:

XML:

<Configuration>
   <ConfiguredPaths>
       <ConfiguredPath>
           <LocalPath>C:\Temp</LocalPath>
           <EffectivePath>\\SERVERNAME\C$\Temp</EffectivePath>
       </ConfiguredPath>
       <ConfiguredPath>
           <LocalPath>C:\Files</LocalPath>
           <EffectivePath>\\SERVERNAME\C$\Files</EffectivePath>
       </ConfiguredPath>
       <ConfiguredPath>
           <LocalPath>C:\DOCS</LocalPath>
           <EffectivePath>\\SERVERNAME\C$\DOCS</EffectivePath>
       </ConfiguredPath>
   </ConfiguredPaths>
</Configuration>

我需要做的是找到元素“EffectivePath”(如果它等于某个值) 删除它所属的整个部分。由于是“ConfiguredPath”的子级(需要删除与该特定有效路径相关的部分)

这是一个示例结果,如果 有效路径 = "\SERVERNAME\C$\DOCS"

=> 结果 XML 文件应如下所示:

<Configuration>
   <ConfiguredPaths>
       <ConfiguredPath>
           <LocalPath>C:\Temp</LocalPath>
           <EffectivePath>\\SERVERNAME\C$\Temp</EffectivePath>
       </ConfiguredPath>
       <ConfiguredPath>
           <LocalPath>C:\Files</LocalPath>
           <EffectivePath>\\SERVERNAME\C$\Files</EffectivePath>
       </ConfiguredPath>
   </ConfiguredPaths>
</Configuration>

这是我的脚本;然而,它删除了所有已配置路径(及其子项),而不是仅删除了所需的路径:

import xml.etree.ElementTree as ET
tree = ET.parse('Data.xml')
root = tree.getroot()

for child in root:
    if child.tag == "ConfiguredPaths":
        for elem in child.iter():
            if elem.tag == "ConfiguredPath":
                for child_elem in child.iter():
                    if child_elem.tag == "EffectivePath" and child_elem.text == r"\\SERVERNAME\C$\DOCS":
                        print(f"Required item is:", child_elem.tag, child_elem.text)
                        root.remove(child)

tree.write('output.xml') 
python xml parsing elementtree
1个回答
0
投票

请尝试以下基于XSLT的解决方案。

输入XML

<Configuration>
    <ConfiguredPaths>
        <ConfiguredPath>
            <LocalPath>C:\Temp</LocalPath>
            <EffectivePath>\\SERVERNAME\C$\Temp</EffectivePath>
        </ConfiguredPath>
        <ConfiguredPath>
            <LocalPath>C:\Files</LocalPath>
            <EffectivePath>\\SERVERNAME\C$\Files</EffectivePath>
        </ConfiguredPath>
        <ConfiguredPath>
            <LocalPath>C:\DOCS</LocalPath>
            <EffectivePath>\\SERVERNAME\C$\DOCS</EffectivePath>
        </ConfiguredPath>
    </ConfiguredPaths>
</Configuration>

XSLT

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"
               encoding="UTF-8" indent="yes"/>
   <xsl:strip-space elements="*"/>

   <!--Identity transform-->
   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

<xsl:template match="ConfiguredPath[EffectivePath = '\\SERVERNAME\C$\DOCS']"/>
</xsl:stylesheet>

Python

import lxml.etree as lx

# PARSE XML AND XSLT
doc = lx.parse("Input.xml")
style = lx.parse("Style.xslt")
outfile = "Output.xml"

# CONFIGURE AND RUN TRANSFORMER
transformer = lx.XSLT(style)
result = transformer(doc)

# OUTPUT TO FILE
with open(outfile, "wb") as f:
    f.write(result)

输出XML

<Configuration>
  <ConfiguredPaths>
    <ConfiguredPath>
      <LocalPath>C:\Temp</LocalPath>
      <EffectivePath>\\SERVERNAME\C$\Temp</EffectivePath>
    </ConfiguredPath>
    <ConfiguredPath>
      <LocalPath>C:\Files</LocalPath>
      <EffectivePath>\\SERVERNAME\C$\Files</EffectivePath>
    </ConfiguredPath>
  </ConfiguredPaths>
</Configuration>
© www.soinside.com 2019 - 2024. All rights reserved.