XPath query

J

John Smith

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
 
R

Richard Tobin

John Smith said:
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
 
A

A. Bolmarcich

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

Chris Huebsch

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
 
A

A. Bolmarcich

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

Chris Huebsch

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top