Answer the question
In order to leave comments, you need to log in
Why doesn't DOMParser.evaluate return nodes if xmlns is in the document?
This code returns zero nodes.
If you remove xmlns=" www.w3.org/2000/svg ", then everything works as it should.
const parser = new DOMParser();
const xmlSource = '<svg xmlns="http://www.w3.org/2000/svg"></svg>';
const svgDocument2 = parser.parseFromString(xmlSource, 'application/xml');
console.log(
'count(//svg)',
svgDocument.evaluate('count(//svg)', svgDocument2, null, XPathResult.NUMBER_TYPE).numberValue
);
Answer the question
In order to leave comments, you need to log in
It turns out that it is necessary to use all namespaces in XPath.
And of course add namespaceResolver.
namespaceResolver:
const namespaceMap = {
svg: 'http://www.w3.org/2000/svg',
xlink: 'http://www.w3.org/1999/xlink'
};
const namespaceResolver = prefix => namespaceMap[prefix || ''] || null;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question