views:

233

answers:

1

Hi all,

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.

+6  A: 

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()

Andrew Hare
Thanks but I just want an XPath expression. I'm not using XSLT, just dom4j with jaxen inside Java code.
Mike Q
@Mike Q: Ah I see, I have edited my answer to reflect that.
Andrew Hare
Ah, it turns out in dom4j that text() returns a DefaultText element. However I found a function in dom4j called valueOf(XPath xpath) that does exactly what your first example does (String representation of XPath result) so that worked nicely. Thanks again.
Mike Q