Skip to main content
added 34 characters in body
Source Link
Charles Duffy
  • 299.1k
  • 43
  • 439
  • 495

Tested with both xml.etree.ElementTree and lxml.etree:

datansmap = articlepart.find("npdoc:data",
           namespaces={'npdoc': 'http://www.example.com/npdoc/2.1'}
data = articlepart.find("npdoc:data", namespaces=nsmap)

...will find your data element. No ugly, unreliable string munging required.


It's a typical pattern to define your namespace map once at the top of your script and reuse it on all your queries into the data, by the way; that way, you don't need (Re: "unreliable" -- consider what this would do to type the whole thing out over and overCDATA sections containing literal arrow brackets).

Tested with both xml.etree.ElementTree and lxml.etree:

data = articlepart.find("npdoc:data",
           namespaces={'npdoc': 'http://www.example.com/npdoc/2.1'})

...will find your data element. No ugly, unreliable string munging required.


It's a typical pattern to define your namespace map once at the top of your script and reuse it on all your queries into the data, by the way; that way, you don't need to type the whole thing out over and over.

nsmap = {'npdoc': 'http://www.example.com/npdoc/2.1'}
data = articlepart.find("npdoc:data", namespaces=nsmap)

...will find your data element. No ugly, unreliable string munging required. (Re: "unreliable" -- consider what this would do to CDATA sections containing literal arrow brackets).

Source Link
Charles Duffy
  • 299.1k
  • 43
  • 439
  • 495

Tested with both xml.etree.ElementTree and lxml.etree:

data = articlepart.find("npdoc:data",
           namespaces={'npdoc': 'http://www.example.com/npdoc/2.1'})

...will find your data element. No ugly, unreliable string munging required.


It's a typical pattern to define your namespace map once at the top of your script and reuse it on all your queries into the data, by the way; that way, you don't need to type the whole thing out over and over.