AuthorDocumentController findNodesByXpath not returning expected values

Having trouble installing Oxygen? Got a bug to report? Post it all here.
neon096
Posts: 45
Joined: Wed May 23, 2012 11:20 am

AuthorDocumentController findNodesByXpath not returning expected values

Post by neon096 »

I'm writing some code to find all <para/> nodes within a <paragraph/> node. However I'm retrieving all para nodes within the entire document.

I'm using the following code

Code: Select all


final AuthorNode node = access.getDocumentController().getNodeAtOffset(offset);
final AuthorNode paragraphNode = DocumentUtils.getParagraphNode(node);
final AuthorNode[] paraNodes = access.getDocumentController().findNodesByXPath("//para", paragraphNode, true, true, true, true);
However using the the console output I'm getting the following. The paragraph I'm using for the relative path is

Code: Select all


<paragraph> (23, 41)
However I'm getting the following para nodes returned.

Code: Select all

[<para> (24, 40), <para> (45, 61), <para> (66, 88), <para> (89, 111), <para> (116, 138), <para> (139, 161), <para> (166, 188), <para> (189, 211), <para> (216, 238), <para> (239, 261), <para> (266, 288), <para> (289, 311), <para> (316, 332), <para> (345, 361), <para> (406, 422), <para> (446, 462), <para> (495, 511), <para> (528, 544), <para> (571, 587), <para> (592, 608)]
I only expected "<para> (24, 40)" to be returned.
Radu
Posts: 9449
Joined: Fri Jul 09, 2004 5:18 pm

Re: AuthorDocumentController findNodesByXpath not returning expected values

Post by Radu »

Hi Neil,

This is how XPath expressions work, even when you interpret it relative to a node, if you use a // construct then the expression is interpreted as absolute.

For your case you should have simply used the expression para which would have returned all para's from that element.

You can test XPath expressions using our XPath toolbar.
If you place the caret in the Author document, the XPath expression is interpreted relative to the node which contains the caret offset.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
neon096
Posts: 45
Joined: Wed May 23, 2012 11:20 am

Re: AuthorDocumentController findNodesByXpath not returning expected values

Post by neon096 »

Thanks Radu,

I must have mis-nderstood the API documentation.
Radu
Posts: 9449
Joined: Fri Jul 09, 2004 5:18 pm

Re: AuthorDocumentController findNodesByXpath not returning expected values

Post by Radu »

Hi Neil,

The Javadoc says something like:

Code: Select all

contextNode The node in the context of which the relative XPath Expressions will computed. 
but XPath expressions starting with "//" are not relative.

The XPath specification:

http://www.w3.org/TR/xpath/

gives some examples like:
//para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
.//para selects the para element descendants of the context node
So in your case you could have also used .//para and obtain the correct result.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply