473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

xPath query

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
Mar 10 '06 #1
10 2256


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/
Mar 10 '06 #2
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

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]/@*

Mar 10 '06 #3
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.

Mar 11 '06 #4
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.
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.

Mar 11 '06 #5


TR wrote:

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/
Mar 11 '06 #6
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.

Mar 11 '06 #7
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.

Mar 11 '06 #8
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
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.

Mar 12 '06 #9


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/
Mar 12 '06 #10
TR
Hello Martin,

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

Thansk for your help...
TR
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.

Mar 12 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Joshua Beall | last post by:
Hi All, I have a task that should be very simple but I'm running into trouble. All I want to do is query a document using XPath, and save the resulting XML in a string. Here's that I am trying...
6
by: 0wl | last post by:
Hi, I am trying to get the value of child from xmlstr = """<p:root xmlns:p="http://tempuri.org/string"><p:child DataType="String">Hellpppp</p:child></p:root>""" using...
8
by: Terry P | last post by:
Are there any tools (java classes, tag libraries) which can translate xpath statements into a SQL query? Given an xpath query which has a predicate that filters node values or attributes, I want...
3
by: gfrommer | last post by:
Hello Everyone, I'm writing a server in java, and I want the clients to pass me an XPath query. I want the XPath queries to be in a specific format though, I'm pretty sure it's valid but I want...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
6
by: dotnetnoob | last post by:
i would like to know how i can build xpath expression dynamiclly. let's say i have a following xml file: <EventEnrollment InstanceNumber = "675"> <EventSource> <ObjectReference...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
2
by: =?Utf-8?B?RGlmZmlkZW50?= | last post by:
Hello All, I am trying to construct an XPath query against an XML document, for a requirement that I have. Below is the XML fragment: <SUBMISSIONS> <SUBMISSION YEAR="2004"> <MONTH...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.