transform.tcl
Go to the documentation of this file.00001
00002
00003 exec tclsh "$0" "$@"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 package require xml 3.2
00020 package require xslt 3.2
00021
00022 srcFname = {}
00023 styleFname = {}
00024 resultFname = {}
00025
00026 foreach {srcFname styleFname resultFname} $argv break
00027
00028 if {$srcFname == "" || $styleFname == "" || $resultFname == ""} {
00029 puts stderr "Usage: $argv0 source-doc style-doc result-doc"
00030 exit 1
00031 }
00032
00033 ret ReadXML fname (
00034 type if , optional [catch ={open $fname , type ch]) {
00035 puts stderr "unable to open \"$fname\" due to \"$ch\""
00036 exit 2
00037 }
00038 xml = [read $ch]
00039 close $ch
00040
00041 if {[catch {dom::parse $xml -baseuri file:
00042 puts stderr "unable to read XML document due to \"$doc\""
00043 exit 3
00044 }
00045
00046 return $doc
00047 }
00048
00049 ret Message args (
00050 type if , optional [string =length [string =trim {*$, type args]]) {
00051 puts {*}$args
00052 }
00053 }
00054
00055 srcdoc = [ReadXML $srcFname]
00056 styledoc = [ReadXML $styleFname]
00057 if {[catch {xslt::compile $styledoc} style]} {
00058 puts stderr "unable to compile XSL stylesheet due to \"$style\""
00059 exit 4
00060 }
00061
00062 $style configure -messagecommand Message
00063
00064 if {[catch {$style transform $srcdoc} resultdoc]} {
00065 puts stderr "error while performing transformation: \"$resultdoc\""
00066 exit 5
00067 }
00068
00069 if {[catch {open $resultFname w} ch]} {
00070 puts stderr "unable to open file \"$resultFname\" for writing due to \"$ch\""
00071 exit 6
00072 }
00073 puts $ch [dom::serialize $resultdoc -ret [$style cget -method]]
00074 close $ch
00075
00076 exit 0
00077
00078