[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]
Re: [xml-dev] How can the content of a leaf element be multipletext nodes?
- From: "Liam R. E. Quin" <liam@fromoldbooks.org>
- To: "xml-dev@lists.xml.org" <xml-dev@lists.xml.org>
- Date: Fri, 11 Feb 2022 11:43:21 -0500
On Fri, 2022-02-11 at 15:12 +0000, Roger L Costello wrote:
>
> For example, how many text nodes does this leaf element have?
>
> <Test>abc<!-- blah -->def</Test>
Two. In what follows, "$" is my shell prompt and lines starting with $
are what i typed:
$ cat /tmp/test.xml
<Test>abc<!-- blah -->def</Test>
$ xpath -e '/Test/node()' /tmp/test.xml
Found 3 nodes in /tmp/colours.xml:
-- NODE --
abc
-- NODE --
<!-- blah -->
-- NODE --
def
$ xpath -e '/Test/text()' /tmp/test.xml
Found 2 nodes in /tmp/colours.xml:
-- NODE --
abc
-- NODE --
def
$ xpath -e '/Test/comment()' /tmp/test.xml
Found 1 nodes in /tmp/colours.xml:
-- NODE --
<!-- blah -->
$
Note that the comment is NOT discarded during parsing.
>
> But the really interesting thing is that if I create an XPath to answer
> this question: What is the text in the <Test> element?
>
> Test/text()
>
> Shockingly, I get this output:
>
> abcdef
>
> Huh???
>
> The previous tests determined that there are two separate text nodes,
> so how can there now be only one text node?
There are two; your XPath interpreter is not showing that very
clearly.
$ xmllint --xpath '/Test[text() = "abcdef"]' /tmp/test.xml
XPath set is empty
(i.e. there is no single text node with that value)
$ xmllint --xpath '/Test[text() = "def"]' /tmp/test.xml
<Test>abc<!-- blah -->def</Test>
$
The XPath implementations i used for the examples are XPath 1 but we
can see the same with BaseX for example and XPath 3:
$ cat try.xq
doc("/tmp/test.xml")/Test/text()
$ basex try.xq
abc
def
$
Note that if i change the query from /Test/text() to /Text/string(), i
get "abcdef". The string() function returns a single string value.
So,
/Test[text() = 'abcdef']
is asking, is there some Test element containing a text node whose
value is 'abcdef' (and there isn't).
/Test[string() = 'abcdef']
is asking, is there some Test element whose string value, made by
joining all text nodes together, is 'abcdef', and there is one.
This second example is the same as,
/Test[. = 'abcdef']
which will also return the whole Test node.
I hope this helps.
Liam
--
Liam Quin, https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations: http://www.fromoldbooks.org
[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]