简体   繁体   English

XPath选择属性在Chrome上失败

[英]XPath selecting attribute fails on Chrome

Here's a small test I ran with expect.js : 这是我与expect.js运行的一个小测试:

describe('xpath', function () {
    it('finds attribute nodes', function () {
        $(document.body).append('<string fooBar="bar"><data id="xyz"></data></string>');
        var xyz = document.getElementById('xyz');
        expect(xyz).not.to.be(null);

        var nodes = document.evaluate('../@fooBar', xyz, null, XPathResult.ANY_TYPE, null);
        var element = nodes.iterateNext();
        expect(element.nodeValue).to.equal('bar');
    });
});

I'm testing it on Chrome and Firefox with Testacular, and while it passes on Firefox, it fails on Chrome with an error saying that element is null . 我正在使用Testacular在Chrome和Firefox上对其进行测试,尽管它在Firefox上通过了测试,但在Chrome上却失败了,并显示一个错误,指出那个elementnull The really crazy part to me is that if I change fooBar to just foo , it passes on both systems. 对我来说真正疯狂的部分是,如果我将fooBar更改为foo ,它将在两个系统上传递。 Chrome also breaks if I use Foo instead of foo . 如果我使用Foo而不是foo Chrome也会崩溃。 In other words, XPath querying on Chrome seems to break when the sought-for attribute has a capital letter in it. 换句话说,Chrome中的XPath查询似乎在搜寻属性中包含大写字母时中断。 Is this normal behavior? 这是正常行为吗? Am I missing something? 我想念什么吗? Or is this a bug in Chrome? 还是这是Chrome中的错误?

UPDATE 1 更新1

I added in Google's Wicked Good XPath, and now Internet Explorer works, too, but Chrome still fails. 我添加了Google的Wicked Good XPath,现在Internet Explorer也可以使用,但是Chrome仍然无法运行。

UPDATE 2 更新2

The XPath query works on Chrome if the actual attribute has caps, but the XPath value is all lowercase. 如果实际属性具有上限,则XPath查询可在Chrome上运行,但XPath值均为小写。

Are you using XHTML? 您在使用XHTML吗? If so, the XHTML specification dictates that all element and attribute names must be in lower case . 如果是这样,则XHTML规范规定所有元素和属性名称都必须小写

You may find that Chrome is only geared up for evaluating XPaths on that basis, whereas other browsers are more compliant with XML (where any case is valid). 您可能会发现Chrome仅适合以此为基础评估XPath,而其他浏览器更符合XML(在任何情况下均有效)。

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

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