0

I have an xml as follows:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Header>
<h:Challenge xmlns:h="http://soap-authentication.org/digest/2001/10/" s:mustUnderstand="1">
<Status>Unauthenticated</Status>
<Nonce>359DCCAB1A8AC6E2</Nonce>
<Realm>F!Box SOAP-Auth</Realm>
</h:Challenge>
</s:Header>
<s:Body>
<s:Fault>
<faultcode>s:Client</faultcode>
<faultstring>UPnPError</faultstring>
<detail>
<UPnPError xmlns="urn:dslforum-org:control-1-0">
<errorCode>503</errorCode>
<errorDescription>auth. failed</errorDescription>
</UPnPError>
</detail>
</s:Fault>
</s:Body>
</s:Envelope>

I have no problem to get Nonce and Realm value with et.find('.//Nonce') e.g.

But I cannot get "errorCode" with et.find('.//errorCode').

What do I miss here?

Michael

Michael Schmidt
  • 1,199
  • 4
  • 14
  • 18
  • Please add a language tag to tell us which programming language you're using. – Mark Ransom Jun 29 '17 at 14:52
  • Sorry, it is Python with "import xml.etree.ElementTree as et" – Michael Schmidt Jun 29 '17 at 15:38
  • You need to pay attention to the namespace that `` is in. The duplicate question shows how to use namespaces in lxml searches. – Tomalak Jun 29 '17 at 15:41
  • In fact, this might be a better duplicate: https://stackoverflow.com/questions/8053568/how-do-i-use-empty-namespaces-in-an-lxml-xpath-query – Tomalak Jun 29 '17 at 15:41
  • @Tomalak, that is not a better dupe IMHO. That question involves lxml and not ElementTree (similar but not identical APIs). – mzjn Jun 29 '17 at 15:51
  • I think the API for xpath searches is the same in both. Here's another one that explicitly is tagged [elementtree] https://stackoverflow.com/questions/14853243/parsing-xml-with-namespace-in-python-via-elementtree - and it works exactly the same. – Tomalak Jun 29 '17 at 15:55
  • @Tomalak: ElementTree has no `xpath()` method. The APIs are not the same. – mzjn Jun 29 '17 at 16:05
  • @mzjn *"Making a dict with the namespaces and passing it to the function that evaluates the XPath expression"* is the same thing, fundamentally. – Tomalak Jun 29 '17 at 16:10
  • 1
    @mzjn But you are right, it's not exactly the same. I tend to mix up lxml and ElementTree and disregard the differences as minor details. The second link I posted to the comments seems to be the best duplicate match. – Tomalak Jun 29 '17 at 16:15
  • The links are all using lxml, but I am bound to ElementTree. So, how would it look like with just ElementTree. Perhaps https://stackoverflow.com/questions/14853243/parsing-xml-with-namespace-in-python-via-elementtree is closest? – Michael Schmidt Jun 29 '17 at 17:30
  • Sorry, Ii am new to namespaces. Could someone be so kind to elaborate, how it should be done in this case? When I add the namespaces as >>> my_namespaces {'': 'urn:dslforum-org:control-1-0', u'h': 'http://soap-authentication.org/digest/2001/10/', u's': 'http://schemas.xmlsoap.org/soap/envelope/'} I get still get nothing with et.find(".//errorCode", my_namespaces) – Michael Schmidt Jun 29 '17 at 19:38
  • Straight forward solution here was to use the namespace in the find command: root.find(".//{urn:dslforum-org:control-1-0}errorCode"). Not clear to me though, why querying "Nonce" was working without namespace. – Michael Schmidt Jun 30 '17 at 09:38
  • @MichaelSchmidt; It was working because the Nonce element isn't in any namespace. – mzjn Jun 30 '17 at 14:17

0 Answers0