Reading XML namespaces
Adam Tauno Williams
awilliam at whitemice.org
Sun May 16 00:11:57 EDT 2010
On Sun, 2010-05-16 at 02:37 +0200, Martin v. Loewis wrote:
> > ??? The namespaces are embedded in the document. Personally I find it
> > odd I have to tell xpath about the namespace of the document it is a
> > $*&@(*& method of.
> How so? Why do you say it's a "method", and why do you say "of"?
> Usually, xpath expressions are *not* part of the document they operate
> on, but part of the code that performs the operation.
from lxml import etree
doc = etree.parse(data)
doc.xpath(....)
> Consequentially,
> the namespace prefixes in the xpath expression do *not* occur in the
> document (other than by chance), but are defined by whoever writes the
> xpath expression. That is typically somebody different from the one
> writing the document
Maybe true technically, but false in practice. If I receive XML data
from source XYZ or service XYZ the use of namespaces and their prefixes
is extremely consistent [in practice] and very customary (for example:
I've never seen the DSML namespace abbreviated as anything other than
"dsml" and I rarely see WebDAV propfind XML use a namespace prefix other
than "D"). The odds that a customer or vendors ERP will generate
different namespaces and abbreviations between requests is ludicrously
remote [I don't recall ever seeing it happen].
And if the xpath fails to produce normal [or any] output the workflow
with either do nothing or abend which will draw the attention of an
administrator.
> - if you would always write them together, you
> wouldn't need xpath in the first place, but could produce the selection
> result right away.
More information about the Python-list
mailing list