Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active December 30, 2015 00:09
Show Gist options
  • Save coderofsalvation/7747273 to your computer and use it in GitHub Desktop.
Save coderofsalvation/7747273 to your computer and use it in GitHub Desktop.
xpath in bash using php
#!/bin/bash
# parse xpath on the commandline (when php is installed)
# example usage : xpath /tmp/test.xml /catalog/book[1]/title
# example output: XML Developer's Guide
#
# given that /tmp/test.xml looks like:
# <?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>
phpcode_xmlget='
$xml = new SimpleXMLElement( file_get_contents($argv[1]) );
$result = $xml->xpath($argv[2]);
while( list( , $node) = each($result)) { echo (string)$node; break; }
echo "";
'
xpath(){
php -r "$phpcode_xmlget" "$1" "$2"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment