How can I access namespaced XML elements using BeautifulSoup?

Question:

I have an XML document which reads like this:

<xml>
<web:Web>
<web:Total>4000</web:Total>
<web:Offset>0</web:Offset>
</web:Web>
</xml>

my question is how do I access them using a library like BeautifulSoup in python?

xmlDom.web[“Web”].Total ? does not work?

Asked By: demos

||

Answers:

BeautifulSoup isn’t a DOM library per se (it doesn’t implement the DOM APIs). To make matters more complicated, you’re using namespaces in that xml fragment. To parse that specific piece of XML, you’d use BeautifulSoup as follows:

from BeautifulSoup import BeautifulSoup

xml = """<xml>
  <web:Web>
    <web:Total>4000</web:Total>
    <web:Offset>0</web:Offset>
  </web:Web>
</xml>"""

doc = BeautifulSoup( xml )
print doc.find( 'web:total' ).string
print doc.find( 'web:offset' ).string

If you weren’t using namespaces, the code could look like this:

from BeautifulSoup import BeautifulSoup

xml = """<xml>
  <Web>
    <Total>4000</Total>
    <Offset>0</Offset>
  </Web>
</xml>"""

doc = BeautifulSoup( xml )
print doc.xml.web.total.string
print doc.xml.web.offset.string

The key here is that BeautifulSoup doesn’t know (or care) anything about namespaces. Thus web:Web is treated like a web:web tag instead of as a Web tag belonging to th eweb namespace. While BeautifulSoup adds web:web to the xml element dictionary, python syntax doesn’t recognize web:web as a single identifier.

You can learn more about it by reading the documentation.

Answered By: Craig Trader

This is an old question but somebody might not know that at least BeautifulSoup 4 does handle namespaces well if you pass 'xml' as second argument to the constructor:

soup = BeautifulSoup("""<xml>
<web:Web>
<web:Total>4000</web:Total>
<web:Offset>0</web:Offset>
</web:Web>
</xml>""", 'xml')

print soup.prettify()
<?xml version="1.0" encoding="utf-8"?>
<xml>
 <Web>
  <Total>
   4000
  </Total>
  <Offset>
   0
  </Offset>
 </Web>
</xml>
Answered By: Suzana

You should explicitly define your namespace on root element, using syntax (see examples here), and then you access you attribute via prefix:tag from BeautifulSoup. Keep in mind,what you also should explicitly define, how BeautifulSoup should process you document, in that case:

xml = BeautifulSoup(xml_content, ‘xml’)

Answered By: Sergey Lyapustin

Environment

import bs4
bs4.__version__
---
4.10.0'

import sys
print(sys.version)
---
3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]

BS4/XML Parser on XML with namespace definition

from bs4 import BeautifulSoup

xbrl_with_namespace = """
<?xml version="1.0" encoding="UTF-8"?>
<xbrl
    ># extract the first element with tag name
xml_soup.find('web:Web')

# extract all elements with tag name
xml_soup.find_all('web:Web')

Searching within a namespace with CSS selectors

BS4 also allows you to search within namespaces using CSS selectors by using a prefix: your namespace, a pipe symbol | and finally your CSS selector. Template: ns_name|css_selector.

# select all elements in the namespace 'web'
xml_soup.select('web|*')

# selecting specific elements within the namespace 'web'
xml_soup.select('web|Web > Total')

More complex searches within a namespace

For anything more complex, you’ll want to write a custom boolean function:

def ns_and_regex_match(tag) -> bool:
  if tag.prefix != 'web':
    return False
  return bool(re.search('^Off.*$', tag.name))

xml_soup.find_all(ns_and_regex_match)
Answered By: mon