Home All Groups Group Topic Archive Search About
Author
10 Mar 2006 6:37 PM
TR
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

Author
10 Mar 2006 6:48 PM
Martin Honnen
TR wrote:

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

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



--

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/
Author
10 Mar 2006 7:06 PM
TR
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


Show quote
> TR wrote:
>
>> 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.
>>
> //element[$value > @minValue and $value < @maxValue]/@*
>
Author
11 Mar 2006 5:26 AM
Cerebrus
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.
Author
11 Mar 2006 6:25 AM
TR
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.

Show quote
> 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.
>
Author
11 Mar 2006 1:43 PM
Martin Honnen
TR wrote:


Show quote
> 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>

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/
Author
11 Mar 2006 1:54 PM
Cerebrus
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.
Author
11 Mar 2006 4:12 PM
Cerebrus
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.
Author
12 Mar 2006 9:13 AM
TR
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

Show quote
> 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.
>
Author
12 Mar 2006 11:55 AM
Martin Honnen
TR wrote:


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

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/
Author
12 Mar 2006 6:19 PM
TR
Hello Martin,

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

Thansk for your help...


TR

Show quote
> TR wrote:
>
>> 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.
>>
> 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.
>

AddThis Social Bookmark Button