S
S
Svyatoslav Khusamov2019-10-06 16:00:46
JavaScript
Svyatoslav Khusamov, 2019-10-06 16:00:46

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

namespaceResolver tried to substitute, but does not help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Svyatoslav Khusamov, 2019-10-06
@khusamov

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 question

Ask a Question

731 491 924 answers to any question