xpath select nodes which satisfying certain criteria
Questions about XML that are not covered by the other forums should go here.
-
- 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!!!!
++++++++++++
<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!!!!
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
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
-
- Posts: 4
- Joined: Wed Nov 09, 2005 4:48 pm
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service