XPath for selecting elements which start with a text node???

Thomas Schmidt

Usenet Archive
Joined
Nov 12, 2001
Messages
9
Reaction score
0
Hi all,

I need an XPath which selects all nodes of a specific name which start with a text node, i.e. I want the expression to select:

<x>
abcdefg
<y>hijklmn</y>
</x>

but NOT

<x>
<y>hijklmn</y>
abcdefg
</x>

I cant' seem to find the correct expression. Can somebody help?

Thanks,

Thomas
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
Thomas said:
I need an XPath which selects all nodes of a specific name which start with a text node

Node with specific name (x) with a first child that is a text node.

x/node[0][self::text]
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
Thomas said:
I need an XPath which selects all nodes of a specific name which start with a text node

Node with specific name (x) with a first child that is a text node.

x/node[1][self::text]
 

Pavel Lepin

Usenet Archive
Joined
Apr 20, 2007
Messages
263
Reaction score
0
Thomas Schmidt said:
I need an XPath which selects all nodes of a specific name which start with a text node, i.e. I want the expression to select:
x[child::node[1][self::text]]

<x>
abcdefg
<y>hijklmn</y>
</x>

but NOT

<x>
<y>hijklmn</y>
abcdefg
</x>

In both of your examples the first child of the x element is a text node, though.
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
Thomas said:
I need an XPath which selects all nodes of a specific name which start with a text node, i.e. I want the expression to select:

<x>
abcdefg
<y>hijklmn</y>
</x>

but NOT

<x>
<y>hijklmn</y>
abcdefg
</x>

Please note that x has as its first child a text node with white space. So //x[node[1][self::text]] holds for both samples. //x[node[1][self::text and normalize-space(.)]] does hold for the first sample only.
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
Joe said:
Node with specific name (x) with a first child that is a text node.
x/node[1][self::text]

Whups. Sloppy; that selects the text node. Pavel's solution is correct for the question posed

x[child::node[1][self::text]]

and as Martin pointed out, if you want only non-whitespace textnodes you can get that using

x[child::node[1][self::text and normalize-space(.)]]
 

Thomas Schmidt

Usenet Archive
Joined
Nov 12, 2001
Messages
9
Reaction score
0
Please note that x has as its first child a text node with white space.

Yes, I hadn't thought of whitespace... It's working now.
 

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

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,246
Members
248,133
Latest member
DujonT

Latest Threads

Top