You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Gadbury <ga...@googlemail.com> on 2010/03/16 11:34:40 UTC

XPath query with path constraint and same name siblings

Hi,

Consider the following example repository structure:

[URL=http://img192.imageshack.us/i/repostructureexample.png/][IMG]http://img192.imageshack.us/img192/3504/repostructureexample.png[/IMG][/URL]

...and the following XPATH query:

QueryManager queryManager = jcrSession.getWorkspace().getQueryManager();
String queryString = "/jcr:root/root/providers/provider//element(*,
atl:account)";
Query query = queryManager.createQuery(queryString, Query.XPATH);
QueryResult queryResult = query.execute();

The idea is to retrieve all account node types under a given provider node;
the first provider in this case, ignoring the other two providers accounts. 
The trouble is, my query is returning all accounts for all providers.

Is this a problem due to same name sibling nodes?  Should the path to the
first provider be provider[1]?  I have tried different varieties of path
constraint syntax (for exact, child nodes, descendants...) with no success.
-- 
View this message in context: http://n4.nabble.com/XPath-query-with-path-constraint-and-same-name-siblings-tp1594661p1594661.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: XPath query with path constraint and same name siblings

Posted by Alexander Klimetschek <ak...@day.com>.
On Tue, Mar 16, 2010 at 12:56, Gadbury <ga...@googlemail.com> wrote:
> I have put in a piece of code that appends "[1]" to the path constraint if
> pathNode.getIndex() == 1, but this seems nasty.  Shouldn't getPath() for the
> first provider return ../provider[1] and not ../provider ? Is this a bug?

No. AFAIK the spec simply does not define a specific behavior and
Jackrabbit is optimized for the non-SNS case. Appending [1] in the
case of SNS would require to look for existing SNS nodes, which can be
quite costly, especially for an often-used method like getPath().

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: XPath query with path constraint and same name siblings

Posted by Gadbury <ga...@googlemail.com>.
Thanks for your response, Alex.  I should hang my head in shame that I did
not think of that.  I guess my mind automatically excluded a more manual
process.  Thanks again for your help.

Kind regards,

James

On 17 March 2010 10:39, Alexander Klimetschek [via Jackrabbit] <
ml-node+1596173-1115890781-35600@n4.nabble.com<ml...@n4.nabble.com>
> wrote:

> On Wed, Mar 17, 2010 at 09:52, Gadbury <[hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1596173&i=0>>
> wrote:
> > How do you mean by node graph?  Please would you be able to provide an
> > example...?  Do you mean call getNodes() on the providerNode?
>
> Yes. Navigational access in JCR is a very notable difference compared
> to RDBMS. It is seen as the major feature, whereas search can be
> considered as "optional" (yet very convenient for many use cases).
> Just like in a file system.
>
> > If so, how would I supply property or type constraints?
>
> By looking at the properties while traversing ;-)
>
> If the number of nodes is low enough (say less then a few hundred or
> one thousand) and if there is no need for a more complex, "dynamic"
> query, node traversal will be better, ie. more performant and
> scalable. Especially if your content model is optimized for that (main
> & read) use case.
>
> Regards,
> Alex
>
> --
> Alexander Klimetschek
> [hidden email]<http://n4.nabble.com/user/SendEmail.jtp?type=node&node=1596173&i=1>
>
>
> ------------------------------
>  View message @
> http://n4.nabble.com/XPath-query-with-path-constraint-and-same-name-siblings-tp1594661p1596173.html
> To unsubscribe from Re: XPath query with path constraint and same name
> siblings, click here< (link removed) =>.
>
>
>

-- 
View this message in context: http://n4.nabble.com/XPath-query-with-path-constraint-and-same-name-siblings-tp1594661p1596179.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: XPath query with path constraint and same name siblings

Posted by Alexander Klimetschek <ak...@day.com>.
On Wed, Mar 17, 2010 at 09:52, Gadbury <ga...@googlemail.com> wrote:
> How do you mean by node graph?  Please would you be able to provide an
> example...?  Do you mean call getNodes() on the providerNode?

Yes. Navigational access in JCR is a very notable difference compared
to RDBMS. It is seen as the major feature, whereas search can be
considered as "optional" (yet very convenient for many use cases).
Just like in a file system.

> If so, how would I supply property or type constraints?

By looking at the properties while traversing ;-)

If the number of nodes is low enough (say less then a few hundred or
one thousand) and if there is no need for a more complex, "dynamic"
query, node traversal will be better, ie. more performant and
scalable. Especially if your content model is optimized for that (main
& read) use case.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: XPath query with path constraint and same name siblings

Posted by Gadbury <ga...@googlemail.com>.
Hi Justin,

Thanks for your reply.  I wish to perform a query at a given ('provider')
node, to find all nodes of a certain type that are children of the provider. 
I had been using the provider node's path to provide the path constraint in
the query.  The problem was that the first provider node's path returns
../provider, rather than ../provider[1].

How do you mean by node graph?  Please would you be able to provide an
example...?  Do you mean call getNodes() on the providerNode?  If so, how
would I supply property or type constraints?

This is what I am currently doing:

	Node providerNode = jcrSession.getNodeByUUID(providerRepositoryUUID);
	
	String providerNodePath = providerNode.getPath();
	
	//Nasty same name siblings!  The first same name sibling i.e. provider
doesn't have the array index notation.  
	//Therefore, the path of the first provider is ../provider and not
../provider[1].  Without the array index notation, we get all providers!
	if (providerNode.getIndex() == 1)
		providerNodePath += "[1]";
				
	QueryManager queryManager = jcrSession.getWorkspace().getQueryManager();
	String queryString = "/jcr:root" + providerNodePath + "//element(*,
atl:account)";
	Query query = queryManager.createQuery(queryString, Query.XPATH);
	QueryResult queryResult = query.execute();

Thanks and kind regards,

James


-- 
View this message in context: http://n4.nabble.com/XPath-query-with-path-constraint-and-same-name-siblings-tp1594661p1596054.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: XPath query with path constraint and same name siblings

Posted by Justin Edelson <ju...@gmail.com>.
If you have the provider node, why not just follow the node graph from
there? Something just seems wrong about taking a node and using getPath() to
construct a (relatively trivial) query.

Justin

On Tue, Mar 16, 2010 at 7:56 AM, Gadbury <ga...@googlemail.com> wrote:

>
> Hi Alex.  Thanks for your reply.
>
> I first get a given provider node by node uuid.  So, it isn't always the
> first provider (but the problem lies with the returned path of the first
> provider node (or any first SNS node) ).
>
> Once I have the node I use providerNode.getPath() to help construct the
> XPath query's path constraint.  The trouble is that the path of the first
> same name sibling is ../provider and not ../provider[1].
>
> I have put in a piece of code that appends "[1]" to the path constraint if
> pathNode.getIndex() == 1, but this seems nasty.  Shouldn't getPath() for
> the
> first provider return ../provider[1] and not ../provider ? Is this a bug?
>
> Thanks again.  Kind regards,
>
> James
> --
> View this message in context:
> http://n4.nabble.com/XPath-query-with-path-constraint-and-same-name-siblings-tp1594661p1594761.html
> Sent from the Jackrabbit - Users mailing list archive at Nabble.com.
>

Re: XPath query with path constraint and same name siblings

Posted by Gadbury <ga...@googlemail.com>.
Hi Alex.  Thanks for your reply.

I first get a given provider node by node uuid.  So, it isn't always the
first provider (but the problem lies with the returned path of the first
provider node (or any first SNS node) ).

Once I have the node I use providerNode.getPath() to help construct the
XPath query's path constraint.  The trouble is that the path of the first
same name sibling is ../provider and not ../provider[1].

I have put in a piece of code that appends "[1]" to the path constraint if
pathNode.getIndex() == 1, but this seems nasty.  Shouldn't getPath() for the
first provider return ../provider[1] and not ../provider ? Is this a bug?

Thanks again.  Kind regards,

James
-- 
View this message in context: http://n4.nabble.com/XPath-query-with-path-constraint-and-same-name-siblings-tp1594661p1594761.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: XPath query with path constraint and same name siblings

Posted by Alexander Klimetschek <ak...@day.com>.
On Tue, Mar 16, 2010 at 11:34, Gadbury <ga...@googlemail.com> wrote:
> "/jcr:root/root/providers/provider//element(*, atl:account)"
>
> The idea is to retrieve all account node types under a given provider node;
> the first provider in this case, ignoring the other two providers accounts.
> The trouble is, my query is returning all accounts for all providers.
>
> Is this a problem due to same name sibling nodes?

Not a a problem, that's how XML and Xpath work ;-) In the document xml
view (which is what is used for Xpath queries), all your provider
nodes have the same name "provider", hence
/jcr:root/root/providers/provider matches multiple elements.

>  Should the path to the first provider be provider[1]?

Yes, see http://www.day.com/specs/jcr/1.0/6.6.4.1_Same-Name_Siblings.html

However, with same-name siblings you will see this problem in various
places, so I strongly recommend to change your content model and give
those SNS a distinctive name (eg. name of the provider, date, or even
some id if it is common). How do you know the first provider is the
right one anyway?

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com