Page 1 of 1

xpath select nodes which satisfying certain criteria

Posted: Wed Nov 09, 2005 4:59 pm
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!!!!

Posted: Thu Nov 10, 2005 5:40 pm
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

thanks

Posted: Mon Nov 14, 2005 1:30 am
by zqzproject
thanks! now it works