XPath queries in IE use zero-based indexes but the W3C spec is one-based. How should I handle the difference?

北城余情 提交于 2019-12-05 08:20:56

we just came across the limitation that positions in IE are zero-based but in the W3C model used by the other browsers they are one-based. This means that to get the first element we need to do //books[0] in IE and //books[1] in the other browsers.

Before doing any XPath selection, specify:

xmlDoc.setProperty("SelectionLanguage", "XPath");

MSXML3 uses a dialect of XSLT/XPath that was in use before XSLT and XPath became W3C Recommendations. The default is "XSLPattern" and this is what you see as behavior.

Read more on this topic here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms754679(v=vs.85).aspx

Why not modify the original expressions, so that this:

var expr = "books[1]";

...becomes:

var expr = "books[" + index(1)  + "]";

...where index is defined as (pseudocode):

function index(i) {
    return isIE ? (i - 1) : i;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!