为什么 XPath 中的索引从 1 而不是 0 开始? [英] Why do indexes in XPath start with 1 and not 0?

查看:61
本文介绍了为什么 XPath 中的索引从 1 而不是 0 开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和一些同事比较了我们过去使用的编程语言,并讨论了我们使用 VBScript 及其奇数 功能(例如基于 1 的索引)的体验 而不是像几乎所有其他语言那样基于 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 的方法的理由,包括一些来自迈克尔·凯本人:

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:

  • ...使用一维数组访问表达式访问多维数组时,从零开始的索引往往会使索引公式更简单"
  • 在处理表格或字符串下标时,从零开始的寻址通常会更方便"
  • ...硬件寻址并不是基于 0 寻址的唯一好处...它还使计算更容易..."

但随后引用迈克尔·凯作为结论:

but then Michael Kay is quoted as concluding:

...基于 1 的逻辑是 XPath 和 XSLT 的正确选择...因为该语言是设计的为用户,而不是为程序员,以及用户仍然有这个老式的习惯参考第一个一本书中的一章作为第一章...

...1-based logic was the right choice for XPath and XSLT...because the language was designed for users, not for programmers, and users still have this old-fashioned habit of referring to the first chapter in a book as Chapter One...

谁能给我解释一下?(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 索引表示位置和计数,因此从逻辑上讲它们从 1 开始(因此 0 代表空")

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 开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆