问题描述
我和一些同事正在比较我们编程过去的语言,并讨论了我们使用 VBScript 的经验以及奇数等功能,例如 1-based index 而不是几乎所有其他语言都有基于0的索引,其原因是它是用户的语言(例如Excel VBA),而不是开发人员的语言。
Some colleagues and I were comparing past languages we had programmed in and were talking about our experience with VBScript with its odd features such as 1-based index instead of 0-based indexes like almost every other language has, the reasoning being that it was a language for users (e.g. Excel VBA) instead of a language for developers.
然后有人说, XPath也有基于1的索引这是我无法相信的,直到我找到 其中有很多理由支持0-based方法包括Michael Kay本人的一些人:
Then someone said, "XPath also has 1-based indexes" which I couldn't believe until I found this article in which many reasons are given in favor of the 0-based approach including some from Michael Kay himself:
- ...从零开始的索引倾向于使索引当访问具有一维数组访问表达式
- 的多维数组时,公式更简单dling表,或者订阅字符串,基于零的寻址通常会更方便
- ...硬件寻址不是0的唯一好处基于寻址...它也使计算更容易......
- "...zero-based indexing tends to make the index formulae simpler when accessing a multi-dimensional array with a one-dimensional array access expression"
- "when handling tables, or subscripting into strings, zero-based addressing would often be much more convenient"
- "...hardware addressing is not the only benefit of 0-based addressing ... it also makes computations easier..."
但是Michael Kay被引用为结论:
but then Michael Kay is quoted as concluding:
有人可以向我解释一下吗? (1)XPath是如何为用户设计的?我无法想象任何不是开发人员与XPath的语法刚性或XSLT的声明/功能编程方面争论的人。和(2)为什么XPath的创建者通过选择基于1的索引来反对现代编程语言的规范?
Can someone explain that to me? (1) How is XPath designed for users? I can't imagine anyone who is not a developer wrangling with the syntactical rigidity of XPath or the declarative/functional-programming-aspects of XSLT. and (2) Why really did the creators of XPath go against the norm of modern programming languages by choosing a 1-based index?
推荐答案
数组和其他集合索引表示内存偏移,因此逻辑上它们从零开始。 XML和XPATH索引代表位置和计数,因此逻辑上它们从一开始(零因此代表空)
Array and other collection indexes represent memory offsets, so logically enough they begin at zero. XML and XPATH indexes represent positions and counts, so logically enough they begin at one (and zero is therefore representative of "empty")
这篇关于为什么XPath中的索引从1开始而不是0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!