Search code examples
xmlshellunixxmllint

Observed error while trying to fetch details from xml that stored in variable


I have below xml that I stored in shell variable as like below:

   file1=$(sudo curl --silent  -XGET 'https://<username>:<token>@dev-pilot.us-central1.gcp.dev.amazon.com/te-ci-9414/job/teserv-ci-9414_FSS_functional_test_develop-test_2/Report_20for_20FS/testng-results.xml')

   echo $file1  // It prints expected xml file.

//Trying to fetch @Total from the above variable as like below:

   total=$(xmllint --xpath "/testng-results/@total" $file1 |sed 's/"//g' | sed 's/^/"/' |sed 's/$/"/')

but It returned below error message:

   20:27:14 /tmp/jenkins4105200679331055571.sh: line 22: /usr/bin/xmllint: Argument list too long

The xml file is so large so I just mentioned a piece that I'm looking into that file:

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="0" failed="7" total="2952" passed="2945">

Can anyone help to fetch the @total attribute value.


Solution

  • When you use $file1 in the xmllint command line, xmllint thinks that your xml is a bunch of command line arguments.

    What you can do instead is use - to tell xmllint that the input is from stdin and a "Here String" (<<<) to supply your XML...

    (NOTE: Tested using cygwin on Windows.)

    #!/usr/bin/env bash.exe
    
    file1='<testng-results skipped="0" failed="7" total="2952" passed="2945"/>'
    
    total=$(xmllint --xpath "/testng-results/@total" - <<<"$file1")
    
    echo $total
    

    output:

    total="2952"
    

    I'm not sure what your pipes to sed are accomplishing, but if you just want the value (2952) you can use string() in your xpath...

    --xpath "string(/testng-results/@total)"