13Jun/090
Edit XML Namespaces with sed
Even if you are a high level programing language user, you sometimes have to use some good old tools to gain some productivity. Sed is one of those tools that might come to be handy.
I recently needed to change the xmlns attribute of a lot of XML/XSL files and doing it file by file would have been a real mess. So I wrote this little piece of script shell:
#! /bin/sh NAMESPACE_NAME=$1 NAMESPACE_URI=$(echo $2 | sed -e 's/\//\\\//g') FILE=$3 if [ x${NAMESPACE_NAME} = 'x' -o x${NAMESPACE_URI} = 'x' -o ! -f ${FILE} ]; then echo 'Usage:' ${0} 'NAMESPACE_NAME NAMESPACE_URI FILE' exit 42 fi cat $FILE | sed -e s/xmlns:${NAMESPACE_NAME}=\".*\"/xmlns:${NAMESPACE_NAME}'="'${NAMESPACE_URI}\"/g > $FILE
You can run the script with the following command line:
./change_namespace.sh myns http://my.namespace.uri my_file.xml
Where :
- myns is the namespace name (html, xsl, xhtml, etc...)
- http://my.namespace.uri is the URI of the namespace
- my_file.xml is the file to edit