Reading XML namespaces

Stefan Behnel stefan_ml at behnel.de
Sat May 15 17:25:33 EDT 2010


Adam Tauno Williams, 15.05.2010 23:04:
> On Sat, 2010-05-15 at 22:58 +0200, Stefan Behnel wrote:
>> Adam Tauno Williams, 15.05.2010 22:40:
>>> On Sat, 2010-05-15 at 22:29 +0200, Stefan Behnel wrote:
>>>> Adam Tauno Williams, 15.05.2010 20:37:
>>>>> Say I have an XML document that begins with:
>>>>> <?xml version="1.0" encoding="utf-8"?>
>>>>> <dsml:dsml xmlns:dsml="http://www.dsml.org/DSML">
>>>>> How can one access the namespaces define in this node?  I've done a fair
>>>>> amount of XML in Python, but haven't been able to uncover the call to
>>>>> enumerate the namespaces.
>>>>> Primarily I am using etree from lxml.
>>>> What do you need the namespaces for?
>>> One needs to know the defined namespace in order to perform xpath
>>> operations.
>> Well, yes, but unless you already know the namespace (URI), you can't know
>> what the tag you find signifies in the first place.
>> Unless, obviously, you are confusing namespaces with namespace prefixes.
>> But you don't need to know the prefixes for XPath.
>> Does this help?
>> http://codespeak.net/lxml/xpathxslt.html#namespaces-and-prefixes
>
> I know that.  I'm getting an XML document and an xpath and need to
> execute it.  But i have to tell xpath via namespaces= the prefixes&
> namespaces; so I need to get that data out of the document.

Ah, you didn't provide that information in your initial post. So you 
control neither the document nor the XPath expression, right? Can't you get 
the namespace-prefix mapping from your user? After all, he/she is the only 
one who knows the meaning of the XPath expression. I'd just reject any 
expression with an undefined prefix.

BTW, I'm still not sure I understand your problem. Could you provide some 
more details?

Stefan




More information about the Python-list mailing list