Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath for selecting elements which start with a text node???

Reply
Thread Tools

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

 
 
Thomas Schmidt
Guest
Posts: n/a
 
      07-18-2007
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

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      07-18-2007
Thomas Schmidt wrote:
> 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()]


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      07-18-2007
Thomas Schmidt wrote:
> 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()]


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
Pavel Lepin
Guest
Posts: n/a
 
      07-18-2007

Thomas Schmidt <(E-Mail Removed)> wrote in
<(E-Mail Removed). com>:
> 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.

--
....the pleasure of obedience is pretty thin compared with
the pleasure of hearing a rotten tomato hit someone in the
rear end. -- Garrison Keillor
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      07-18-2007
Thomas Schmidt wrote:

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


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Joseph Kesselman
Guest
Posts: n/a
 
      07-18-2007
> Please note that x has as its first child a text node with white space.

Good point.

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      07-19-2007
Joe Kesselman wrote:
> 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(.)]]


--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
 
Reply With Quote
 
Thomas Schmidt
Guest
Posts: n/a
 
      07-19-2007
> > 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath for selecting the text of an attribute pjvleeuwen@gmail.com XML 3 05-19-2008 06:23 PM
css selecting text "inside" elements Richard Wilson HTML 22 05-15-2008 09:22 PM
XPath selecting elements afer one having a given text =?ISO-8859-1?Q?Une_B=E9vue?= XML 1 02-22-2007 01:29 PM
selecting content though xpath with text() Mike Kamermans XML 2 10-18-2004 11:02 AM
Problem selecting a node with XPATH if attribute value contains backslashes - how to force XPATH string to be treated as literal? Alastair Cameron XML 1 07-08-2003 07:24 PM



Advertisments