Search code examples
xmlstarlet

xmlstarlet to get and parse the data between tags


I wanted the get data from xml tags to script file, but data is in hexadecimal characters line(& >) which needs are to be converted to form (&& >).

Example:

<project>
<code><shell> if a &amp;&amp; b </shell></code>
</project>

I am able to extract tags using command

xmlstarlet edit --update 'project/code/shell' --value "$DATA" shell.xml > shell.sh

cat shell.sh

Actual:

if a &amp;&amp; b

Expected:

if a && b

How to acheive expected result?


Solution

  • To unescape special XML characters:

    echo ' if a &amp;&amp; b ' | xmlstarlet unescape
    

    Output:

     if a && b 
    

    cat file.xml | xmlstarlet unescape
    

    Output:

    <project>
    <code><shell> if a && b </shell></code>
    </project>