In XPath how to select the element content
33,535
Solution 1
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()
Solution 2
A bit too late but...
data(Element)
...should also be fine.
Author by
Mike Q
Updated on August 26, 2020Comments
-
Mike Q over 3 years
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. -
Mike Q about 14 yearsThanks but I just want an XPath expression. I'm not using XSLT, just dom4j with jaxen inside Java code.
-
Mike Q about 14 yearsAh, 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.
-
reinierpost almost 2 yearsHow can this answer be the accepted and most upvoted one when it doesn't use XPath to extract the content? it answers a different question.
-
reinierpost almost 2 yearsNot for me. I don't know what you're using, but
data
is not a standatd XPath function.