How do I parse namespaces from an XML file using XMLLINT and BASH

This discussion is enlightening.

At the very least, even if not ideal, you should be able to do:

xmllint --xpath "//*[local-name()='product_version']/*[local-name()='name']/text()" file.xml

Or use xmlstarlet instead:

xmlstarlet sel -t -v //swid:product_version/swid:name file.xml

Try using a here-doc. Example:

#!/bin/bash
xmllint --shell file.xml <<EOF
setns swid=http://standards.iso.org/iso/19770/-2/2008/schema.xsd
xpath //swid:product_version/swid:name/text()
EOF

Works with later versions of xmllint that support the --xpath parameter.


With an older version of xmllint (which doesn't support --xpath) you can set a namespace and query more intuitively thus (but you have to grep out some additional garbage):

#!/bin/bash
echo 'setns swid=http://standards.iso.org/iso/19770/-2/2008/schema.xsd
      cat //swid:product_version/swid:name/text()' | \
xmllint --shell file.xml | egrep -v '^(/ >| -----)'

Tags:

Xml

Bash

Osx