How to execute XPath one-liners from shell?

You should try these tools :

  • xmlstarlet (xmlstarlet page) : can edit, select, transform… Not installed by default, xpath1
  • xmllint (man xmllint): often installed by default with libxml2-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 module XML::Xpath, xpath1
  • xml_grep : installed via perl’s module XML::Twig, xpath1 (limited xpath usage)
  • xidel (xidel): xpath3
  • saxon-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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)