Complicated XPATH to return a nodeset based on a sibling node's value

Brad

Usenet Archive
Joined
Jan 3, 2001
Messages
267
Reaction score
0
Help, I have a really complicated XPATH request I can't wrap my head around

I have an XML nodeset like this:

<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value1</a2>
</a>
<a>
<a1 attr="key">DontUse</a1>
<a1 attr="val">Value2</a1>
</a>
<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value3</a1>
</a>

How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"] only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with ["Value1", "Value3"]

Thanks for any insight! This is driving me nuts! :-(
 

A. Bolmarcich

Usenet Archive
Joined
May 9, 2002
Messages
219
Reaction score
0
Help, I have a really complicated XPATH request I can't wrap my head around

I have an XML nodeset like this:

<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value1</a2>
</a>
<a>
<a1 attr="key">DontUse</a1>
<a1 attr="val">Value2</a1>
</a>
<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value3</a1>
</a>

How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"] only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with ["Value1", "Value3"]

Thanks for any insight! This is driving me nuts! :-(

One way is to work from the root of the tree structure inward:

- You want the a1 nodes with the right attribute and text values

a/a1[@attr="key" and .="Use"]

- Within those nodes you want the a1 nodes of the same parent

a/a1[@attr="key" and .="Use"]/../a1

with the right attribute value

a/a1[@attr="key" and .="Use"]/../a1[@attr="val"]

- Within those nodes you (maybe) want only the text.

a/a1[@attr="key" and .="Use"]/../a1[@attr="val"]/text
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
% How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"] % only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with % ["Value1", "Value3"]

First pick a

a[a1/@attr = "key" and a1 = "Use"]

then get the correct a1 from that

a[a1/@attr = "key" and a1 = "Use"]/a1[@attr = "val"]
 

Patrick TJ McPhee

Usenet Archive
Joined
Apr 2, 2000
Messages
269
Reaction score
0
% a[a1/@attr = "key" and a1 = "Use"]/a1[@attr = "val"]

There's a subtle error here. It should be

a[a1[@attr = "key" and . = "Use"]]/a1[@attr = "val"]

You could read this as `the set of a1 elements with attribute attr equal to "val", whose parent element is an a element which has a child a1 element, which has attribute attr equal to "key" and whose text content is "Use"'.

The incorrect version might read `the set of a1 elements with attribute attr equal to "val", whose parent element is an a element which has a child a1 element which has attribute attr equal to "key" and which also has a child a1 element whose text content is "Use"'.

Sorry.
 

tahire72

Usenet Archive
Joined
Jan 15, 2013
Messages
1
Reaction score
0
Help, I have a really complicated XPATH request I can't wrap my head around

I have an XML nodeset like this:

<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value1</a2>
</a>
<a>
<a1 attr="key">DontUse</a1>
<a1 attr="val">Value2</a1>
</a>
<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value3</a1>
</a>

How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"] only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with ["Value1", "Value3"]

Thanks for any insight! This is driving me nuts! :-(

Detailed XPath tutorial here; http://www.liquid-technologies.com/xpath-tutorial.aspx
 

Joe Kesselman

Usenet Archive
Joined
Feb 3, 2006
Messages
1,229
Reaction score
0
Help, I have a really complicated XPATH request I can't wrap my head around

I have an XML nodeset like this:

<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value1</a2>
</a>
<a>
<a1 attr="key">DontUse</a1>
<a1 attr="val">Value2</a1>
</a>
<a>
<a1 attr="key">Use</a1>
<a1 attr="val">Value3</a1>
</a>

How do I form an XPATH that will return me the nodeset a/a1[@attr="Val"] only when a/a1[@attr="key"] is equal to "Use"??? IE, I want a nodeset with ["Value1", "Value3"]

Assuming you meant "val", not "Val"... Try expressing it in simplest English form. "Find the a's whose key is 'Use' and return their val elements."

a[a1[@attr="key"]="Use"]/a1[@attr="Val"]

Note that this is a perfect example of why documents that use attributes to name the values are a Bad Practice. Your document should almost certainly have been something more like:

<entry>
<key>Use</key>
<val>Value1</key>
</entry>

which would simplify your XPath to
entry[key="use"]/val

Much easier to maintain, and in many implementations will run significantly faster.
 

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