Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Complicated XPATH to return a nodeset based on a sibling node's value

Reply
Thread Tools

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

 
 
Brad
Guest
Posts: n/a
 
      08-31-2004
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!


 
Reply With Quote
 
 
 
 
Marrow
Guest
Posts: n/a
 
      08-31-2004
Hi Brad,

Try something like...

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


HTH
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator


"Brad" <(E-Mail Removed)> wrote in message
news:7G6Zc.15897$(E-Mail Removed)...
> 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!
>
>



 
Reply With Quote
 
 
 
 
A. Bolmarcich
Guest
Posts: n/a
 
      08-31-2004
In article <7G6Zc.15897$(E-Mail Removed)>, Brad wrote:
> 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()
 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      09-01-2004
In article <7G6Zc.15897$(E-Mail Removed)>, Brad <(E-Mail Removed)> wrote:

% 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
East York Canada
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      09-02-2004
In article <(E-Mail Removed)>,
Patrick TJ McPhee <(E-Mail Removed)> wrote:

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

Patrick TJ McPhee
East York Canada
(E-Mail Removed)
 
Reply With Quote
 
tahire72@googlemail.com
Guest
Posts: n/a
 
      01-15-2013
On Tuesday, August 31, 2004 11:09:07 PM UTC+1, Brad wrote:
> 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
 
Reply With Quote
 
Joe Kesselman
Guest
Posts: n/a
 
      01-15-2013
On 1/15/2013 8:55 AM, (E-Mail Removed) wrote:
> On Tuesday, August 31, 2004 11:09:07 PM UTC+1, Brad wrote:
>> 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.


--
Joe Kesselman,
http://www.love-song-productions.com...lam/index.html

{} ASCII Ribbon Campaign | "may'ron DaroQbe'chugh vaj bIrIQbej" --
/\ Stamp out HTML mail! | "Put down the squeezebox & nobody gets hurt."
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find not the next sibling but the 2nd sibling or find sibling "a" OR sinbling "b" localpricemaps@gmail.com Python 11 01-23-2006 07:04 PM
Nodeset Difference AKA Nodeset Reduction Bryan Galvin XML 2 09-29-2004 12:09 PM
Select next sibling XPath? Philipp Lenssen XML 1 11-25-2003 02:26 PM
xpath following-sibling - null return looks for object reference? Kathy Burke ASP .Net 0 08-03-2003 10:43 PM
Problems using the "following-sibling"-expression in XPATH Peter Rohleder XML 3 07-02-2003 01:29 PM



Advertisments