On 24 May, Kevin Partin wrote:
> Given an XML document with the following line in it
>
> <library>test</library>
>
> shouldn't the following command return the text node containing the
> value "test"
>
> % set node [$root selectNode library/text()=\"test\"]
No.
> Instead, the command returns the value 1, which I interpret to mean that
> the node was found.
The return value 1 is correct. An XPath expression doesn't necessary
return always a node set.
Your XPath expression is a boolean expression. You ask the XPath
engine, if a certain node set (your /library/text()) is equal (your =)
to a literal (your "test"). XPath does automatic 'type conversion'
(see for the detailed rules
http://www.w3.org/TR/xpath#booleans) and
returns the result of the boolean expression to you.
If the [expr] way of doing math isn't convoluted enough for your tast,
you could give the selectNodes method a try and you your math with it:
puts [$root selectNodes 2.3*5.4]
If you think, to use tcl for string processing is way too easy for a
real programmer, why not use the selectNodes method:
puts [$root selectNodes concat("foo","bar")]
And so one.
> The following command will return the library
> element itself
>
> % set node [$root selectNode library\[text()=\"test\"\]]
> domNode0x80b6a04
> % $node asXML
> <library>test</library>
That's the right result. If you want the text node itself, as I
suspect (but that smells like you're eventually working to hard), use
set node [$root selectNodes {library/text()[.='test']}]
rolf