简体   繁体   English

Firefox、xPath 未评估

[英]Firefox, xPath is not evaluating

I have a problem with document.evaluate function to check xPath validation.我对 document.evaluate function 有问题,以检查 xPath 验证。 In Firefox, document.createNSResolver is not working properly... I get just xmlDoc nothing else.在 Firefox 中,document.createNSResolver 无法正常工作......我只得到 xmlDoc 没有别的。 When I just leave it with a null value in an evaluate it still does not work.当我在评估中留下 null 值时,它仍然不起作用。 Edge, Opera, Chrome with all these browsers I have no problem, and everything working smoothly. Edge、Opera、Chrome 与所有这些浏览器我都没有问题,一切运行顺利。 Does somebody know where is the problem in FireFox?有人知道 FireFox 的问题在哪里吗? What should I change?我应该改变什么? I've been seeking a solution and I couldn't find...我一直在寻找解决方案,但我找不到...

My main function looks like:我的主要 function 看起来像:

  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: xPathValid:

 number(Dokument/F0002x2) = 1

xml: 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>

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): 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/ :或(创建解析器) 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM