XPath query

John Smith

Usenet Archive
Joined
May 5, 2000
Messages
1,003
Reaction score
0
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
 

Richard Tobin

Usenet Archive
Joined
Jan 6, 2000
Messages
4,251
Reaction score
0
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. Bolmarcich

Usenet Archive
Joined
May 9, 2002
Messages
219
Reaction score
0
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.
 

Chris Huebsch

Usenet Archive
Joined
Apr 12, 2004
Messages
18
Reaction score
0
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. Bolmarcich

Usenet Archive
Joined
May 9, 2002
Messages
219
Reaction score
0
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.
 

Chris Huebsch

Usenet Archive
Joined
Apr 12, 2004
Messages
18
Reaction score
0
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
548,042
Messages
3,104,224
Members
248,132
Latest member
henrywalsh

Latest Threads

Top