Search code examples
xmlxpathxmllintveracode

How can I use Xmlint to map a certain value from a XML file?


I'm a beginner.

I need to script a automation command line in linux(but havent achieved success yet), that can map the value sandbox_id= and output it, using the xml as follows:

    <?xml version="1.0" encoding="UTF-8"?>
<sandboxinfo xmlns="https://analysiscenter.veracode.com/schema/4.0/sandboxinfo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://analysiscenter.veracode.com/schema/4.0/sandboxinfo https://analysiscenter.veracode.com/resource/4.0/sandboxinfo.xsd" sandboxinfo_version="1" account_id="1" app_id="1">
   <sandbox sandbox_id="1" sandbox_name="SANDBOX" sandbox_status="1" owner="1" modified_date="1" created_date="1" expires="1" auto_recreate="1">
      <customfield name="Custom 1" value="" />
      <customfield name="Custom 2" value="" />
      <customfield name="Custom 3" value="" />
      <customfield name="Custom 4" value="" />
      <customfield name="Custom 5" value="" />
   </sandbox>
</sandboxinfo>

I've tried using xmllint as follows, but didnt get any result:

xmllint --xpath 'string(/sandbox/@sandbox_id)' output.xml

Also tried:

xmllint --xpath 'string(/*[local-name()="sandbox"]/@sandbox_id)' output.xml

Any help will be very apreciated


Solution

  • Both your xpath attempts expect sandbox to be the root element, but it's not.

    Try the xpath 'string(//*[local-name()="sandbox"]/@sandbox_id)'

    or 'string(/*/*[local-name()="sandbox"]/@sandbox_id)' instead.