How to extract text outside xml tags

I want to extract text outside of tags. For instance,

<body>
    This is an exmaple
    <p>
        blablabla
    </p>
    <references>
        refer 1
        refer 2
    </references>
</body>

I want to get the text "This is an example" only without the text in other tags (p or reference). I tried several methods but did not work. Any1 can help? Many thanks.

+5
source share
2 answers

You must present the text inside the tag, for example node. The text node is extracted using the node test text(). Example. Given:

<body>
    This is an exmaple
    <p>
    blablabla
    <\p>
    <references>
        refer 1
        refer 2
    <\references>
    another example
<\body>

XPath:

"/body/text()"

Retrieves all child text nodes body, for example, “This is an example” and “another example”, as well:

"/body/text()[1]"

, " ". , :

"/body//text()"

, p:

"/body/p[1]//text()"
+8

XPath: /body/text(). This is an exmaple.

+2

All Articles