yaronn / xpath.js

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

textContent or innerText #4

Open sradu opened 10 years ago

sradu commented 10 years ago

Currently I get a result like < span >my_div< /span >. Would it be possible to run .innerText or .textContent on the object so I get only the text?

yaronn commented 10 years ago

hi

can you provide a full sample of xml and xpath you run? You could try xpath with text():

var xml = "<book><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml)    
var title = select(doc, "//title/text()")[0].data   
console.log(title)
sradu commented 10 years ago

I have some precreated xpaths to elements. Would have been great to use different funcs (innerhtml, textcontent).

R.

On Tuesday, December 24, 2013, Yaron Naveh wrote:

hi

can you provide a full sample of xml and xpath you run? You could try xpath with text():

var xml = "Harry Potter" var doc = new dom().parseFromString(xml) var title = select(doc, "//title/text()")[0].data console.log(title)

— Reply to this email directly or view it on GitHubhttps://github.com/yaronn/xpath.js/issues/4#issuecomment-31176441 .

yaronn commented 10 years ago

Since I only wrapped xpath.js as a node module I need to defer to the original author. @heycam - what does it take to achieve this?

heycam commented 10 years ago

If you have some existing XPath expressions that you are happy to change, but you don't want to change the code around them too much, you could wrap them with a function call for a custom function that would get .textContent from the result. There's no easy way though to change the evaluate function to do this automatically though.

JLRishe commented 9 years ago

@sradu The SelectNodes wrapper function returns an array of DOMNodes. If you want to access their .innerText or .textContent property, you can just go ahead and do so:

var select = require('xpath.js'), 
    dom = require('xmldom').DOMParser 

var xml = "<book><title>Harry Potter</title></book>" ;
var doc = new dom().parseFromString(xml);
var nodes = select(doc, "//title") ;
console.log(nodes[0].textContent);