osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lxml namespace as an attribute


-----Original Message-----
From: Python-list <python-list-
bounces+jcasale=activenetwerx.com at python.org> On Behalf Of Skip
Montanaro
Sent: Wednesday, August 15, 2018 3:26 PM
To: Python <python-list at python.org>
Subject: lxml namespace as an attribute
 
> Much of XML makes no sense to me. Namespaces are one thing. If I'm
> parsing a document where namespaces are defined at the top level, then
> adding namespaces=root.nsmap works when calling the xpath method. I
> more-or-less get that.
> 
> What I don't understand is how I'm supposed to search for a tag when
> the namespace appears to be defined as an attribute of the tag itself.
> I have some SOAP XML I'm trying to parse. It looks roughly like this:
> 
> <s: Envelope xmlns:a="..." xmlns:s="...">
>   <s:Header>
>      ...
>   </s:Header>
>   <s:Body>
>     <Tag xmlns="http://some/new/path";>
>     ...
>     </Tag>
>   </s:Body>
> 
> If the document is "doc", I can find the body like so:
> 
> body = doc.xpath(".//Body" namespaces=doc.nsmap)
> 
> I don't understand how to find Tag, however. When I iterate over the
> body's children, printing them out, I see that Tag's name is actually:
> 
>     {http://some/new/path}Tag
> 
> yet that namespace is unknown to me until I find Tag. It seems I'm
> stuck in a chicken-and-egg situation. Without knowing that
> http://some/new/path namespace, is there a way to cleanly find all
> instances of Tag?

See https://lxml.de/tutorial.html#namespaces and
https://lxml.de/2.1/FAQ.html#how-can-i-specify-a-default-namespace-for-xpath-expressions
for direction. I don't have Python at my current location but I trust that will point you straight.

jlc