"Gerald W. Lester" <Gerald...@KnG-Consulting.net> writes:
> A little XML example with a couple of your searches where you are
> having issues would help a lot to ensure that everyone is talking
> about the same thing.
Here it goes (along with an idea which I'll be trying out). But first,
I'd like to re-state my targets, since they weren't very clear:
I wanted the user to enter an XPath expression and to show (visually)
which nodes would be selected, in some graphical representation of the
XML tree (whether that'd be a more traditional tree on a canvas or some
hierarchical list representation).
Since it's goig to be a demo, I'd strongly prefer a 99.5-percent
solution which is simple and easy to understand to a complex and messy
100-percent solution (which would be a 99.95% anyway ;-)
So consider this XML snippet:
--- example.xml ---------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<example size="small" useful="you bet">
<section useful="no">
<subsection useful="possibly">
With some text inside
<subsubsection useful="perhaps">
some text here, too
</subsubsection>
And another text here, just to be nasty
</subsection>
A loose text node here, just to show off
what a stupid idea it is to mis-use XML as
a data description language.
<subsection useful="sometimes" empty="yes" />
</section>
<section useful="definitely not">
A last fight here, horrified by the ubiquity of
XML data representations in my favourite distro.
<subsection useful="meh">
Just to be here
</subsection>
<subsection useful="never">
Because we can
</subsection>
</section>
</example>
-------------------------------------------------------
And this little XML program (the most important part is, of course in
the comments). Assume I've already rendered the whole XML tree on the
canvas, tagging each node representation there with its domNode ID.
--- example -------------------------------------------
#!/usr/bin/tclsh8.5
package require tdom
set doc [dom parse -channel [open "example.xml"]]
set docroot [$doc documentElement]
# (1) All subsection nodes, at any depth:
puts [$docroot selectNodes {//subsection}]
# => domNode0x1383470 domNode0x1383900 domNode0x1383ba0 domNode0x1383cd0
#
# Easy: highlight the nodes with the above tags
# All "useful" attributes, somewhere:
puts [$docroot selectNodes {//@useful}]
# => {useful {you bet}} {useful no} {useful possibly} {useful perhaps}
# {useful sometimes} {useful {definitely not}} {useful meh} {useful never}
#
# Argh. But "where" do they live?
puts [$docroot selectNodes {//@useful/..}]
# => domNode0x22d7170 domNode0x22d7350 domNode0x22d74d0 domNode0x22d76c0
# domNode0x22d7960 domNode0x22d7a80 domNode0x22d7c00 domNode0x22d7d30
#
# Ah, there they are! Highlight "domNode0x22d7170@useful" and so on
-------------------------------------------------------
So I tend to do the following.
(1) Eval the user-given XPath
(2) look out for results not matching "domNodeXXXXXX"
(3) If there are any, they must be attributes or text nodes (which are
nodes under the XPath model, but not under the tdom
model).
(4) Re-evaluate the XPath expression with "/.." slapped at the end. The
new result is a list of the parent nodes under (1)
Now, if I've been careful when tagging the node representations on the
canvas, giving, f.ex. to the attribute "useful" of domNode0x22d7170 the
tag "domNode0x22d7170@useful", things should kind of work, right? For
text nodes, I think domNode0x22d74d0#0 "With some..." or
domNode0x22d74d0#1 might do the job (since a node can have more than one
text child). It'll be difficult to differentiate text and attr nodes in
the result of [selectNodes] (yes, yes there is the typeVar there, but
for all practical purposes it's saying "mixed" :-(
There's of course the possibility that some nasty XML has some text
inside like:
<nasty-node>domNode0x22d76c0</nasty-node>
...but hey. You get what you pay for ;-)
Ideas welcome (remember -- I'd like to stay below about 200 lines of
code, even if the section "Known Issues" grows by a bit ;-)
Another problem is that the results are node sets: I didn't look what it
happens when the slapping of "/.." at the end isn't injective: do we get
repetitions (good) -- or does XPath weed out duplicates (bad).
Regards
-- tomás