在“跟随"时在 XPath 中获取以下同级不支持轴 [英] Getting the following sibling in an XPath when "following" axis is not supported

查看:22
本文介绍了在“跟随"时在 XPath 中获取以下同级不支持轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 MS SQL Server 上的 XQuery 获取包含特定文本元素的段落后面的表格的文本元素.问题是,每当我使用轴跟随"、跟随兄弟"或上一个兄弟"时,我都会收到一条错误消息,指出 SQL Server 不支持此功能(我使用的是 2008 版).因此,例如,我可以获得包含值为blah"的文本节点的第一个段落节点:

I am trying to get the text elements of a table that follows a paragraph that contains a specific text element using XQuery on MS SQL Server. The problem is whenever I use the axes "following", "following-sibling" or "previous-sibling", I get an error saying this is not supported in SQL Server (I'm using version 2008). So for instance, I can get the first paragraph node that contains a text node whose value is "blah":

//w:p[descendant::w:t = "blah"]

我可以使用以下方法从表格元素中获取文本:

And I can get the text from a table element using:

//w:tbl//w:t/text()

我看不出有任何方法可以强制查询只返回先前捕获的段落节点之后的第一个表元素,因为:

I don't see any way I can force the query to only return the first table element that follows the previously captured paragraph node since:

//w:tbl[following:://w:p//w:t = "blah"]//w:t/text()

给出错误:XQuery [Specification.document.query()]:不支持 XQuery 语法‘以下’."

Gives the error: "XQuery [Specification.document.query()]: The XQuery syntax 'following' is not supported."

同样适用于:

//w:tbl[following-sibling::w:p[descendant::w:t = "blah"]]//w:t/text()

给出XQuery [Specification.document.query()]:不支持 XQuery 语法‘following-sibling’."

Gives "XQuery [Specification.document.query()]: The XQuery syntax 'following-sibling' is not supported."

这不对,你们都知道!自 1999 年 AFAICT 的 1.0 以来,XPath 一直支持跟随和跟随兄弟,因此 MS SQL Server 似乎在标准合规性方面存在严重缺陷,但无论哪种方式,有没有人看到我可以在没有这些轴的情况下做到这一点的方法?提前致谢!

That ain't right, y'all know! XPath has supported following and following-sibling since 1.0 back in 1999 AFAICT so MS SQL Server seems to be severely deficient in terms of standard compliance but either way, does anyone see a way I can do this without those axes? Thanks in advance!

推荐答案

XQuery 中节点的文档顺序也可以使用节点比较运算符进行评估.

Document order of nodes in XQuery can also be evaluated by using node comparison operators.

Operator >> 适用于两个节点,如果左侧节点按照文档顺序在右侧节点之后,则返回 true.为了解决您的问题,您需要选择第一个这样的节点.

Operator >> applies to two nodes and returns true if the left hand side node follows the right hand side node in document order. For solving your problem, you'd select the first such node.

在下面的代码中,$blah$text 是给定的表达式.返回的值是 $text 中紧跟在 $blah 中的第一个节点之后的第一个节点.

In the following code, $blah and $text are the given expressions. The returned value is the first node in $text that follows the first node in $blah.

let $blah := //w:p[descendant::w:t = "blah"]
let $text := //w:tbl//w:t/text()
return $text[. >> $blah[1]][1]

或者,组合成一个表达式,

Or, combined into a single expression,

(//w:tbl//w:t/text()[. >> (//w:p[descendant::w:t = "blah"])[1]])[1]

这篇关于在“跟随"时在 XPath 中获取以下同级不支持轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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