| 1 | #!/bin/bash
|
|---|
| 2 | #
|
|---|
| 3 | # xsltrans [options] <xmlfile> [<xslfile>]
|
|---|
| 4 | #
|
|---|
| 5 | # This little utility script will call the XSL transformator of your choice.
|
|---|
| 6 | #
|
|---|
| 7 | # Options:
|
|---|
| 8 | # -p, --param name=value Set XSL XPATH parameter
|
|---|
| 9 | # -s, --stringparam name=value Set XSL string parameter
|
|---|
| 10 | #
|
|---|
| 11 | # The <xmlfile> is the input XML file. The <xslfile> is the XSL stylesheet.
|
|---|
| 12 | # If you have a xml-stylesheet processing instruction in your XML file,
|
|---|
| 13 | # you don't need to add the <xslfile>. Output is always on STDOUT.
|
|---|
| 14 | #
|
|---|
| 15 | # If the environment variable XSLTRANS is set, it will call the program
|
|---|
| 16 | # named in the variable. If the content of XSLTRANS ends in .jar, it will
|
|---|
| 17 | # call java with this JAR file as a parameter.
|
|---|
| 18 | #
|
|---|
| 19 | # xsltrans knows the calling syntax of xsltproc, xmlstarlet, xalan (c++)
|
|---|
| 20 | # and xalan (Java) and will supply the parameters in the right form.
|
|---|
| 21 | # Note that not all programs support all options.
|
|---|
| 22 | #
|
|---|
| 23 |
|
|---|
| 24 | #set -x
|
|---|
| 25 |
|
|---|
| 26 | prgname=`basename $0`
|
|---|
| 27 |
|
|---|
| 28 | function usage() {
|
|---|
| 29 | echo >&2 "Usage: $prgname [options] <xmlfile> [<xslfile>]"
|
|---|
| 30 | echo >&2 "Options: -p, --param name=value Set XSL XPATH parameter"
|
|---|
| 31 | echo >&2 " -s, --stringparam name=value Set XSL string parameter"
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | paralist=$(getopt --unquoted "--name=$prgname" --options=p:s: --longoptions=param:,stringparam: -- $@)
|
|---|
| 35 |
|
|---|
| 36 | if [ $? -ne 0 ]; then
|
|---|
| 37 | usage
|
|---|
| 38 | exit 1
|
|---|
| 39 | fi
|
|---|
| 40 |
|
|---|
| 41 | set -- $paralist
|
|---|
| 42 | pp=""
|
|---|
| 43 | ps=""
|
|---|
| 44 | for option
|
|---|
| 45 | do case "$option" in
|
|---|
| 46 | -p|--param) shift; pp="$pp $1"; shift;;
|
|---|
| 47 | -s|--stringparam) shift; ps="$ps $1"; shift;;
|
|---|
| 48 | --) shift; break;;
|
|---|
| 49 | esac
|
|---|
| 50 | done
|
|---|
| 51 |
|
|---|
| 52 | xml="$1"; shift
|
|---|
| 53 | if [ "$xml" = "" ]; then
|
|---|
| 54 | echo >&2 "$prgname: Missing input XML file"
|
|---|
| 55 | usage
|
|---|
| 56 | exit 1
|
|---|
| 57 | fi
|
|---|
| 58 |
|
|---|
| 59 | xsl="$1"; shift
|
|---|
| 60 | if [ "$*" != '' ]; then
|
|---|
| 61 | echo >&2 "$prgname: Too many parameters"
|
|---|
| 62 | usage
|
|---|
| 63 | exit 1
|
|---|
| 64 | fi
|
|---|
| 65 |
|
|---|
| 66 | ##echo "pp=$pp"
|
|---|
| 67 | ##echo "ps=$ps"
|
|---|
| 68 | ##echo "xml=$xml"
|
|---|
| 69 | ##echo "xsl=$xsl"
|
|---|
| 70 |
|
|---|
| 71 | # if XSLTRANS environment variable is not set, we try to autodetect it
|
|---|
| 72 | if [ "$XSLTRANS" = "" ]; then
|
|---|
| 73 | XSLTRANS=`which xsltproc`
|
|---|
| 74 | if [ "$XSLTRANS" = "" ]; then
|
|---|
| 75 | XSLTRANS=`which xmlstarlet`
|
|---|
| 76 | if [ "$XSLTRANS" = "" ]; then
|
|---|
| 77 | XSLTRANS=`which xalan`
|
|---|
| 78 | if [ "$XSLTRANS" = "" ]; then
|
|---|
| 79 | if [ -e /usr/share/java/xalan2.jar ]; then
|
|---|
| 80 | XSLTRANS="/usr/share/java/xalan2.jar"
|
|---|
| 81 | fi
|
|---|
| 82 | fi
|
|---|
| 83 | fi
|
|---|
| 84 | fi
|
|---|
| 85 | fi
|
|---|
| 86 |
|
|---|
| 87 | if [ "$XSLTRANS" = "" ]; then
|
|---|
| 88 | echo >&2 "Can't find any XSL transformer on your system."
|
|---|
| 89 | echo >&2 "Please set the XSLTRANS environment variable to path of an XSL transformer."
|
|---|
| 90 | exit 2
|
|---|
| 91 | fi
|
|---|
| 92 |
|
|---|
| 93 | ##echo "XSLTRANS=$XSLTRANS"
|
|---|
| 94 |
|
|---|
| 95 | # We now have the XSL transformator we want to use in XSLTRANS
|
|---|
| 96 |
|
|---|
| 97 | # If the transformator is a java .jar file, change the command to call java
|
|---|
| 98 | command="$XSLTRANS"
|
|---|
| 99 | if [[ "$command" =~ '\.jar$' ]]; then
|
|---|
| 100 | command="java -jar /usr/share/java/$XSLTRANS"
|
|---|
| 101 | fi
|
|---|
| 102 |
|
|---|
| 103 | ##echo "command=$command"
|
|---|
| 104 |
|
|---|
| 105 | xsltrans=`basename $XSLTRANS`
|
|---|
| 106 |
|
|---|
| 107 | if [ "$xsltrans" = "xsltproc" ]; then
|
|---|
| 108 | pl=''
|
|---|
| 109 | for p in $pp; do
|
|---|
| 110 | k=${p%%=*}
|
|---|
| 111 | v=${p##*=}
|
|---|
| 112 | pl="$pl --param $k $v"
|
|---|
| 113 | done
|
|---|
| 114 | for p in $ps; do
|
|---|
| 115 | k=${p%%=*}
|
|---|
| 116 | v=${p##*=}
|
|---|
| 117 | pl="$pl --stringparam $k $v"
|
|---|
| 118 | done
|
|---|
| 119 | $command $pl $xsl $xml
|
|---|
| 120 | elif [ "$xsltrans" = "xmlstarlet" ]; then
|
|---|
| 121 | if [ "$xsl" = '' ]; then
|
|---|
| 122 | echo >&2 "xmlstarlet doesn't work without explicit stylesheet"
|
|---|
| 123 | exit 4
|
|---|
| 124 | fi
|
|---|
| 125 |
|
|---|
| 126 | pl=''
|
|---|
| 127 | for p in $pp; do
|
|---|
| 128 | pl="$pl -p $p"
|
|---|
| 129 | done
|
|---|
| 130 | for p in $ps; do
|
|---|
| 131 | pl="$pl -s $p"
|
|---|
| 132 | done
|
|---|
| 133 | $command tr $xsl $pl $xml
|
|---|
| 134 | elif [ "$xsltrans" = "xalan" ]; then
|
|---|
| 135 | pl=''
|
|---|
| 136 | for p in $pp; do
|
|---|
| 137 | k=${p%%=*}
|
|---|
| 138 | v=${p##*=}
|
|---|
| 139 | pl="$pl -param $k $v"
|
|---|
| 140 | done
|
|---|
| 141 | for p in $ps; do
|
|---|
| 142 | k=${p%%=*}
|
|---|
| 143 | v=${p##*=}
|
|---|
| 144 | pl="$pl -param $k '$v'"
|
|---|
| 145 | done
|
|---|
| 146 | if [ "$xsl" != '' ]; then
|
|---|
| 147 | xsl="-xsl $xsl"
|
|---|
| 148 | fi
|
|---|
| 149 | $command $pl -in $xml $xsl
|
|---|
| 150 | elif [ "$xsltrans" = "xalan2.jar" ]; then
|
|---|
| 151 | pl=''
|
|---|
| 152 | for p in $pp; do
|
|---|
| 153 | k=${p%%=*}
|
|---|
| 154 | v=${p##*=}
|
|---|
| 155 | pl="$pl -PARAM $k $v"
|
|---|
| 156 | done
|
|---|
| 157 | for p in $ps; do
|
|---|
| 158 | k=${p%%=*}
|
|---|
| 159 | v=${p##*=}
|
|---|
| 160 | pl="$pl -PARAM $k $v"
|
|---|
| 161 | done
|
|---|
| 162 | if [ "$xsl" != '' ]; then
|
|---|
| 163 | xsl="-XSL $xsl"
|
|---|
| 164 | fi
|
|---|
| 165 | $command $pl -IN $xml $xsl
|
|---|
| 166 | else
|
|---|
| 167 | echo >&2 "Unknown XSL transformator '$xsltrans'"
|
|---|
| 168 | exit 3
|
|---|
| 169 | fi
|
|---|
| 170 |
|
|---|