How to execute XPath one-liners from shell?
You can also try my Xidel. It is not in a package in the repository, but you can just download it from the webpage (it has no dependencies).
It has simple syntax for this task:
xidel filename.xml -e '//element/@attribute'
And it is one of the rare of these tools that supports XPath 2.
One package that is very likely to be installed on a system already is python-lxml
. If so, this is possible without installing any extra package:
python -c "from lxml.etree import parse; from sys import stdin; print('\n'.join(parse(stdin).xpath('//element/@attribute')))"
You should try these tools :
xmlstarlet
(xmlstarlet page) : can edit, select, transform... Not installed by default, xpath1xmllint
(man xmllint): often installed by default withlibxml2-utils
, xpath1 (check my wrapper to have--xpath
switch on very old releases and newlines delimited output (v < 2.9.9)). Can be used as interactive shell with the--shell
switch.xpath
: installed via perl's moduleXML::Xpath
, xpath1xml_grep
: installed via perl's moduleXML::Twig
, xpath1 (limited xpath usage)xidel
(xidel): xpath3saxon-lint
(saxon-lint): my own project, wrapper over @Michael Kay's Saxon-HE Java library, xpath3: using SaxonHE 9.6 ,XPath 3.x (+retro compatibility)
Examples:
xmllint --xpath '//element/@attribute' file.xml
xmlstarlet sel -t -v "//element/@attribute" file.xml
xpath -q -e '//element/@attribute' file.xml
xidel -se '//element/@attribute' file.xml
saxon-lint --xpath '//element/@attribute' file.xml