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

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

问题描述

我和一些同事正在比较我们编程过去的语言,并讨论了我们使用 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和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索引代表位置和计数,因此逻辑上它们从一开始(零因此代表空)

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天全站免登陆