XPath query

Discussion in 'XML' started by John Smith, Jun 8, 2004.

  1. John Smith

    John Smith Guest

    Is it possible using an XPath query to select a child node and all its
    parents. e.g.

    <a x="1">
    <b x="2">
    <c x="3"/>
    <c x="4"/>
    </b>
    <b x="5">
    <c x="6"/>
    <c x="7"/>
    </b>
    </a>

    I want to search for c[@x="4"] and get the the following:
    <a x="1">
    <b x="2">
    <c x="4"/>
    </b>
    </a>

    Is this possible?

    Thanks

    jon
     
    John Smith, Jun 8, 2004
    #1
    1. Advertising

  2. In article <ca3v43$hjt$>,
    John Smith <> wrote:

    >Is this possible?


    No. XPath can only return nodes that exist in the document, but you
    want a modified copy of some nodes. If you had an expression that
    returned the "a" ancestor, it would return the whole document.

    -- Richard
     
    Richard Tobin, Jun 8, 2004
    #2
    1. Advertising

  3. In article <ca3v43$hjt$>, John Smith wrote:
    > Is it possible using an XPath query to select a child node and all its
    > parents. e.g.
    >
    > <a x="1">
    > <b x="2">
    > <c x="3"/>
    > <c x="4"/>
    > </b>
    > <b x="5">
    > <c x="6"/>
    > <c x="7"/>
    > </b>
    > </a>
    >
    > I want to search for c[@x="4"] and get the the following:
    > <a x="1">
    > <b x="2">
    > <c x="4"/>
    > </b>
    > </a>
    >
    > Is this possible?


    The value of the XPath expression

    c[@x="4"]/ancestor-or-self::*

    is a node set that contains the elements that you want.
     
    A. Bolmarcich, Jun 8, 2004
    #3
  4. A. Bolmarcich (Tue, 08 Jun 2004 14:44:27 -0000):
    > The value of the XPath expression
    >
    > c[@x="4"]/ancestor-or-self::*
    >
    > is a node set that contains the elements that you want.


    Does it? I think the result-set will look like that:

    =================
    <c x="4"/>
    ---------------
    <b x="2">
    <c x="3"/>
    <c x="4"/>
    </b>
    ---------------
    <a x="1">
    <b x="2">
    <c x="3"/>
    <c x="4"/>
    </b>
    <b x="5">
    <c x="6"/>
    <c x="7"/>
    </b>
    </a>
    =================


    Chris
    --
    Chris Huebsch www.hübsch-gemacht.de | TU Chemmnitz, Informatik, RNVS
    GPG-Encrypted mail welcome! ID:7F2B4DBA | Str. d. Nationen 62, B204
    Chemnitzer Linux-Tage 2005, 5.-6.März | D-09107 Chemnitz
    http://www.tu-chemnitz.de/linux/tag/ | +49 371 531-1377, Fax -1803
     
    Chris Huebsch, Jun 8, 2004
    #4
  5. In article <-chemnitz.de>, Chris Huebsch wrote:
    > A. Bolmarcich (Tue, 08 Jun 2004 14:44:27 -0000):
    >> The value of the XPath expression
    >>
    >> c[@x="4"]/ancestor-or-self::*
    >>
    >> is a node set that contains the elements that you want.

    >
    > Does it? I think the result-set will look like that:

    [snip]

    As I wrote, it "contains the elements that you want". It does not
    contain a tree consisting of only those elements. A program that
    uses the results of this XPath expression can construct a tree
    consisting only those elements.
     
    A. Bolmarcich, Jun 8, 2004
    #5
  6. A. Bolmarcich (Tue, 08 Jun 2004 21:07:54 -0000):
    > As I wrote, it "contains the elements that you want". It does not
    > contain a tree consisting of only those elements. A program that
    > uses the results of this XPath expression can construct a tree
    > consisting only those elements.


    Ok. If he has to use an other program, then he might simply use
    c[@x="4"] and then use the parent-node-reference of each element
    recursively towards the documentNode.


    Chris
    --
    Chris Huebsch www.hübsch-gemacht.de | TU Chemmnitz, Informatik, RNVS
    GPG-Encrypted mail welcome! ID:7F2B4DBA | Str. d. Nationen 62, B204
    Chemnitzer Linux-Tage 2005, 5.-6.März | D-09107 Chemnitz
    http://www.tu-chemnitz.de/linux/tag/ | +49 371 531-1377, Fax -1803
     
    Chris Huebsch, Jun 8, 2004
    #6
    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,010
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,481
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    555
  4. goog
    Replies:
    0
    Views:
    527
  5. David Gordon

    xpath query query

    David Gordon, May 18, 2005, in forum: XML
    Replies:
    2
    Views:
    819
    David Gordon
    May 18, 2005
Loading...

Share This Page