In XPath how to select the element content

Mike Q picture Mike Q · Mar 18, 2010 · Viewed 30.7k times · Source

Is there a way of writing an XPath expression to select the content of the element.

e.g.

<Element>xxx</Element>

Assuming I can write XPath (/Element) to get Element how do I tweak the XPath to get xxxx returned rather than the Element wrapper?

EDIT/ANSWER

To do this in dom4j world use the Element.valueOf(String xpathExpression) rather than the .selectXXX() methods.

Answer

Andrew Hare picture Andrew Hare · Mar 18, 2010

Use the value-of element:

<xsl:value-of select="/Some/Path/To/Element"/>

If you can only specify an XPath then use the text function like this:

/Some/Path/To/Element/text()