Created
March 3, 2014 17:08
-
-
Save davydany/9329622 to your computer and use it in GitHub Desktop.
Rip through XML files in a Directory with XPATH query with XMLStarlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## DirXPath | |
## -------- | |
## | |
## Requirements: | |
## ============= | |
## Ensure that XMLStarlet is installed on your machine (http://xmlstar.sourceforge.net/) | |
## | |
## Install | |
## ======= | |
## Put the following in your .bashrc (Linux/Unix/Cygwin/MinGW) or .bash_profile (Mac) file. Then source the file. | |
## | |
## ``` | |
## source ~/.bashrc | |
## ``` | |
## | |
## Usage | |
## ===== | |
## Syntax: dirxpath <option: -v / -c> <xpath> | |
## | |
## If you want only values in the node, run: | |
## ``` | |
## dirxpath -v //vs:GivenName | |
## ``` | |
## | |
## If you want a dump of the XML node that corresponds to your | |
## xpath query, run: | |
## ``` | |
## dirxpath -c //vs:GivenName | |
## ``` | |
## | |
## NOTE: My default namespace is "vs", and so I needed to replace | |
## it with "_" to work with xmlstarlet. You can remove this entirely. | |
## To do so, look remove this line: new_xpath=${orig_xpath//vs/_} | |
## and replace all instances of $new_xpath with $old_xpath. | |
xmlstarlet_dir_xpath() | |
{ | |
if [[ $1 == *help* ]] | |
then | |
echo "$0 <xmlstarlet_sel_params> <xpath_query>" | |
else | |
echo "PARAM: $1" | |
echo "XPATH: $2" | |
for f in *.xml; | |
do | |
orig_xpath=$2 | |
new_xpath=${orig_xpath//vs/_} | |
echo "-- $f --------"; | |
echo "`xmlstarlet sel -t $1 $new_xpath $f 2> /dev/null`" | |
done | |
fi | |
} | |
alias dirxpath=xmlstarlet_dir_xpath |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment