trying to use xpath

Discussion in 'XML' started by woodworthjames@yahoo.com, Jan 16, 2007.

  1. Guest

    Hello, not sure i am using xpath correctly. have the following trivial
    xml.
    <?:xml version="1.0" standalone="yes" ?>
    <top attr="1">
    content1
    content2
    <inner id="first" day="friday">
    content3
    </inner>
    </top>

    if i want the content of the tag "inner", do i need either of these or
    something else?
    /top/inner/child::text()
    /top/inner/*[node()=text()]
    and expect to be pointing at "content3".

    thanks.
     
    , Jan 16, 2007
    #1
    1. Advertising

  2. wrote:
    > Hello, not sure i am using xpath correctly. have the following trivial
    > xml.
    > <?:xml version="1.0" standalone="yes" ?>
    > <top attr="1">
    > content1
    > content2
    > <inner id="first" day="friday">
    > content3
    > </inner>
    > </top>
    >
    > if i want the content of the tag "inner", do i need either of these or

    note yeu don't want the content of the tag inner (the content of the
    start tag is "inner id="first" day="friday"" and the content of the end
    tag is "/inner", you want the content of the element (it's best not to
    confuse "tag" and "element" when working with xpath, as xpath has no
    access to the tags.

    > something else?
    > /top/inner/child::text()


    that's the same as
    top/inner/text()
    as child is the default axis, and selects the text node with content3
    (and some white space) note that it's usially better to just use
    /top/inner
    as the string value of the element is all its text descendents. If you
    explictly use text() then it will most likely fail if for example anyone
    adds a comment
    <inner>
    <!-- here -->
    content3
    </inner>
    as then the first text node is _just_ the white space before the comment
    (and most xpath1 string functions only operate on the first node if mre
    than one is selected)

    > /top/inner/*[node()=text()]


    inner* selects all the element node children of inner but in your case
    inner has no element children so this will be empty (even without the
    filter). The filter [node()=text()] is true if any child node is equal
    to any child text node. since the right hand side is a subset f the left
    hand side this is always true if text() is non empty, ie if there is a
    text node. so *[node()=text()] is *[text()] and selects all elements
    that have text node children.

    David



    > and expect to be pointing at "content3".
    >
    > thanks.
    >
     
    David Carlisle, Jan 16, 2007
    #2
    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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,027
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,519
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    570
  4. goog
    Replies:
    0
    Views:
    533
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,698
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page