Search code examples
xmlparsingxmllint

Extract data from xml without xpath


Here is my issue, I'm working on UNIX, I have a xml file contained into a string foo:

echo $foo
<a> <b> <c> bar </c> </b> </a>

I want to get the value bar. I can use xmllint but I don't have --xpath option. Not that foo is an UNIX variable and not a file.

The unique solution I found is to run:

echo $foo | xmllint --shell <(cat) <<<'xpath a/b/c/text()'

But it produces a very verbose output:

/ > Object is a Node Set :
Set contains 1 nodes:
1  TEXT
    content= bar
/ >

I just want to get “bar”. Thanks for your help !


Solution

  • You could try to replace xpath with cat as xmlshell-command to invoke. This should output something like:

    / >  -------
     bar
    

    The first line is from the prompt of the xmlshell. To get only the last line, use your favorite tool to extract just line 2, like:

    foo="<a> <b> <c>_ bar _</c> </b> </a>"
    
    echo "cat //c/text()" | xmllint --shell <(echo $foo) | awk 'NR==2'