简体   繁体   English

XPath选择在Microsoft Edge中不起作用(适用于Chrome和Firefox)

[英]XPath selection not working in Microsoft Edge (works in Chrome and Firefox)

When trying to select a specific row or set of rows from an XML Document Microsoft Edge always returns the first row. 尝试从XML文档中选择特定行或一组行时,Microsoft Edge始终返回第一行。 The other browser handle the selection just fine. 另一个浏览器处理选择就好了。

I've made a small demonstration using the exact same code and XML as on the website in question. 我使用与相关网站上完全相同的代码和XML进行了一次小型演示。

https://jsbin.com/wufoyisudi/edit?html,output https://jsbin.com/wufoyisudi/edit?html,output

when entering 'aar' in the textbox en pressing submit the first PortERPID of the first row is returnerd. 当在文本框中输入'aar'时按下提交,第一行的第一个PortERPID将被返回。 However when entering 'abi' in the textbox the PortERPID of the second row should be returned. 但是,当在文本框中输入'abi'时,应返回第二行的PortERPID。 This works fine in Chrome and Firefox but doesn't work in Microsoft edge. 这在Chrome和Firefox中运行良好,但在Microsoft边缘不起作用。

XML: XML:

<ROOT>
<FAKE>
    <row PortERPID="DKAAR" PortName="AARHUS"/>
    <row PortERPID="CIABJ" PortName="ABIDJAN"/>
</FAKE>
</ROOT>

JavaScript: JavaScript的:

var val = document.getElementById("filter").value;
var xml = '<ROOT><FAKE>' + 
          '<row PortERPID="DKAAR" PortName="AARHUS"/>' + 
          '<row PortERPID="CIABJ" PortName="ABIDJAN"/>' + 
          '</FAKE></ROOT>';

var parser=new DOMParser();
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async="false";               
xmlDoc = parser.parseFromString(xml,"text/xml");

var portsERPIDXpath = "//row[@PortName[contains(translate(.,'abcdefghijklmnopqrstuvwxyz'," + " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'" + val.toUpperCase() + "')]]/@PortERPID";
var ERPIDS = xmlDoc.evaluate(portsERPIDXpath, xmlDoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var ERPID = ERPIDS.snapshotItem(0).value;
document.getElementById("result").innerHTML = ERPID;

Keep in mind that this is old code that's going to get replaced by a new system so completely rewriting is not an option. 请记住,这是旧代码,它将被新系统取代,因此完全重写不是一种选择。

Any ideas on a possible fix/workaround? 有关可能的修复/解决方法的任何想法?

EDIT: 编辑:

I was able to fix the problem by rewriting the XPATH expression to 我能够通过重写XPATH表达式来解决问题

//row[contains(translate(@PortName,'abcdefghijklmnopqrstuvwxyz'," + " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'" + val.toUpperCase() + "')]/@PortERPID

I have no idea why this works as it is basically the same expression but it works. 我不知道为什么这个工作,因为它基本上是相同的表达,但它的工作原理。

Perhaps querySelector has better support: 也许querySelector有更好的支持:

      var el = xmlDoc.querySelector('row[PortName*="' + value.toUpperCase() + '"]');
      if (el != null) {
        var ERPID = el.getAttribute('PortERPID');
        document.getElementById("result").innerHTML = ERPID;
      }

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

相关问题 JavaScript 在 Firefox、Chrome 中不起作用 - 在 IE、Edge 中有效 - JavaScript not working in Firefox, Chrome - works in IE, Edge Access-Control-Allow-Methods和Microsoft Edge,可与Firefox和Chrome一起使用 - Access-Control-Allow-Methods and Microsoft Edge, works with Firefox and Chrome Web通知无法在chrome上运行,但在Firefox和Microsoft Edge上可以正常运行 - Web notifications is not working on chrome, but it's working fine on firefox and microsoft edge jquery 适用于 Firefox、Edge,但不适用于 Chrome - jquery works in Firefox, Edge, but not Chrome ScrollTop可在Chrome和Edge中使用,但不能在Firefox中使用 - ScrollTop works in Chrome and Edge but not Firefox 登录可以在Chrome和Firefox上使用,但不能在Edge上使用 - Login works on Chrome and Firefox but not on Edge DirectLineJS在FireFox中不起作用,在Edge和Chrome中工作 - DirectLineJS isn't working in FireFox, works in Edge and Chrome Element.scrollIntoView(); 不在 Chrome 和 Edge 中工作,在 Firefox 中工作? - Element.scrollIntoView(); Not working in Chrome and Edge, Works in Firefox? Rangy 选择在 Microsoft Edge 上不起作用 - Rangy selection not working on Microsoft Edge Websocket无法在Chrome / Firefox上发送/接收消息,在Microsoft Edge上运行正常 - Websocket can't send/receive messages on Chrome/Firefox, works fine on Microsoft Edge
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM