为什么XPath中的索引从1开始而不是0?

编程入门 行业动态 更新时间:2024-10-22 11:09:06
本文介绍了为什么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?

本文发布于:2023-11-29 16:44:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   索引   XPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!