|
From: Steve B. <Ste...@zv...> - 2000-12-06 22:29:44
|
New files have been checked in to both the tclxml and tcldom
modules.
The major addition is the new 'xmlswitch' package. This package
provides two commands: 'xmlswitch::xmlswitch' and
'xmlswitch::domswitch'.
The reason for the redundancy is that I figure most people will
use 'namespace import' anyway.
This work is at a very early stage, and I am seeking comments and
suggestions on how these commands might be improved.
The 'xmlswitch' command aims to function in a way reminiscent
of the Tcl 'switch' command, except that an XML document is the
subject of the switch and the case labels are XPath location paths.
The script associated with each template is evaluated in the caller's
context when the location path matches.
For example:
xmlswitch <test><example/></test> {
example[2] {
puts "I found the second example element"
}
/test/example/text() {
# Absolute path
}
* {
puts "The default case"
}
}
This should produce the output:
The default case
The default case
Case labels/script pairs are "templates", which is quite analogous
to XSLT. It is also possible to specify the templates as multiple
arguments, to allow substitution:
xmlswitch {<test><example>Some text</example></test>} \
example/text() {
# Get the text
} \
{example[2]} {
# Find the second example
}
Notice how the second location path must be braced in this example.
The similarities to XSLT are only surface deep. xmlswitch has quite
a different processing model. Firstly, the XML document is parsed
at command execution time and the templates matched as the parsing
occurs, ie sequentially. The idea is to make this construct
suitable for processing large documents. Unlike XSLT, every XML
construct (start tags, character data, etc) is checked in document
order.
Future versions will allow the template script to return a 'break'
or 'continue' error code to alter the control flow. At the moment
templates are matched when the start tag for an element occurs,
but one possible chnge is to allow end tags to match.
This command does a similar job to the 'xmlact' package used by
Ajuba2/Ajuba3[1]. The major differences are that 'xmlswitch' is a
single command, the complete XPath spec will be supported and
finally that 'xmlswitch' _does not_ extract data from the document.
To achieve the latter, the 'selectNode' and 'getStringValue' methods
of the DOM package may be used (I haven't yet implemented the latter).
In combination, the facilities provided here should be more powerful
than 'xmlact' (anyone have any comments on that?).
As the document is parsed a partial DOM tree is built. The XPath
location paths are matched against this tree. This means that
certain kinds of paths may not match as they would with XSLT;
namely those that look ahead in the tree. For example 'example[simple]'
will not match, because the 'example' element occurs before 'simple'
in document order (but see above about matching end tags).
If the '-async' option is given to xmlswitch, then the command returns
a token which can be used to retrieve the DOM root node. There will
also be a facility for getting the DOM node which matches a template.
The '-async' also allows incremental parsing, which is obviously
very important when processing large documents.
The 'domswitch' command behaves very similarly to 'xmlswitch',
except that it takes a pre-built DOM tree as its argument.
In this case, all XPath location paths will match as expected.
I haven't implemented this command yet.
Enjoy,
Steve Ball
References
==========
[1] 'Scriptics Connect', E. Melski, S. Stanton, J. Ousterhout.
Proceedings of the 7th USENIX Tcl/Tk Conference.
--
Steve Ball | waX Me Lyrical XML Editor | Training & Seminars
Zveno Pty Ltd | Web Tcl Complete | XML XSL
http://www.zveno.com/ | TclXML TclDOM | Tcl, Web Development
Ste...@zv... +---------------------------+---------------------
Ph. +61 2 6242 4099 | Mobile (0413) 594 462 | Fax +61 2 6242 4099
|