XQuery:// vs descendant-or-self :: node()

War*_*kst 5 html xml xpath xquery descendant-or-self

最近我需要在HTML文档的节点上评估一个XQuery.基本上,我需要从body元素的第一个子元素中选择具有href属性的所有元素.我添加了一个小例子来解释:

<html>
    <body>
        <a href="http://www.google.be"/>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,所需的提取结果显然是:

<a href="http://www.google.be"/>
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是使用//body/*[1]//*[@href]因为:

  • //body 匹配body元素,无论它在哪里
  • /*[1] 匹配body元素的第一个子元素
  • //*[@href] 匹配当前元素的所有后代或self

我认为这可行,但在提供的示例中,XQuery没有给出任何结果.

但是,我读了一下,发现了以下内容(来源:http://www.keller.com/xslt/8/):

Alternate notation for "//": descendant-or-self::node()
Run Code Online (Sandbox Code Playgroud)

所以我改变了我的XQuery //body/*[1]/descendant-or-self::node()[@href],这次,结果是正确的.

我的问题://和descendant-or-self :: node()之间有什么区别?我在这里找到的(在xpath中//节点和/ descendant :: node之间有什么区别?)和这里(http://www.w3.org/TR/xpath/#axes)说:

//是的缩写/descendant-or-self::node()/.例如,//para简称/descendant-or-self::node()/child::para.

这导致我得出结论//并且/descendant-or-self::node()不可互换(可能是因为最后终止/?),但是有人可以告诉我是否有速记/descendant-or-self::node()

Jen*_*rat 5

你的第一个XPath表达式(//body/*[1]//*[@href])实际上代表你在自然语言中描述的内容://body/*[1]是body元素的第一个子元素,并//*[@href]选择具有@href属性的第一个元素(下面).

在您的示例中,锚标记下方没有具有此类属性的元素.例如,此查询将匹配

<html>
    <body>
        <p>
            <a href="http://www.google.be"/>
        </p>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

此查询的非缩写版本是:

//body/*[1]/descendant-or-self::node()/*[@href]
Run Code Online (Sandbox Code Playgroud)

相比之下,问题应该很容易看出:

//body/*[1]/descendant-or-self::node()[@href]
Run Code Online (Sandbox Code Playgroud)