Add script to test XPath expressions (requires xmlstarlet)
[SXSI/xpathcomp.git] / tests / scripts / xpath.sh
diff --git a/tests/scripts/xpath.sh b/tests/scripts/xpath.sh
new file mode 100755 (executable)
index 0000000..abb32b4
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/bash
+
+COUNT=0
+QUERY=""
+FILE=""
+
+function usage {
+    echo "usage: $0 [options] <query> <file>"
+    echo "options:"
+    echo "-c     counting mode"
+    echo "-h     display this help"
+}
+
+for i in "$@"
+do
+    if [ "$i" == "-h" ]
+    then
+       usage
+       exit 0
+    elif [ "$i" == "-c" ]
+    then
+       COUNT=1
+       continue
+    elif [ "$QUERY" == "" ]
+    then
+       QUERY="$i"
+       continue
+    else
+       FILE="$i"
+    fi
+done
+
+if [ -z "$FILE" ]
+then
+    echo "error: missing filename"
+    usage
+    exit 1
+fi
+
+
+if [ "$COUNT" == "0" ]
+then
+    xmlstarlet sel -t -m "$QUERY" -c . -n "$FILE"
+else
+    xmlstarlet sel -t -v "count($QUERY)" "$FILE"
+fi