Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the following sibling in an XPath when "following" axis is not supported

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()

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

And the same for:

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

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

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!

like image 246
TimeHorse Avatar asked Jul 13 '10 15:07

TimeHorse


People also ask

When to use following-sibling in XPath?

We can use the concept of following-sibling in xpath for identifying elements in Selenium. It identifies the siblings of the context node. The siblings should be located at the equal level of the existing node and should have the same parent.

How can I get next sibling in XPath?

To traverse to the next sibling, we have to use the following-sibling concept in xpath. This will allow us to traverse to the next sibling from the present sibling of the same parent. Let us try to move from the first child<h1> of parent <div> to the second <h2> as in the above image.

What is sibling XPath?

Xpath Sibling is defined as a is a function for retrieving a web element that is a child of the parent element. The web element may be readily recognised or accessed using the sibling attribute of the Xpath expression if the parent element is known.


1 Answers

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

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.

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]
like image 61
Gunther Avatar answered Nov 15 '22 00:11

Gunther