56

I have an xml doc that I am trying to parse using Etree.lxml

<Envelope xmlns="http://www.example.com/zzz/yyy">
  <Header>
    <Version>1</Version>
  </Header>
  <Body>
    some stuff
  <Body>
<Envelope>

My code is:

path = "path to xml file"
from lxml import etree as ET
parser = ET.XMLParser(ns_clean=True)
dom = ET.parse(path, parser)
dom.getroot()

When I try to get dom.getroot() I get:

<Element {http://www.example.com/zzz/yyy}Envelope at 28adacac>

However I only want:

<Element Envelope at 28adacac>

When i do

dom.getroot().find("Body")

I get nothing returned. However, when I

dom.getroot().find("{http://www.example.com/zzz/yyy}Body") 

I get a result.

I thought passing ns_clean=True to the parser would prevent this.

Any ideas?

Kenster
  • 23,465
  • 21
  • 80
  • 106
Mark
  • 2,522
  • 5
  • 36
  • 42
  • see also: [Python ElementTree module: How to ignore the namespace of XML files](https://stackoverflow.com/questions/13412496/python-elementtree-module-how-to-ignore-the-namespace-of-xml-files-to-locate-ma/76601149#76601149) – milahu Jul 03 '23 at 06:49

5 Answers5

59
import io
import lxml.etree as ET

content='''\
<Envelope xmlns="http://www.example.com/zzz/yyy">
  <Header>
    <Version>1</Version>
  </Header>
  <Body>
    some stuff
  </Body>
</Envelope>
'''    
dom = ET.parse(io.BytesIO(content))

You can find namespace-aware nodes using the xpath method:

body=dom.xpath('//ns:Body',namespaces={'ns':'http://www.example.com/zzz/yyy'})
print(body)
# [<Element {http://www.example.com/zzz/yyy}Body at 90b2d4c>]

If you really want to remove namespaces, you could use an XSL transformation:

# http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
xslt='''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>

<xsl:template match="/|comment()|processing-instruction()">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

<xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:attribute name="{local-name()}">
      <xsl:value-of select="."/>
    </xsl:attribute>
</xsl:template>
</xsl:stylesheet>
'''

xslt_doc=ET.parse(io.BytesIO(xslt))
transform=ET.XSLT(xslt_doc)
dom=transform(dom)

Here we see the namespace has been removed:

print(ET.tostring(dom))
# <Envelope>
#   <Header>
#     <Version>1</Version>
#   </Header>
#   <Body>
#     some stuff
#   </Body>
# </Envelope>

So you can now find the Body node this way:

print(dom.find("Body"))
# <Element Body at 8506cd4>
Kenster
  • 23,465
  • 21
  • 80
  • 106
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
34

Try using Xpath:

dom.xpath("//*[local-name() = 'Body']")

Taken (and simplified) from this page, under "The xpath() method" section

Denis Malinovsky
  • 5,840
  • 1
  • 22
  • 17
dusan
  • 9,104
  • 3
  • 35
  • 55
5

The last solution from https://bitbucket.org/olauzanne/pyquery/issue/17 can help you to avoid namespaces with little effort

apply xml.replace(' xmlns:', ' xmlnamespace:') to your xml before using pyquery so lxml will ignore namespaces

In your case, try xml.replace(' xmlns="', ' xmlnamespace="'). However, you might need something more complex if the string is expected in the bodies as well.

Andrei
  • 10,918
  • 12
  • 76
  • 110
  • 3
    This is amazing. You have changed my life, thank you. (ps, whoever designed XML namespaces, wtf?) – Walt W Jun 25 '12 at 22:57
  • 18
    String munging is always the path to madness. In the general case, this answer is dead wrong. Suppose you're formatting an rss feed of this exact question -- the result would tell people to `xml.replace(' xmlnamespace="', ' xmlnamespace="')`... – bukzor Sep 16 '13 at 21:49
  • A simple but very crucial improvement would be `xml.replace(' xmlns="', ' xmlnamespace="', 1)`, in which way only the first appearance would be replaced, thus the `xmlns` in other places like the main content would not be affected. – ospider Nov 11 '22 at 16:10
1

Another not-too-bad option is to use the QName helper and wrap it in a function with a default namespace:

from lxml import etree

DEFAULT_NS = 'http://www.example.com/zzz/yyy'

def tag(name, ns=DEFAULT_NS):
    return etree.QName(ns, name)

dom = etree.parse(path)
body = dom.getroot().find(tag('Body'))
Tom
  • 7,269
  • 1
  • 42
  • 69
-4

You're showing the result of the repr() call. When you programmatically move through the tree, you can simply choose to ignore the namespace.

robert
  • 33,242
  • 8
  • 53
  • 74