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())
答案 0 :(得分:1)
“如果
refpt
和title
包含title
”,我想删除designum
的{{1}}个孩子。 p>
以下XPath应根据上述标准返回要删除的refpt
元素:
refpt
如果您需要确保只有//leg:heading[desig/designum/refpt]/title/refpt
下的refpt
与title
匹配id
下的designum
,请尝试此XPath:
//leg:heading/title/refpt[@id = ../../desig/designum/refpt/@id]