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

T

Thomas Schmidt

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
 
J

Joe Kesselman

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()]
 
J

Joe Kesselman

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()]
 
P

Pavel Lepin

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.
 
M

Martin Honnen

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.
 
J

Joseph Kesselman

Please note that x has as its first child a text node with white space.

Good point.
 
J

Joe Kesselman

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(.)]]
 
T

Thomas Schmidt

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
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top