Search code examples
xmlbashxpathxmllint

xmllint not returning what I expect -- n00b warning


The Problem> I have some xml returned from an API call stored in get_build_info.xml. I am trying to grab an attribute from that xml, build_id. Here is the xml:

 <?xml version="1.0" encoding="UTF-8"?>

<buildinfo xmlns:xsi="http&#x3a;&#x2f;&#x2f;www.w3.org&#x2f;2001&#x2f;XMLSchema-instance" xmlns="https&#x3a;&#x2f;&#x2f;analysiscenter.veracode.com&#x2f;schema&#x2f;4.0&#x2f;buildinfo" xsi:schemaLocation="https&#x3a;&#x2f;&#x2f;analysiscenter.veracode.com&#x2f;schema&#x2f;4.0&#x2f;buildinfo https&#x3a;&#x2f;&#x2f;analysiscenter.veracode.com&#x2f;resource&#x2f;4.0&#x2f;buildinfo.xsd" buildinfo_version="1.4" account_id="1234" app_id="010101" sandbox_id="020202" build_id="987654321"><build version="4 Sep 2020 Static &#x28;2&#x29;" build_id="987654321" submitter="Someone Else" platform="Not Specified" lifecycle_stage="Not Specified" results_ready="true" policy_name="Some Development App Policy" policy_version="7" policy_compliance_status="Conditional Pass" rules_status="Not Assessed" grace_period_expired="false" scan_overdue="false" legacy_scan_engine="false">
      <analysis_unit analysis_type="Static" published_date="2020-09-04T11&#x3a;44&#x3a;09-04&#x3a;00" published_date_sec="1599234249" status="Results Ready" engine_version="20200821190810"/>
   </build>
</buildinfo>

What I've tried> The following, and many other variations thereof:

xmllint --xpath 'string(//xml/buildinfo/@build_id)' get_build_info.xml

xmllint --xpath 'string(//buildinfo/@build_id)' get_build_info.xml

xmllint --xpath 'string(/xml/buildinfo/@build_id)' get_build_info.xml

xmllint --xpath '(//xml/buildinfo/build_id/text())' get_build_info.xml

xmllint --xpath '(/xml/buildinfo/build_id/text())' get_build_info.xml

The last two at least yield some kind of output, albeit be that "XPath set is empty". The first few where I'm using that 'string( +...+ @build_id in the --xpath, I just get nothing returned. These all appear to exit 0 from bash as well so there's no syntax issue from what I can tell. Like I said, I'm a n00b. I looked at other popular tickets on stackoverflow and that's what got me to this point. I might very well not be taking something obvious into account here, so assume that I know nothing about bash (which is close to true). I'm happy to be given a direction to go and find the answer myself.


Solution

  • You're are getting entangled with namespaces, so try using an xpath expression like:

    //*[local-name()='buildinfo']/@build_id
    

    and see if it works.