即使使用strip_cdata = False,CDATA也会被剥离为lxml

问题描述:

我有一个需要读取XML文件并用特定值替换字符串的需求。 XML包含CDATA元素,我需要保留它。 我曾尝试使用解析器并将strip_data设置为false。这不起作用,需要帮助才能找到实现它的方法。即使使用strip_cdata = False,CDATA也会被剥离为lxml

import lxml.etree as ET 

parser1 = ET.XMLParser(strip_cdata=False) 

with open('testxml.xml', encoding="utf8") as f: 
tree = ET.parse(f, parser=parser1) 

root = tree.getroot() 
for elem in root.getiterator(): 
    try: 
     elem.text = elem.text.replace('Bundled Manager 2.2(8b)', '123456') 
    except AttributeError: 
     pass 

tree.write('output_new8.xml', xml_declaration=True, method='xml', encoding="utf8") 

下面是示例XML:


 <?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- Copyright (c) 2015 Moto Company, LLC. All rights reserved. Moto Confidential/Proprietary Information --> 
<Benchmark> 
     <status date="2013-03-11">draft</status> 
    <title>Logitech TMM block(TM) System 300 Release Certification Matrix</title> 
    <description>Random discription</description> 
    <version time="2013-03-05T15:20:20.995-04:00" update="">3.0.0-2017.03.00</version> 
     <model system="urn:xccdf:scoring:default"/> 
    <Profile id="xccdf_com.Moto_profile_release_4.0.21"> 
     <status date="2016-03-30">draft</status> 
     <title>RCM 4.0.21</title> 
     <description><![CDATA[<p>Moto Vblock System 300 Release 4.0.21</p> 
<ul><li> TMM VNX OE for File was updated to 7.1.79.8.</li> 
</ul>]]> 
</description> 
     <set-value idref="xccdf_com.Moto_value_vision_content_version">3.0.0-2015.07.00</set-value> 
     <set-value idref="xccdf_com.Moto_value_vision_version">3.0.0</set-value> 
     <set-value idref="xccdf_com.Moto_value_vplex_version">5.3.0.03.00.04</set-value> 
     <set-value idref="xccdf_com.Moto_value_powerpath_version">Bundled Manager 2.2(8b)</set-value>  
     <select idref="xccdf_com.Moto_rule_vnx_version" selected="true"/> 
     <select idref="xccdf_com.Moto_rule_vplex_version" selected="true"/> 
    </Profile> 
</Benchmark> 

代码的输出如下所示:

<?xml version='1.0' encoding='UTF8'?> 
<!-- Copyright (c) 2015 Moto Company, LLC. All rights reserved. Moto Confidential/Proprietary Information --><Benchmark> 
    <status date="2013-03-11">draft</status> 
    <title>Logitech TMM block(TM) System 300 Release Certification Matrix</title> 
    <description>Random discription</description> 
    <version time="2013-03-05T15:20:20.995-04:00" update="">3.0.0-2017.03.00</version> 
     <model system="urn:xccdf:scoring:default"/> 
    <Profile id="xccdf_com.Moto_profile_release_4.0.21"> 
     <status date="2016-03-30">draft</status> 
     <title>RCM 4.0.21</title> 
     <description>&lt;p&gt;Moto Vblock System 300 Release 4.0.21&lt;/p&gt; 
&lt;ul&gt;&lt;li&gt; TMM VNX OE for File was updated to 7.1.79.8.&lt;/li&gt; 
&lt;/ul&gt; 
</description> 
     <set-value idref="xccdf_com.Moto_value_vision_content_version">3.0.0-2015.07.00</set-value> 
     <set-value idref="xccdf_com.Moto_value_vision_version">3.0.0</set-value> 
     <set-value idref="xccdf_com.Moto_value_vplex_version">5.3.0.03.00.04</set-value> 
     <set-value idref="xccdf_com.Moto_value_powerpath_version">123456</set-value>   
     <select idref="xccdf_com.Moto_rule_vnx_version" selected="true"/> 
     <select idref="xccdf_com.Moto_rule_vplex_version" selected="true"/> 
    </Profile> 
</Benchmark 

>

正如你可以看到,CDATA部分被剥离。 如果有人能帮助我,这将是一件好事。

这是因为你在做

elem.text = elem.text.replace('Bundled Manager 2.2(8b)', '123456') 

与普通文本节点替换CDATA。

documentation状态

注意.text属性不怎么不给任何迹象表明该文本内容由CDATA部分包裹着。如果你想确保你的数据被CDATA块包装,你可以使用CDATA()文本包装器。

因此,如果你想保持CDATA部分,你应该只分配给elem.text如果要修改它,并指示LXML使用CDATA节:

if 'Bundled Manager 2.2(8b)' in elem.text: 
    elem.text = ET.CDATA(elem.text.replace('Bundled Manager 2.2(8b)', '123456')) 

由于如何ElementTree库工程(整个文本和cdata内容连接并在.text属性中以str的形式公开),但实际上不可能知道CDATA是否最初被使用。 (见Figuring out where CDATA is in lxml element?the source code

+0

谢谢基思,你的解释是有道理的。我会尝试解决方案并让你知道。 – Anky

+0

@Keith,我很好奇,如果你能分辨CDATA在空时是否被使用过? ''。我的预感是可以的,因为lxml返回一个空字符串,而一个空节点通常返回'None'。 –

+0

@MarcelWilson我的测试确认你的直觉:)即使使用'remove_blank_text = True',CDATA仍然存在。 –