无法使用 Oxygen XQuery 实现选择 XML 属性;氧气 XPath 发出结果 [英] Can't select XML attributes with Oxygen XQuery implementation; Oxygen XPath emits result

查看:27
本文介绍了无法使用 Oxygen XQuery 实现选择 XML 属性;氧气 XPath 发出结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到每个 Xpath 表达式也是一个有效的 Xquery 表达式.我在此示例 XML 中使用 Oxygen 16.1:

I learned that every Xpath expression is also a valid Xquery expression. I'm using Oxygen 16.1 with this sample XML:

<actors>
    <actor filmcount="4" sex="m" id="15">Anderson, Jeff</actor>
    <actor filmcount="9" sex="m" id="38">Bishop, Kevin</actor>
</actors>

我的表达是:

//actor/@id

当我使用 Xpath 3.0 在 Oxygen 中评估这个表达式时,我得到了我所期望的:

When I evaluate this expression in Oxygen with Xpath 3.0, I get exactly what I expect:

15
38

但是,当我使用 Xquery 3.0(也是 1.0)评估这个表达式时,我收到消息:您的查询返回了一个空序列.

However, when I evaluate this expression with Xquery 3.0 (also 1.0), I get the message: "Your query returned an empty sequence.

任何人都可以提供任何有关为什么会这样的见解,以及我如何编写等效的 Xquery 语句来获得上述 Xpath 语句的作用?

Can anyone provide any insight as to why this is, and how I can write the equivalent Xquery statement to get what the Xpath statement did above?

推荐答案

其他 XQuery 实现支持这个查询

如果您想验证您的查询(根据评论中的讨论进行了更正)确实在完全按照问题中给出的输入时确实可以与其他 XQuery 实现一起使用,您可以按如下方式运行它(在 BaseX 中测试):

Other XQuery implementations do support this query

If you want to validate that your query (as corrected per discussion in comments) does in fact work with other XQuery implementations when entered exactly as given in the question, you can run it as follows (tested in BaseX):

declare context item := document { <actors>
    <actor filmcount="4" sex="m" id="15">Anderson, Jeff</actor>
    <actor filmcount="9" sex="m" id="38">Bishop, Kevin</actor>
</actors> };

//actor/@id

<小时>

Oxygen XQuery 需要一些额外的帮助

Oxygen XML 不支持序列化属性,因此会在结果序列以其他方式提供给用户时将它们从结果序列中丢弃.


Oxygen XQuery needs some extra help

Oxygen XML doesn't support serializing attributes, and consequently discards them from a result sequence when that sequence would otherwise be provided to the user.

因此,您可以使用如下查询来解决此问题:

Thus, you can work around this with a query such as the following:

  • //actor/@id/string(.)
  • data(//actor/@id)

坦率地说,我不希望 //actors/@id 使用任何有效的 XPath 或 XQuery 引擎针对该数据返回任何内容.

Frankly, I would not expect //actors/@id to return anything against that data with any valid XPath or XQuery engine, ever.

原因是你只在一个地方递归——一个//——那就是寻找actors.actor@id 之间的单个 / 意味着它们需要直接连接,但事实并非如此您在此处提供的数据中的案例 - 它们之间有一个 actor 元素.

The reason is that there's only one place you're recursing -- one // -- and that's looking for actors. The single / between the actors and the @id means that they need to be directly connected, but that's not the case in the data you give here -- there's an actor element between them.

因此,您需要修正您的查询.您可以编写许多查询来查找本文档中所需的数据——知道哪一个是合适的需要比您提供的更多信息:

Thus, you need to fix your query. There are numerous queries you could write that would find the data you wanted in this document -- knowing which one is appropriate would require more information than you've provided:

  • //actor/@id - 在任意位置查找 actor 元素,并获取它们的 id 属性值.
  • //actors/actor/@id - 在任何地方查找 actors 元素;寻找它们正下方的 actor 元素,并取此类 actor 元素的 id 属性.
  • //actors//@id - 在 actors 元素的子树中查找所有 id 属性.
  • //@id - 查找文档中任意位置的 id 属性.
  • //actor/@id - Find actor elements anywhere, and take their id attribute values.
  • //actors/actor/@id - Find actors elements anywhere; look for actor elements directly under them, and take the id attribute of such actor elements.
  • //actors//@id - Find all id attributes in subtrees of actors elements.
  • //@id - Find id attributes anywhere in the document.

...等等

这篇关于无法使用 Oxygen XQuery 实现选择 XML 属性;氧气 XPath 发出结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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