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.

Share:
33,535
Mike Q
Author by

Mike Q

Updated on August 26, 2020

Comments

  • Mike Q
    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
    Mike Q about 14 years
    Thanks but I just want an XPath expression. I'm not using XSLT, just dom4j with jaxen inside Java code.
  • Mike Q
    Mike Q about 14 years
    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.
  • reinierpost
    reinierpost almost 2 years
    How 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
    reinierpost almost 2 years
    Not for me. I don't know what you're using, but data is not a standatd XPath function.