XPath expression to test for empty node?

Discussion in 'XML' started by n_o_s_p_a__m@mail.com, May 6, 2005.

  1. Guest

    My xml doc has many <title></title> and <title> in it, meaning the
    nodes have no content (although some do). How can I test for this?

    I tried title[. is null] (doesn't work)
    I tried //title[node() is null] (doesn't work)
    I tried //title[text() is null] (doesn't work)
    I tried //title[text() = ''] (doesn't work)

    Any suggestions welcome.

    Thank you,
    -KJ
     
    , May 6, 2005
    #1
    1. Advertising

  2. KJ Guest

    I meant (in the 2nd example) <title/>
     
    KJ, May 6, 2005
    #2
    1. Advertising

  3. writes:

    > My xml doc has many <title></title> and <title> in it, meaning the
    > nodes have no content (although some do). How can I test for this?


    This is a faq you should csee the xsl-list faq at www.dpawson.co.uk

    >
    > I tried title[. is null] (doesn't work)

    the is operator is only in XPath2 which is still in draft form
    that is legal but always selects an empty sequence as it selects all
    title elements that are the same node as their child <null> elements,
    which can't happen.

    > I tried //title[node() is null] (doesn't work)

    That is again XPath2 only and is the same as //[null] and selects all
    title elements that have a child element called null.

    > I tried //title[text() is null] (doesn't work)


    that again is xpath2 only and is the empty sequence, it selects all
    title elements which have a text node which is an element node with name
    null. text nodes are never element nodes so this selects nothing.

    > I tried //title[text() = ''] (doesn't work)

    This one is valid XPath1 but selects nothing as it selects all title
    elements that have a text node with value '' but text nodes are never
    empty in Xpath, an empty node has no child at all, not a text node with
    empty string value.

    To test if the current element has no children you just need
    test="not(node())"

    to match on empty title elements you could do
    <xsl:template match="title[not(node())]">
    empty title was here
    </xsl:template>

    >
    > Any suggestions welcome.
    >
    > Thank you,
    > -KJ
     
    David Carlisle, May 6, 2005
    #3
  4. wrote:

    > My xml doc has many <title></title> and <title> in it, meaning the
    > nodes have no content (although some do). How can I test for this?
    >
    > I tried title[. is null] (doesn't work)
    > I tried //title[node() is null] (doesn't work)
    > I tried //title[text() is null] (doesn't work)
    > I tried //title[text() = ''] (doesn't work)


    Use
    //title[not(text())]
    for <title> elements which have no text content.
    Or use
    //title[not(node())]
    for <title> elements which have no child nodes at all (i.e. which are
    empty).



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 7, 2005
    #4
  5. KJ Guest

    thank you very much!
     
    KJ, May 7, 2005
    #5
    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. Replies:
    0
    Views:
    1,648
  2. Jeff
    Replies:
    2
    Views:
    1,095
    Dimitre Novatchev
    Jul 5, 2003
  3. Alastair Cameron
    Replies:
    1
    Views:
    7,522
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  4. ricky
    Replies:
    3
    Views:
    610
    ricky
    May 13, 2004
  5. Tjerk Wolterink
    Replies:
    2
    Views:
    1,506
    Dimitre Novatchev
    Aug 24, 2006
Loading...

Share This Page