xpath select nodes which satisfying certain criteria

Questions about XML that are not covered by the other forums should go here.
zqzproject
Posts: 4
Joined: Wed Nov 09, 2005 4:48 pm

xpath select nodes which satisfying certain criteria

Post by zqzproject »

hi, i am writing a java program which process an xml file. and i need a search function, which basically build up xpath. the xml looks:

++++++++++++
<contactlist>
<contact>
<name>abc</name>
<email>abc@email.com</age>
</contact>
<contact>
<name>bcd</name>
<email>bcd@email.com</age>
</contact>
</contactlist>
++++++++++++

and i want to select those contacts that have a name abc and email as abc@email.com. i did the following which does not work:

+++++++++++
string n= "abc";
string e = "abc@email.com"

List nodes = XPath.selectNodes(doc.getRootElement(), "//contact[contains(child::name/child::text(),'" +n+"')]"
+ "&& "//contact[contains(child::email/child::text(),'" +e+"')]");
+++++++++++

however if the search is based on a SINGLE KEYWORD, such as
++++++++++
List nodes = XPath.selectNodes(doc.getRootElement(), "//contact[contains(child::name/child::text(),'" +n+"')]");
++++++++++
it does the proper job. i think i did something wrong in the syntax which combines several conditions. ive tried many things including replace "&&" with "and" and re-write "contain" function but they all did not work.

any help please!!!!
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

Hi,

You are trying to create a wrong XPath expression, what you need is something like:
//contact[name='abc' and email='abc@email.com']
First write down the XPath expression and make sure that it works (you can test that for instance in oXygen) then check that what you are generating in Java as the XPath expression is the same as what you tested.

Best Regards,
George
zqzproject
Posts: 4
Joined: Wed Nov 09, 2005 4:48 pm

thanks

Post by zqzproject »

thanks! now it works
Post Reply