0

I have the XML below:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <LogonResponse xmlns="http://schemas.navitaire.com/ClientServices/Common/SessionManagerClient">
      <LogonResult>
        <LastName xmlns="http://schemas.navitaire.com/Messages/Session/Response">Mont</LastName>
        <FirstName xmlns="http://schemas.navitaire.com/Messages/Session/Response">Paul</FirstName>
        <PersonID xmlns="http://schemas.navitaire.com/Messages/Session/Response">123</PersonID>
        <CultureCode xmlns="http://schemas.navitaire.com/Messages/Session/Response">en-US</CultureCode>
        <CurrencyCode xmlns="http://schemas.navitaire.com/Messages/Session/Response">Dollar</CurrencyCode>
        <LastLogon xmlns="http://schemas.navitaire.com/Messages/Session/Response">0001-01-01T00:00:00</LastLogon>
        <SessionContext xmlns="http://schemas.navitaire.com/Common">
          <SessionControl>OneOnly</SessionControl>
          <SystemType>Default</SystemType>
          <SessionID>0</SessionID>
          <SequenceNumber>0</SequenceNumber>
          <MessageVersion>0</MessageVersion>
          <Signature>00000000-0000-0000-0000-000000000000</Signature>
          <ChannelType>Default</ChannelType>
          <InTransaction>false</InTransaction>
          <TransactionDepth>0</TransactionDepth>
          <TransactionCount>0</TransactionCount>
          <SecureToken>kFBOdZGqP6s=|/TftALE31236mSppQoFpArBizzz=</SecureToken>
        </SessionContext>
      </LogonResult>
    </LogonResponse>
  </s:Body>
</s:Envelope>

The idea is grab SecureToken, Signature and MessageVersion into variables, I'm trying use the code below:

SecureToken = tree.find('.//SecureToken').text

But no luck, I need use all the namespaces or something to achieve this?

Keep in mind that this XML is a return from a request and I can't edit it.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

If you want to avoid having to deal with the namespaces altogether you could do something like

tree.xpath("//*[local-name() = 'SecureToken']")[0].text

That would solve this particular problem, but has its own limitations and in most cases I would rather go for a slightly more verbose namespace-aware search.

kamjagin
  • 3,614
  • 1
  • 22
  • 24