trying to use xpath

W

woodworthjames

Hello, not sure i am using xpath correctly. have the following trivial
xml.
<?:xml version="1.0" standalone="yes" ?>
<top attr="1">
content1
content2
<inner id="first" day="friday">
content3
</inner>
</top>

if i want the content of the tag "inner", do i need either of these or
something else?
/top/inner/child::text()
/top/inner/*[node()=text()]
and expect to be pointing at "content3".

thanks.
 
D

David Carlisle

Hello, not sure i am using xpath correctly. have the following trivial
xml.
<?:xml version="1.0" standalone="yes" ?>
<top attr="1">
content1
content2
<inner id="first" day="friday">
content3
</inner>
</top>

if i want the content of the tag "inner", do i need either of these or
note yeu don't want the content of the tag inner (the content of the
start tag is "inner id="first" day="friday"" and the content of the end
tag is "/inner", you want the content of the element (it's best not to
confuse "tag" and "element" when working with xpath, as xpath has no
access to the tags.
something else?
/top/inner/child::text()

that's the same as
top/inner/text()
as child is the default axis, and selects the text node with content3
(and some white space) note that it's usially better to just use
/top/inner
as the string value of the element is all its text descendents. If you
explictly use text() then it will most likely fail if for example anyone
adds a comment
<inner>
<!-- here -->
content3
</inner>
as then the first text node is _just_ the white space before the comment
(and most xpath1 string functions only operate on the first node if mre
than one is selected)
/top/inner/*[node()=text()]

inner* selects all the element node children of inner but in your case
inner has no element children so this will be empty (even without the
filter). The filter [node()=text()] is true if any child node is equal
to any child text node. since the right hand side is a subset f the left
hand side this is always true if text() is non empty, ie if there is a
text node. so *[node()=text()] is *[text()] and selects all elements
that have text node children.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top