Fix the xpath script to enclose results in an <xml_result></xml_result> element
[SXSI/xpathcomp.git] / tests / scripts / xpath.sh
1 #!/bin/bash
2
3 COUNT=0
4 QUERY=""
5 FILE=""
6
7 function usage {
8     echo "usage: $0 [options] <query> <file>"
9     echo "options:"
10     echo "-c     counting mode"
11     echo "-h     display this help"
12 }
13
14 for i in "$@"
15 do
16     if [ "$i" == "-h" ]
17     then
18         usage
19         exit 0
20     elif [ "$i" == "-c" ]
21     then
22         COUNT=1
23         continue
24     elif [ "$QUERY" == "" ]
25     then
26         QUERY="$i"
27         continue
28     else
29         FILE="$i"
30     fi
31 done
32
33 if [ -z "$FILE" ]
34 then
35     echo "error: missing filename"
36     usage
37     exit 1
38 fi
39
40 echo "<xml_result>"
41 if [ "$COUNT" == "0" ]
42 then
43     xmlstarlet sel -t -m "$QUERY" -c . -n "$FILE"
44 else
45     xmlstarlet sel -t -v "count($QUERY)" "$FILE"
46 fi
47 echo "</xml_result>"