'How to parse a SOAP file using xmllint or xmlstarlet?

I have the following SOAP file:

<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
  <SOAP-ENV:Header>
    <auth:Session xmlns:auth="http://www" SOAP-ENV:mustUnderstand="1">
      <auth:IPAddress>1111</auth:IPAddress>
    </auth:Session>
  </SOAP-ENV:Header>
  <SOAP-ENV:Body>
    <auth:SessionT xmlns:auth="http://www.">result</auth:SessionT>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

and I want to get result. I have tried different ways but every time I get the same error "Namespace prefix auth on SessionT is not defined".

Here it is one of the commands I've tried: xmlstarlet sel -t -v /auth:SessionT test.xml

Br, JD



Solution 1:[1]

With xmlstarlet, provide the namespace present in the source document using the -N option:

$ xmlstarlet sel -N auth="http://www." -t -v "//auth:SessionT" test.xml
result

Using xmllint, you could instead simply use local-name() to just check the tag name:

$ xmllint --xpath '//*[local-name() = "SessionT"]/text()' test.xml
result

Not tagged, but yet another alternative would be using xq (a jq wrapper provided with yq) like this:

$ xq -r '.. | ."auth:SessionT"? // empty | ."#text"' test.xml
result

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1