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

Discussion in 'XML' started by Thomas Schmidt, Jul 18, 2007.

  1. 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
    Thomas Schmidt, Jul 18, 2007
    #1
    1. Advertising

  2. 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
    Joe Kesselman, Jul 18, 2007
    #2
    1. Advertising

  3. 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
    Joe Kesselman, Jul 18, 2007
    #3
  4. Thomas Schmidt

    Pavel Lepin Guest

    Thomas Schmidt <> wrote in
    <>:
    > 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
    Pavel Lepin, Jul 18, 2007
    #4
  5. 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/
    Martin Honnen, Jul 18, 2007
    #5
  6. > 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
    Joseph Kesselman, Jul 18, 2007
    #6
  7. 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
    Joe Kesselman, Jul 19, 2007
    #7
  8. > > 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.
    Thomas Schmidt, Jul 19, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alastair Cameron
    Replies:
    1
    Views:
    7,398
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  2. Tjerk Wolterink

    XPath: Selecting namspace node

    Tjerk Wolterink, Sep 18, 2004, in forum: XML
    Replies:
    1
    Views:
    422
    Tjerk Wolterink
    Sep 18, 2004
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,423
    Dimitre Novatchev
    Aug 24, 2006
  4. Cory  Nelson
    Replies:
    1
    Views:
    1,105
    Peter Flynn
    Dec 26, 2006
  5. =?ISO-8859-1?Q?Une_B=E9vue?=

    XPath selecting elements afer one having a given text

    =?ISO-8859-1?Q?Une_B=E9vue?=, Feb 22, 2007, in forum: XML
    Replies:
    1
    Views:
    756
    =?ISO-8859-1?Q?Une_B=E9vue?=
    Feb 22, 2007
Loading...

Share This Page