Just checked in some new files and changes for both the TclXML
and TclDOM packages. This is the start of adding XPath support.
In the TclXML package, added xpath.tcl.
Use it with 'package require xpath'. It provides the command
xpath::split, which takes an XPath location path as an argument
and parses it into its constituent location steps. The result
is a Tcl list containing a number of sublists which have the
parts of the step.
For example:
xpath::split {/simple/example[2]}
would return:
{} {child simple {}} {child example { { 2 } }}
The first sublist is empty, indicating that this is an absolute
location path. Each subsequent step is of the form
{axis node-test {?predicate ...?}}
NB. the abbreviated syntax has been expanded.
WARNING: This is only an initial implementation and the complete
XPath syntax is certainly not yet supported, though that is
the eventual aim. At the moment simple steps and uncomplicated
predicates (not containing a path expression themselves) work OK.
The TclDOM package has a new method for the 'DOMImplementation'
and 'node' commands: createNode. Usage:
dom::DOMImplementation createNode $doc {simple/example[2]}
dom::node createNode $node {simple/example[2]/text()}
The difference between them is that former sets the document root
node as the initial context, whereas the latter sets the given node
as the initial context.
'createNode' will interpret the XPath path and walk the tree looking
for the deepest matching node. If any nodes are missing along the
way they are created. The deepest matching node is returned.
Obviously, not all XPath location paths are going to make sense
in this context.
TODO: a 'selectNode' method which will return all nodes matching
an XPath location path.
Also added is a new 'examples' directory. There is one example
script, 'cgi2dom'. This provides the command 'cgi2dom::createdocument'
which takes name-value pairs. Names starting with '/' are interpreted
as XPath location paths and the 'createNode' method above is invoked
to construct the DOM tree. The value is set as the string value of the
created node. Other names are ignored.
The idea of this package is to allow an HTML fill-in form to
automagically create a DOM tree. Eg.
<form action="createdoc.tml">
Name: <input name="/person/name">
Address: <input name="/person/address">
<input type="submit" name="noeffect" value="Press Me">
</form>
createdoc.tml:
[set html {}
set doc [cgi2dom::createdocument [ncgi::nvlist]]
set html
]
<h1>Submission Accepted</h1>
Thanks! Here's your XML document:
<pre>
[set xml [dom::DOMImplementation serialize $doc]
regsub -all < $xml {\<} xml
regsub -all > $xml {\>} xml
regsub -all & $xml {\&} xml
set xml
]
</pre>
This should result in something looking like (modulo whitespace):
<person>
<name>Steve Ball</name>
<address>1 Markup Way, W3C-Ville</address>
</person>
Comments, suggestions, etc, are welcome.
Enjoy,
Steve Ball
--
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
|