Python 美化具有命名空间的组搜索属性

Python 美化具有命名空间的组搜索属性,python,xml,Python,Xml,我想在XML文件中搜索所有标记中的xlink:href属性。我无法用Beautifulsou find_all和正则表达式完成它。下面是我的XML文件 <body:document-content> <style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard"> <style:text-properties officeooo:paragraph-

我想在XML文件中搜索所有标记中的xlink:href属性。我无法用Beautifulsou find_all和正则表达式完成它。下面是我的XML文件

<body:document-content>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
  <style:text-properties officeooo:paragraph-rsid="00118689"/>
</style:style>

<body:text>
  <text:sequence-decls>
    <text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
    <text:sequence-decl text:display-outline-level="0" text:name="Table"/>
  </text:sequence-decls>
  <text:p text:style-name="P1">This is example document</text:p>
  <text:p text:style-name="P1"/>
  <text:p text:style-name="P1">hello world</text:p>
  <text:p text:style-name="P1"/>
  <text:p text:style-name="P1">
    <text:a xlink:type="simple" xlink:href="https://example.com">https://example.com</text:a>
  </text:p>
  <text:p text:style-name="P1"/>
  <text:p text:style-name="P1"/>
</body:text>
</body:document-content>
<text:a xlink:type="simple" xlink:href="https://example.com">https://example.com</text:a>

这是一个示例文档
你好,世界
https://example.com
我想从XML文件中删除以下标记行

<body:document-content>
<style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard">
  <style:text-properties officeooo:paragraph-rsid="00118689"/>
</style:style>

<body:text>
  <text:sequence-decls>
    <text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>
    <text:sequence-decl text:display-outline-level="0" text:name="Table"/>
  </text:sequence-decls>
  <text:p text:style-name="P1">This is example document</text:p>
  <text:p text:style-name="P1"/>
  <text:p text:style-name="P1">hello world</text:p>
  <text:p text:style-name="P1"/>
  <text:p text:style-name="P1">
    <text:a xlink:type="simple" xlink:href="https://example.com">https://example.com</text:a>
  </text:p>
  <text:p text:style-name="P1"/>
  <text:p text:style-name="P1"/>
</body:text>
</body:document-content>
<text:a xlink:type="simple" xlink:href="https://example.com">https://example.com</text:a>
https://example.com

请建议如何使用beautifulsoup完成此操作。我也试过Elementtree。但是,它带来了许多名称空间问题。

这是一个过于简单的答案,但不清楚您正在尝试做什么,或者您在XML中可能会遇到什么变化。如果您不需要使用XPath进行更复杂的操作,那么示例中的XML建议您只需搜索
text:a
元素(唯一具有
xlink:href
属性的元素)——如果它确实是您要删除的
text:a
“行”(元素节点)

from bs4 import BeautifulSoup

with open('test.xml') as x: # text.xml is the xml from your post
    doc = BeautifulSoup(x)
    #print(doc.find_all( 'text:a' ))  # see that it gets all text:a elements
    [s.extract() for s in doc('text:a')] # extracts text:a elements
    print(doc)