简体   繁体   English

XQuery在谓词中使用函数

[英]XQuery using function in predicates

I am learning XQuery and am curious about functions such as last() . 我正在学习XQuery,并对诸如last()函数感到好奇。 As I understand it from the documentation , I am allowed something like: 据我从文档中了解,我被允许这样的事情:

doc('http://www.functx.com/input/catalog.xml')/catalog/product[last()]

From my understanding, this passes the sequence of product elements: 根据我的理解,这传递了产品元素的顺序:

doc('http://www.functx.com/input/catalog.xml')/catalog/product

... to last() , which then returns the last member. ...到last() ,然后返回最后一个成员。 However, last() isn't supposedly taking any arguments, so how is the data-structure being passed to the function? 但是, last()不应该采用任何参数,那么数据结构如何传递给函数?

Thanks! 谢谢!

XQuery is an extension to XPath, but the answer to this question entirely related to XPath. XQuery是XPath的扩展,但这个问题的答案完全与XPath有关。

XPath expressions have a dynamic context; XPath表达式具有动态上下文; here's the details in the XPath spec: http://www.w3.org/TR/xpath-30/#id-xp-evaluation-context-components . 这是XPath规范中的详细信息: http//www.w3.org/TR/xpath-30/#id-xp-evaluation-context-components The context item can be references as . 上下文项可以作为引用. :

fn:doc('http://www.functx.com/input/catalog.xml')/catalog/product[. eq "my product name"]

The predicate [. eq "my product name"] 谓词[. eq "my product name"] [. eq "my product name"] is re-evaluated for each product element, and . 对每个产品元素重新评估[. eq "my product name"] ,并且. is a reference to the context item, ie, the element specific to that evaluation. 是对上下文项的引用,即特定于该评估的元素。

Several XPath functions only accept the context, while others default to the context, with optional args. 几个XPath函数只接受上下文,而其他函数默认使用上下文,带有可选的args。

Here's the complete list of functions that only accept the context in XPath 3: http://www.w3.org/TR/xpath-functions-30/#context . 以下是仅接受XPath 3中上下文的完整功能列表: http//www.w3.org/TR/xpath-functions-30/#context

Some examples: 一些例子:

  • fn:last() takes zero arguments and returns the size of the context item fn:last()接受零参数并返回上下文项的大小

If there are 10 <product/> elements, that following expressions are equivalent: 如果有10个<product/>元素,则以下表达式是等效的:

fn:doc('http://www.functx.com/input/catalog.xml')/catalog/product[fn:last()]

fn:doc('http://www.functx.com/input/catalog.xml')/catalog/product[10]
  • fn:position() takes zero arguments and returns the context position fn:position()接受零参数并返回上下文位置

fn:doc('http://www.functx.com/input/catalog.xml')/catalog/product[fn:position() > 3]


There are many function that take zero-or-1 arguments, where the zero-argument form accesses the context. 有许多函数采用零或一参数,其中零参数形式访问上下文。 I see 15 of them, searching the spec for this phrase: "The zero-argument form of this function is ·deterministic·, ·context-dependent·, and ·focus-dependent·." 我看到其中的15个,在这个短语的规范中搜索: “这个函数的零参数形式是·确定性的,·依赖于上下文的,和·依赖于焦点的”。

One example: 一个例子:

  • fn:string() takes zero-or-1 arguments and returns the argument or the context item as a string fn:string()接受零或一个参数,并将参数或上下文项作为字符串返回

fn:doc('http://www.functx.com/input/catalog.xml')/catalog/product/fn:string()

returns the string value of each <product/> element. 返回每个<product/>元素的字符串值。 It's equivalent to 它相当于

fn:doc('http://www.functx.com/input/catalog.xml')/catalog/product/fn:string(.)

where the context item is explicitly passed as an argument. 将上下文项显式作为参数传递的位置。 fn:string() can also be used for type conversions: fn:string(1) returns "1" . fn:string()也可用于类型转换: fn:string(1)返回"1"

Quoting from Walmsley , p. 引自沃姆斯利 ,p。 49: 49:

The position and last functions are... useful when writing predicates based on position... [Omitted here is a discussion of position .] The last function returns the number of nodes in the current sequence. 当根据位置编写谓词时, positionlast函数是有用的... [这里省略了对position的讨论。] last函数返回当前序列中的节点数。 It takes no arguments and returns an integer representing the number of items. 它不带参数,返回表示项数的整数。 The last function is useful for testing whether an item is the last one in the sequence. last函数可用于测试项是否是序列中的最后一项。 For example, catalog/product[last()] returns the last product child of catalog. 例如, catalog/product[last()]返回catalog的最后一个product child。

So, basically, these functions operate on their context sequence, rather than on parameters/arguments passed to them. 所以,基本上,这些函数对它们的上下文序列进行操作,而不是传递给它们的参数/参数。 position returns the position of the context item within the context sequence, while last returns the number of nodes in the current sequence. position返回上下文序列中上下文项的位置,而last返回当前序列中的节点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM