XML:
<leg:heading nline="true">
<desig inline="true" searchtype="SECT-NUM">
<designum>
<refpt type="ext" id="USM.A000001y1966e.S1"/>1.
</designum>
</desig>
<title inline="true" searchtype="SECT-TITLE">
<refpt type="ext" id="USM.A000001y1966e.S1"/>Short title, commencement and application
</title>
</leg:heading>
如果title和designum都包含refpt,我想删除title的refpt子代。
我的代码:
for elem in doc.xpath('//leg:heading',namespaces={'leg':'http://www.lexis-nexis.com/glp/leg'}):
for element in elem.getiterator():
if(element.tag=='refpt'):
print(element.tag.getparent())
最佳答案
“如果refpt
和title
都包含title
,我想删除designum
的refpt
子级”
下面的XPath应该返回refpt
元素,该元素应根据上述标准删除:
//leg:heading[desig/designum/refpt]/title/refpt
如果需要确保仅在
refpt
与title
下的匹配的id
匹配时删除designum
下的,请尝试以下XPath://leg:heading/title/refpt[@id = ../../desig/designum/refpt/@id]