typescriptfirefoxxpathevaluateresolver

Firefox, xPath is not evaluating


I have a problem with document.evaluate function to check xPath validation. In Firefox, document.createNSResolver is not working properly... I get just xmlDoc nothing else. When I just leave it with a null value in an evaluate it still does not work. Edge, Opera, Chrome with all these browsers I have no problem, and everything working smoothly. Does somebody know where is the problem in FireFox? What should I change? I've been seeking a solution and I couldn't find...

My main function looks like:

  const checkXPathVisiblity = (xPathValid?: string) => {
if (!xPathValid) return false;

try {
  const parsedXPathValid = _.unescape(xPathValid);
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xml, 'text/xml');
  const resolver = document.createNSResolver(xmlDoc);
  const result = document.evaluate(parsedXPathValid, xmlDoc, resolver);

  return !!result.booleanValue;
} catch (error) {
  return false;
}
  };

Where the:

xPathValid:

 number(Dokument/F0002x2) = 1

xml:

<?xml version="1.0" encoding="utf-8"?>
<Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<F0001x1 id="F0001x1"></F0001x1>
<F0002x2 id="F0002x2">1</F0002x2>
</Dokument>

Solution

  • When I try plain JavaScript I don't have any problems with Firefox, see https://jsfiddle.net/2rwszcyq/ (which doesn't use a resolver as the XPath doesn't use any namespaces):

    const xmlSource = `<?xml version="1.0" encoding="utf-8"?>
    <Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <F0001x1 id="F0001x1"></F0001x1>
    <F0002x2 id="F0002x2">1</F0002x2>
    </Dokument>`;
    
    const xmlDoc = new DOMParser().parseFromString(xmlSource, 'application/xml');
    
    const xpathResult = xmlDoc.evaluate('number(Dokument/F0002x2) = 1', xmlDoc);
    
    console.log(xpathResult.booleanValue);

    const xmlSource = `<?xml version="1.0" encoding="utf-8"?>
    <Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <F0001x1 id="F0001x1"></F0001x1>
    <F0002x2 id="F0002x2">1</F0002x2>
    </Dokument>`;
    
    const xmlDoc = new DOMParser().parseFromString(xmlSource, 'application/xml');
    
    const xpathResult = xmlDoc.evaluate('number(Dokument/F0002x2) = 1', xmlDoc);
    
    console.log(xpathResult.booleanValue);
    

    or (which creates a resolver) https://jsfiddle.net/2rwszcyq/1/:

    const xmlSource = `<?xml version="1.0" encoding="utf-8"?>
    <Dokument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <F0001x1 id="F0001x1"></F0001x1>
    <F0002x2 id="F0002x2">1</F0002x2>
    </Dokument>`;
    
    const xmlDoc = new DOMParser().parseFromString(xmlSource, 'application/xml');
    
    const xpathResult = xmlDoc.evaluate('number(Dokument/F0002x2) = 1', xmlDoc, xmlDoc.createNSResolver(xmlDoc));
    
    console.log(xpathResult.booleanValue);