Search code examples
javaxmlxpathsaxxpath-2.0

Xpath: Select text and subchild node


Having an XML

<node>Text1<subnode/>text2</node>

How do I select whole substring inside <node> tag?

Something like this:

Text1<subnode/>text2

Writing xpath as: ./node/text() returns null.


Solution

  • ./node/node() returns a sequence of three nodes:

    • A text node whose string value is "Text1"

    • An element node whose name is "subnode"

    • A text node whose string value is "text2"

    If you want the string "Text1<subnode/>text2" then that involves serializing the element node. XPath cannot see the original lexical XML, only a tree of nodes, so if you want lexical XML then you have to reconstruct it: this process is called serialization. To do serialization from within XPath you will need XPath 3.0, which has a serialize() function that converts a node tree into a string.