Search code examples
shellxmlstarlet

How to use variables as input for xmlstarlet val?


How can I use variables as input for xmlstarlet validate command? Here is the complete example:

read -r -d '' XML <<"EOF"
<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</catalog>
EOF

# emits a usage message, exits with status 2
xmlstarlet val -v . <<< "$XML"

# also emits a usage message and exits with status 2
xmlstarlet val -v - <<< "$XML"

Solution

  • Remove the invalid -v argument, and your command parses correctly:

    $ xmlstarlet val - <<<"$XML"
    - - valid