Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 10th, 2006, 06:55 PM
TR
Guest
 
Posts: n/a
Default xPath query

I'm trying the select a element based on values of two attributes. In the
Xml doc i need to look at an attribute minVal and maxVal. if the Value I'm
passing in in the xPath is greate than min "and" less than max, I'll want
to pull all the attributes of that element.

Is this possible with xPath? I'm able to easily query 1 attribute, but not
two.

Thanks

TR


  #2  
Old March 10th, 2006, 07:05 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: xPath query



TR wrote:
[color=blue]
> I'm trying the select a element based on values of two attributes. In
> the Xml doc i need to look at an attribute minVal and maxVal. if the
> Value I'm passing in in the xPath is greate than min "and" less than
> max, I'll want to pull all the attributes of that element.[/color]

//element[$value > @minValue and $value < @maxValue]/@*



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old March 10th, 2006, 07:35 PM
TR
Guest
 
Posts: n/a
Default Re: xPath query

Hello Martin,


Thanks for the reply. I forget to mention 1 thing. I values need to be
greater-than or equal to and less-than or equal to.

I missed the equal to part.

the syntax you provided worked for evaluating two the attributes.

Tr

[color=blue]
> TR wrote:
>[color=green]
>> I'm trying the select a element based on values of two attributes.
>> In the Xml doc i need to look at an attribute minVal and maxVal. if
>> the Value I'm passing in in the xPath is greate than min "and" less
>> than max, I'll want to pull all the attributes of that element.
>>[/color]
> //element[$value > @minValue and $value < @maxValue]/@*
>[/color]


  #4  
Old March 11th, 2006, 05:35 AM
Cerebrus
Guest
 
Posts: n/a
Default Re: xPath query

Hi TR,

XPath supports the operators >= and <= as well. You can use these in
the expression that Martin provided to meet your requirement.

//element[$value >= @minValue and $value <= @maxValue]/@*

HTH,

Regards,

Cerebrus.

  #5  
Old March 11th, 2006, 06:35 AM
TR
Guest
 
Posts: n/a
Default Re: xPath query

Hello Cerebrus,


Thanks for the follow-up. here are some details and results....


Below is a snippet of the Xml I'm trying to select. If a match is found,
I need to use all attribs of the Node

<Node>
<Formgroups>
<Formula MinAmt="0" MaxAmt="51" Group="0" PercentValue="0"> </Formula>
<Formula MinAmt="51" MaxAmt="192" Group="51.00" PercentValue="10">
</Formula>
<Formula MinAmt="192" MaxAmt="620" Group="98.00" PercentValue="15">
</Formula>
<Formula MinAmt="620" MaxAmt="1409" Group="306.80" PercentValue="25">
</Formula>
</Formgroups>
</Node>


EX:: $Value=53 Result: NodeList.item(0) should be: <Formula MinAmt="51"
MaxAmt="192" Group="51.00" PercentValue="10"> </Formula>


Current syntax

/Node/Formgroups/Formula[@MinAmt >= $Value] == Works

/Node/Formgroups/Formula[@MinAmt >= $Value and(@MaxAmt <= $Value)] ===
Fails

/Node/Formgroups/Formula[@MinAmt >= $Value and @MaxAmt <= $Value] ===Fails



Also, I tried the syntax you include at the end of the attribute eval,
" /@* ". Not sure what that does, but I put it in and still no luck. The
xPath expression was valid, just nothing populated in my NodeList


Thanks again!!! Appreciate the assistance.
[color=blue]
> Hi TR,
>
> XPath supports the operators >= and <= as well. You can use these in
> the expression that Martin provided to meet your requirement.
>
> //element[$value >= @minValue and $value <= @maxValue]/@*
>
> HTH,
>
> Regards,
>
> Cerebrus.
>[/color]


  #6  
Old March 11th, 2006, 01:55 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: xPath query



TR wrote:

[color=blue]
> Below is a snippet of the Xml I'm trying to select. If a match is found,
> I need to use all attribs of the Node
>
> <Node>
> <Formgroups>
> <Formula MinAmt="0" MaxAmt="51" Group="0" PercentValue="0"> </Formula>
> <Formula MinAmt="51" MaxAmt="192" Group="51.00" PercentValue="10">
> </Formula>
> <Formula MinAmt="192" MaxAmt="620" Group="98.00" PercentValue="15">
> </Formula>
> <Formula MinAmt="620" MaxAmt="1409" Group="306.80"
> PercentValue="25"> </Formula>
> </Formgroups>
> </Node>[/color]

Here is one example

public static void Test (double value, string url) {
XPathDocument xmlDocument = new XPathDocument(url);
string xPathExpression =
String.Format(
CultureInfo.InvariantCulture,
"Node/Formgroups/Formula[{0} >= @MinAmt and {0} <= @MaxAmt]",
value
);
XPathNavigator xPathNavigator = xmlDocument.CreateNavigator();
XPathNodeIterator nodeIterator =
xPathNavigator.Select(xPathExpression);
while (nodeIterator.MoveNext()) {
Console.WriteLine("Found {0}, attributes are:",
nodeIterator.Current.Name);
if (nodeIterator.Current.MoveToFirstAttribute()) {
do {
Console.WriteLine("Attribute name: {0}, attribute value:
{1}", nodeIterator.Current.Name, nodeIterator.Current.Value);
}
while (nodeIterator.Current.MoveToNextAttribute());
nodeIterator.Current.MoveToParent();
}
Console.WriteLine();
}
}

you could call that as e.g.

Test(53.6, @"example.xml");

and then the output is

Found Formula, attributes are:
Attribute name: MinAmt, attribute value: 51
Attribute name: MaxAmt, attribute value: 192
Attribute name: Group, attribute value: 51.00
Attribute name: PercentValue, attribute value: 10


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #7  
Old March 11th, 2006, 02:05 PM
Cerebrus
Guest
 
Posts: n/a
Default Re: xPath query

Hi TR,

1. The " /@* " at the end selects all the attributes of all the
returned nodes. Martin probably added that in because of your original
statement : "I'll want to pull all the attributes of that element."

2. Well, I don't know if we're on the same page, but in your example :
(Using sample $Value of 53 on your sample XML)

As expected, only the first one of your 3 XPath expressions will work.
Reasons are below...
The first expression selects the 3rd and 4th node.
The second and 3rd expression won't select anything. This is because
you're making a logical mistake here. The 3rd expression should be :
/Node/Formgroups/Formula[@MinAmt >= $Value and $Value <= @MaxAmt].

^^^^^^^^^
This is because you need to select a value that is greater than the
MinAmt and *less than* the MaxAmt. The expression you've written can be
interpreted as MaxAmt <= Value <= MinAmt, which is impossible in your
XML file. So, it does not select anything.

When written properly, your XPath should select the 3rd and 4th node.
(of the sample XML you gave)

Hope this helps,

Regards,

Cerebrus.

  #8  
Old March 11th, 2006, 04:25 PM
Cerebrus
Guest
 
Posts: n/a
Default Re: xPath query

Hi TR,

1. The " /@* " at the end selects all the attributes of all the
returned nodes. Martin probably added that in because of your original
statement : "I'll want to pull all the attributes of that element."


2. Well, I don't know if we're on the same page, but in your example :
(Using sample $Value of 53 on your sample XML)


As expected, only the first one of your 3 XPath expressions will work.
Reasons are below...
The first expression selects the 3rd and 4th node.
The second and 3rd expression won't select anything. This is because
you're making a logical mistake here. The 3rd expression should be :

/Node/Formgroups/Formula[@MinAmt >= $Value and $Value <= @MaxAmt].

This is because you need to select a value that is greater than the
MinAmt and *less than* the MaxAmt. The expression you've written can be

interpreted as MaxAmt <= Value <= MinAmt, which is impossible in your
XML file. So, it does not select anything.


When written properly, your XPath should select the 3rd and 4th node.
(of the sample XML you gave) ,

Edit: And not the 2nd node.


Hope this helps,


Regards,


Cerebrus.

  #9  
Old March 12th, 2006, 09:25 AM
TR
Guest
 
Posts: n/a
Default Re: xPath query

Hello Cerebrus, Martin,


I appreciate the assistance.

The syntax provided partially works. You made it very clear on the logic
error.

I tried the syntax provided, but something still doen't work correctly.
If I provide a value of say, 195, the expression returns record 4 from the
example previously. It should have returned records 3 and 4.


Thanks!!!!


TR
[color=blue]
> Hi TR,
>
> 1. The " /@* " at the end selects all the attributes of all the
> returned nodes. Martin probably added that in because of your original
> statement : "I'll want to pull all the attributes of that element."
>
> 2. Well, I don't know if we're on the same page, but in your example :
> (Using sample $Value of 53 on your sample XML)
>
> As expected, only the first one of your 3 XPath expressions will work.
> Reasons are below...
> The first expression selects the 3rd and 4th node.
> The second and 3rd expression won't select anything. This is because
> you're making a logical mistake here. The 3rd expression should be :
> /Node/Formgroups/Formula[@MinAmt >= $Value and $Value <= @MaxAmt].
>
> This is because you need to select a value that is greater than the
> MinAmt and *less than* the MaxAmt. The expression you've written can
> be
>
> interpreted as MaxAmt <= Value <= MinAmt, which is impossible in your
> XML file. So, it does not select anything.
>
> When written properly, your XPath should select the 3rd and 4th node.
> (of the sample XML you gave) ,
>
> Edit: And not the 2nd node.
>
> Hope this helps,
>
> Regards,
>
> Cerebrus.
>[/color]


  #10  
Old March 12th, 2006, 12:05 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: xPath query



TR wrote:

[color=blue]
> I tried the syntax provided, but something still doen't work correctly.
> If I provide a value of say, 195, the expression returns record 4 from
> the example previously. It should have returned records 3 and 4.[/color]

Why? Greater than or equal to the minimum and less than or equal to the
maximum was your reqirement in your first two posts.
The example provided in the third post was

<Node>
<Formgroups>
<Formula MinAmt="0" MaxAmt="51" Group="0" PercentValue="0"> </Formula>
<Formula MinAmt="51" MaxAmt="192" Group="51.00" PercentValue="10">
</Formula>
<Formula MinAmt="192" MaxAmt="620" Group="98.00"
PercentValue="15"> </Formula>
<Formula MinAmt="620" MaxAmt="1409" Group="306.80"
PercentValue="25"> </Formula>
</Formgroups>
</Node>

For a value of 195 there is only one element

<Formula MinAmt="192" MaxAmt="620" Group="98.00" PercentValue="15">
</Formula>

matching the requirement. Unless you are changing the requirements with
every post.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #11  
Old March 12th, 2006, 06:35 PM
TR
Guest
 
Posts: n/a
Default Re: xPath query

Hello Martin,

Oops, my bad. It is correct. If you noticed it was very early in the morning.

Thansk for your help...


TR
[color=blue]
> TR wrote:
>[color=green]
>> I tried the syntax provided, but something still doen't work
>> correctly. If I provide a value of say, 195, the expression returns
>> record 4 from the example previously. It should have returned
>> records 3 and 4.
>>[/color]
> Why? Greater than or equal to the minimum and less than or equal to
> the
> maximum was your reqirement in your first two posts.
> The example provided in the third post was
> <Node>
> <Formgroups>
> <Formula MinAmt="0" MaxAmt="51" Group="0" PercentValue="0">
> </Formula>
> <Formula MinAmt="51" MaxAmt="192" Group="51.00"
> PercentValue="10">
> </Formula>
> <Formula MinAmt="192" MaxAmt="620" Group="98.00"
> PercentValue="15"> </Formula>
> <Formula MinAmt="620" MaxAmt="1409" Group="306.80"
> PercentValue="25"> </Formula>
> </Formgroups>
> </Node>
> For a value of 195 there is only one element
>
> <Formula MinAmt="192" MaxAmt="620" Group="98.00"
> PercentValue="15"> </Formula>
>
> matching the requirement. Unless you are changing the requirements
> with every post.
>[/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles