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://www.w3.org/2001/XMLSchema-instance" xmlns="https://analysiscenter.veracode.com/schema/4.0/buildinfo" xsi:schemaLocation="https://analysiscenter.veracode.com/schema/4.0/buildinfo https://analysiscenter.veracode.com/resource/4.0/buildinfo.xsd" buildinfo_version="1.4" account_id="1234" app_id="010101" sandbox_id="020202" build_id="987654321"><build version="4 Sep 2020 Static (2)" 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:44:09-04: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.
You're are getting entangled with namespaces, so try using an xpath expression like:
//*[local-name()='buildinfo']/@build_id
and see if it works.