yaronn / xpath.js

An xpath module for node, written in pure javascript
103 stars 100 forks source link

How am I supposed to extract CDATA? #12

Closed mitchellmorris closed 10 years ago

mitchellmorris commented 10 years ago

Wondering best way to extract CDATA?

<resume>
    <objective visibility="homepage">
        <![CDATA[
            <p>Help please!</p>
        ]]>
    </objective>
</resume>

This:

var node = select(res.xmldoc, "//resume/objective[1]/text()");
console.log(node[0].data);

Returns:

'\r\n        '

Any ideas how I can get:

<p>Help please!</p>

Thanks so much for the great work!

yaronn commented 10 years ago

Hi @mitchellmorris

Try this:

//resume/objective[1]/text()[2]

The objective element has 3 text childs (first and last are whitespace) and you need the second one. If you were to delete the white space then your query would also get it.

mitchellmorris commented 10 years ago

Indeed it worked! Thanks for your help!