[英]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.