xpath select nodes which satisfying certain criteria
Posted: Wed Nov 09, 2005 4:59 pm
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!!!!
++++++++++++
<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!!!!