xpath.js icon indicating copy to clipboard operation
xpath.js copied to clipboard

textContent or innerText

Open sradu opened this issue 11 years ago • 5 comments

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?

sradu avatar Dec 24 '13 16:12 sradu

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)

yaronn avatar Dec 24 '13 16:12 yaronn

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 .

sradu avatar Dec 24 '13 16:12 sradu

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?

yaronn avatar Dec 24 '13 20:12 yaronn

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.

heycam avatar Dec 26 '13 04:12 heycam

@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); 

JLRishe avatar Jan 06 '15 10:01 JLRishe