source: subversion/applications/utils/xsltrans/xsltrans @ 30525

Last change on this file since 30525 was 1960, checked in by jochen, 13 years ago

Changed to use /bin/bash instead of /bin/sh

  • Property svn:executable set to *
File size: 4.1 KB
Line 
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
24prgname=`basename $0`
25
26function usage() {
27    echo >&2 "Usage: $prgname [options] <xmlfile> [<xslfile>]"
28    echo >&2 "Options: -p, --param name=value          Set XSL XPATH parameter"
29    echo >&2 "         -s, --stringparam name=value    Set XSL string parameter"
30}
31
32paralist=$(getopt --unquoted "--name=$prgname" --options=p:s: --longoptions=param:,stringparam: -- $@)
33
34if  [ $? -ne 0 ]; then
35    usage
36    exit 1
37fi
38
39set -- $paralist
40pp=""
41ps=""
42for option
43    do case "$option" in
44        -p|--param)       shift; pp="$pp $1"; shift;;
45        -s|--stringparam) shift; ps="$ps $1"; shift;;
46        --)               shift; break;;
47    esac
48done
49
50xml="$1"; shift
51if [ "$xml" = "" ]; then
52    echo >&2 "$prgname: Missing input XML file"
53    usage
54    exit 1
55fi
56
57xsl="$1"; shift
58if [ "$*" != '' ]; then
59    echo >&2 "$prgname: Too many parameters"
60    usage
61    exit 1
62fi
63
64##echo "pp=$pp"
65##echo "ps=$ps"
66##echo "xml=$xml"
67##echo "xsl=$xsl"
68
69# if XSLTRANS environment variable is not set, we try to autodetect it
70if [ "$XSLTRANS" = "" ]; then
71    XSLTRANS=`which xsltproc`
72    if [ "$XSLTRANS" = "" ]; then
73        XSLTRANS=`which xmlstarlet`
74        if [ "$XSLTRANS" = "" ]; then
75            XSLTRANS=`which xalan`
76            if [ "$XSLTRANS" = "" ]; then
77                if [ -e /usr/share/java/xalan2.jar ]; then
78                    XSLTRANS="/usr/share/java/xalan2.jar"
79                fi
80            fi
81        fi
82    fi
83fi
84
85if [ "$XSLTRANS" = "" ]; then
86    echo >&2 "Can't find any XSL transformer on your system."
87    echo >&2 "Please set the XSLTRANS environment variable to path of an XSL transformer."
88    exit 2
89fi
90
91##echo "XSLTRANS=$XSLTRANS"
92
93# We now have the XSL transformator we want to use in XSLTRANS
94
95# If the transformator is a java .jar file, change the command to call java
96command="$XSLTRANS"
97if [[ "$command" =~ '\.jar$' ]]; then
98    command="java -jar /usr/share/java/$XSLTRANS"
99fi
100
101##echo "command=$command"
102
103xsltrans=`basename $XSLTRANS`
104
105if [ "$xsltrans" = "xsltproc" ]; then
106    pl=''
107    for p in $pp; do
108        k=${p%%=*}
109        v=${p##*=}
110        pl="$pl --param $k $v"
111    done
112    for p in $ps; do
113        k=${p%%=*}
114        v=${p##*=}
115        pl="$pl --stringparam $k $v"
116    done
117    $command $pl $xsl $xml
118elif [ "$xsltrans" = "xmlstarlet" ]; then
119    if [ "$xsl" = '' ]; then
120        echo >&2 "xmlstarlet doesn't work without explicit stylesheet"
121        exit 4
122    fi
123
124    pl=''
125    for p in $pp; do
126        pl="$pl -p $p"
127    done
128    for p in $ps; do
129        pl="$pl -s $p"
130    done
131    $command tr $xsl $pl $xml
132elif [ "$xsltrans" = "xalan" ]; then
133    pl=''
134    for p in $pp; do
135        k=${p%%=*}
136        v=${p##*=}
137        pl="$pl -param $k $v"
138    done
139    for p in $ps; do
140        k=${p%%=*}
141        v=${p##*=}
142        pl="$pl -param $k '$v'"
143    done
144    if [ "$xsl" != '' ]; then
145        xsl="-xsl $xsl"
146    fi
147    $command $pl -in $xml $xsl
148elif [ "$xsltrans" = "xalan2.jar" ]; then
149    pl=''
150    for p in $pp; do
151        k=${p%%=*}
152        v=${p##*=}
153        pl="$pl -PARAM $k $v"
154    done
155    for p in $ps; do
156        k=${p%%=*}
157        v=${p##*=}
158        pl="$pl -PARAM $k $v"
159    done
160    if [ "$xsl" != '' ]; then
161        xsl="-XSL $xsl"
162    fi
163    $command $pl -IN $xml $xsl
164else
165    echo >&2 "Unknown XSL transformator '$xsltrans'"
166    exit 3
167fi
168
Note: See TracBrowser for help on using the repository browser.