2

I'm facing a problem with a very simple XPath but can't figure why it doesn't work.

When I search for an element with a namespace it works like a charm but for the "default" namespace (don't know the exact term).

 var xmlString = `<?xml version="1.0" encoding="utf-8"?>
                  <h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml">
                    <h:ok>OK</h:ok>
                    <ko>KO</ko>
                  </h:html>`;


var doc = new DOMParser().parseFromString(xmlString, 'text/xml');
var rootNode = doc.documentElement;
var evaluator = new XPathEvaluator();
var resolver = evaluator.createNSResolver(rootNode);

var result1 = doc.evaluate('//h:ok', rootNode, resolver, XPathResult.ANY_TYPE, null);
var result1Next = result1.iterateNext();
alert(result1Next ? result1Next.innerHTML : 'not found'); // Works !

var result2 = doc.evaluate('//ko', rootNode, resolver, XPathResult.ANY_TYPE, null);
var result2Next = result2.iterateNext();
alert(result2Next ? result2Next.innerHTML : 'not found'); // Fail !

I made a fiddle (http://jsfiddle.net/1bon769r/).

Thanks in advance for any help.

hapouu
  • 21
  • 1

1 Answers1

0

Finally I found a workaround which fits my use case.

I use the 'local-name()' to find an element without any namespace criteria.

Thanks to #19438596

hapouu
  • 21
  • 1