Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not work with mime type text/html #56

Closed
TRPB opened this issue Sep 1, 2016 · 1 comment
Closed

does not work with mime type text/html #56

TRPB opened this issue Sep 1, 2016 · 1 comment

Comments

@TRPB
Copy link

TRPB commented Sep 1, 2016

a small adjustment to the sample code stops it working:

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

var xml = "<book><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(xml, 'text/html');
var nodes = xpath.select("//title", doc)

console.log(nodes);

nodes is an empty array.

I don't know whether this is an issue with xmldom or xpath but this fixes it:

var xpath = require('xpath')
  , dom = require('xmldom').DOMParser
  , serializer = require('xmldom').XMLSerializer;

var xml = "<book><title>Harry Potter</title></book>"
var doc = new dom().parseFromString(new serializer().serializeToString(new dom().parseFromString(xml, 'text/html')));
var nodes = xpath.select("//title", doc)

console.log(nodes);

Obviously this is terribly inefficient but it does work.

@JLRishe
Copy link
Collaborator

JLRishe commented Sep 2, 2016

There's already an open issue #27 on this. The library works with DOMs that are parsed that way, but parsing with that mime type implicitly puts the nodes in the http://www.w3.org/1999/xhtml namespace (this is the parser's doing, not the xpath library). That's why you're not finding any matches with //title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants