Query for Nodes from a given Path

6 messages Options
Embed this post
Permalink
BOWLER Paul

Query for Nodes from a given Path

Reply Threaded More More options
Print post
Permalink
Hi,

I need to query a subtree for specific nodes, but do not know the syntax
for this.

Currently I have:

def query = qm.createQuery("//pcf:Color", Query.XPATH)
return query.execute()

This returns ALL pcf:Color nodes in the repository, but I'd like to
limit the search down a specific path (or from a specific node).

Any ideas how I could do this?

Thanks,

Paul.

Fabian Mandelbaum

Re: Query for Nodes from a given Path

Reply Threaded More More options
Print post
Permalink
BOWLER Paul wrote:

> Hi,
>
> I need to query a subtree for specific nodes, but do not know the syntax
> for this.
>
> Currently I have:
>
> def query = qm.createQuery("//pcf:Color", Query.XPATH)
> return query.execute()
>
> This returns ALL pcf:Color nodes in the repository, but I'd like to
> limit the search down a specific path (or from a specific node).
>
> Any ideas how I could do this?
>
> Thanks,
>
> Paul.
>
>
>  
Hello Paul,

you simply prepend the starting path to your path specification in the
createQuery statement, for example, if you want to query the pcf:Color
nodes below /the/starting/node, you say:

query = qm.createQuery("/the/starting/node/pcf:Color", Query.XPATH);

if this still doesn't work, you prepend /jcr:root at the beginning, like
this:

query = qm.createQuery("/jcr:root/the/starting/node/pcf:Color",
Query.XPATH);

Good luck!
Alexander Klimetschek

Re: Query for Nodes from a given Path

Reply Threaded More More options
Print post
Permalink
In reply to this post by BOWLER Paul
On Tue, Jul 7, 2009 at 12:36 PM, BOWLER
Paul<[hidden email]> wrote:

> Hi,
>
> I need to query a subtree for specific nodes, but do not know the syntax
> for this.
>
> Currently I have:
>
> def query = qm.createQuery("//pcf:Color", Query.XPATH)
> return query.execute()
>
> This returns ALL pcf:Color nodes in the repository, but I'd like to
> limit the search down a specific path (or from a specific node).
>
> Any ideas how I could do this?

Standard xpath (except for the jcr:root prefix):

/jcr:root/your/search/path//pcf:Color

Regards,
Alex

--
Alexander Klimetschek
[hidden email]
BOWLER Paul

RE: Query for Nodes from a given Path

Reply Threaded More More options
Print post
Permalink
In reply to this post by Fabian Mandelbaum
Thanks for the response.

The problem is that the pcf:Color node can appear at many levels within the tree so specifying an absolute path won't work.

Any ideas how I can get around this?

P.

-----Original Message-----
From: Fabián Mandelbaum [mailto:[hidden email]]
Sent: 07 July 2009 11:39
To: [hidden email]
Subject: Re: Query for Nodes from a given Path

BOWLER Paul wrote:

> Hi,
>
> I need to query a subtree for specific nodes, but do not know the syntax
> for this.
>
> Currently I have:
>
> def query = qm.createQuery("//pcf:Color", Query.XPATH)
> return query.execute()
>
> This returns ALL pcf:Color nodes in the repository, but I'd like to
> limit the search down a specific path (or from a specific node).
>
> Any ideas how I could do this?
>
> Thanks,
>
> Paul.
>
>
>  
Hello Paul,

you simply prepend the starting path to your path specification in the
createQuery statement, for example, if you want to query the pcf:Color
nodes below /the/starting/node, you say:

query = qm.createQuery("/the/starting/node/pcf:Color", Query.XPATH);

if this still doesn't work, you prepend /jcr:root at the beginning, like
this:

query = qm.createQuery("/jcr:root/the/starting/node/pcf:Color",
Query.XPATH);

Good luck!
Alexander Klimetschek

Re: Query for Nodes from a given Path

Reply Threaded More More options
Print post
Permalink
On Tue, Jul 7, 2009 at 12:46 PM, BOWLER
Paul<[hidden email]> wrote:
> The problem is that the pcf:Color node can appear at many levels within the tree so specifying an absolute path won't work.
> [..]
> query = qm.createQuery("/jcr:root/the/starting/node/pcf:Color",

The trick is the "//" location step, which means descendent-or-self,
ie. any node named pcf:Color in the subtree under the absolute path
given before.

Regards,
Alex

--
Alexander Klimetschek
[hidden email]
BOWLER Paul

RE: Query for Nodes from a given Path

Reply Threaded More More options
Print post
Permalink
Superb.

Many thanks Alex.

P.

-----Original Message-----
From: Alexander Klimetschek [mailto:[hidden email]]
Sent: 07 July 2009 11:47
To: [hidden email]
Subject: Re: Query for Nodes from a given Path

On Tue, Jul 7, 2009 at 12:46 PM, BOWLER
Paul<[hidden email]> wrote:
> The problem is that the pcf:Color node can appear at many levels
within the tree so specifying an absolute path won't work.
> [..]
> query = qm.createQuery("/jcr:root/the/starting/node/pcf:Color",

The trick is the "//" location step, which means descendent-or-self,
ie. any node named pcf:Color in the subtree under the absolute path
given before.

Regards,
Alex

--
Alexander Klimetschek
[hidden email]