0

I doubt there is a way to get the element which has a particular value (text) from xml document using xpath.

Example doc:

<domain log-root="/logs" application-root="/applications"><resources>
<jdbc-resource pool-name="SamplePool" jndi-name="jdbc/sample" />
<jdbc-resource pool-name="TimerPool" jndi-name="abc">text1</jdbc-resource>
<jdbc-resource pool-name="TimerPool" jndi-name="def">text2</jdbc-resource>
<jdbc-resource pool-name="TimerPool" jndi-name="ghi">text3</jdbc-resource></resources</domain>

Example xPath Query:

/domain//jdbc-resource[@pool-name='TimerPool']/text()='text2'

Please post your ideas if there is any.

2
  • Ultimately I would like to get the element "<jdbc-resource pool-name="TimerPool" jndi-name="def">text2</jdbc-resource>" as the result Commented Nov 19, 2011 at 4:29
  • Can you post me the document link which explains this kind of special tangs? Commented Nov 19, 2011 at 10:53

2 Answers 2

3

Use:

/domain/*/jdbc-resource[@pool-name='TimerPool' and .='text2']

or you may use:

/domain/*/jdbc-resource[@pool-name='TimerPool'][.='text2']

Both expressions above select all jdbc-resource elements the string value of whose pool-name attribute is "TimerPoool" and whose string value (of the jdbc-resource element) is "text2" and that are grand-children of the top element of the XML document.

Sign up to request clarification or add additional context in comments.

6 Comments

Post me the link that explains kind of syntax
@NageswaraRao: You are welcome. As for "the link that explains kind of syntax", I would recommend reading a good book. Here is my answer to a similar question (while the listed resources seem to be on XSLT, they also completely cover XPath, because XPath is "almost all" in XSLT: stackoverflow.com/questions/339930/…
I understood xPath uses DOM inside. Is there a way to update the nodes and write back?
@NageswaraRao: Both your statement is genarally wrong and the answer to your question is negative. An XPath processor isn't obliged at all to use DOM, and in fact efficient XPath implementations, like .NET's XPathDocument don't use DOM. XPath is a query language for XML documents and as such it is readonly. Evaluation of an XPath query never changes an XML document and never produces altered nodes of the document.
Put it straight, I want to insert an element at a particular position. The position is based on the results I get using xPath.
|
-1

Well, text() should do. http://www.w3schools.com/xpath/xpath_examples.asp

Have you tried it already? Also, check the path, it could be

//jdbc-resource[@pool-name='TimerPool']/text()='text2'

or

/domain/resource/jdbc-resource[@pool-name='TimerPool']/text()='text2'

or

//resource/jdbc-resource[@pool-name='TimerPool']/text()='text2'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.