xpath prob, was Re: Why is there no functional xml?

dieter dieter at handshake.de
Thu Jan 25 02:12:53 EST 2018


> Rustom Mody wrote:
>
>> With
>> # Read above xml
>>>>> with open('soap_response.xml') as f: inp = etree.parse(f)
>> # namespace dict
>>>>> nsd = {'soap': "http://schemas.xmlsoap.org/soap/envelope/", 'locns':
>>>>> "http://example.com/"}
>> 
>> The following behavior is observed $(G!7(B actual responses elided in the
>> interest of brevity
>> 
>>>>> inp.xpath('//soap:Body', namespaces = nsd)
>> finds/reaches the node
>> 
>>>>> inp.xpath('//locns:blobRetrieveResponse', namespaces = nsd)
>> finds
>> 
>>>>> inp.xpath('//locns:dtCreationDate', namespaces = nsd)
>> does not find
>> 
>>>>> inp.xpath('//dtCreationDate', namespaces = nsd)
>> finds
>> 
>>>>> inp.xpath('//dtCreationDate')
>> also finds
>> 
>> 
>> Doesnt this contradict the fact that dtCreationDate is under the locns
>> namespace??

Apparently, "dtCreationDate" is not associated with the
namespace corresponding to the "locns" namespace.

Note, that the namespace association is not by default inherited
by child elements -- as least not with stand alone XML. Thus,
if you have e.g.

   <nspref:parent><child/></nspref:parent>

then the element "child" does not belong to the namespace indicated
by "nspref" but to the "default namespace".


An XML schema can change this default. However, to get such an
XML schema effective, you must specify this wish when you are
parsing your XML document. Otherwise, your XML document is parsed
as a "stand alone" XML and the rules of the XML-namespace standard
apply -- which means, that the namespace association is not inherited
to child elements.




More information about the Python-list mailing list