You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@sling.apache.org by Roy Teeuwen <ro...@teeuwen.be> on 2016/03/02 19:13:49 UTC

Sorting of a JCR Query

Hello all,

I have a node A with subnodes B,C,D and am creating a JCR query to retrieve some of the subnodes by using the following XPATH query:

/jcr:root/A//element(*, nt:unstructured) [
	jcr:contains(property, ‘value’)
]

The problem that I am facing at this moment that the order it retrieves the elements seem te be random. Sometimes it gives back B,D and sometimes D,B. 
What I would like is that it always gives back the order that the nodes have below A has in the jcr repository, is it possible to pass this to the query (either XPath or an SQL for the same result)

Thanks
Roy

Re: Sorting of a JCR Query

Posted by Roy Teeuwen <ro...@teeuwen.be>.
Hey Julian,

Oke, thanks! 

Greetings,
Roy
> On 03 Mar 2016, at 13:11, Julian Sedding <js...@gmail.com> wrote:
> 
> Hello Roy
> 
> XPath queries allow ordering by a property:
> /jcr:root/A//element(*, nt:unstructured) [
>        jcr:contains(property, ‘value’)
> ] order by @otherProperty descending
> 
> If you want the child order, you should iterate over the child node
> using the API:
> 
> NodeIterator children = session.getNode("/A").getNodes();
> while (children.hasNext()) {
>    Node child = children.nextNode();
>    // do something with it
> }
> 
> In other words: query results don't know about the sibling order of nodes.
> 
> Regards
> Julian
> 
> 
> 
> On Wed, Mar 2, 2016 at 7:13 PM, Roy Teeuwen <ro...@teeuwen.be> wrote:
>> Hello all,
>> 
>> I have a node A with subnodes B,C,D and am creating a JCR query to retrieve some of the subnodes by using the following XPATH query:
>> 
>> /jcr:root/A//element(*, nt:unstructured) [
>>        jcr:contains(property, ‘value’)
>> ]
>> 
>> The problem that I am facing at this moment that the order it retrieves the elements seem te be random. Sometimes it gives back B,D and sometimes D,B.
>> What I would like is that it always gives back the order that the nodes have below A has in the jcr repository, is it possible to pass this to the query (either XPath or an SQL for the same result)
>> 
>> Thanks
>> Roy


Re: Sorting of a JCR Query

Posted by Julian Sedding <js...@gmail.com>.
Hello Roy

XPath queries allow ordering by a property:
/jcr:root/A//element(*, nt:unstructured) [
        jcr:contains(property, ‘value’)
] order by @otherProperty descending

If you want the child order, you should iterate over the child node
using the API:

NodeIterator children = session.getNode("/A").getNodes();
while (children.hasNext()) {
    Node child = children.nextNode();
    // do something with it
}

In other words: query results don't know about the sibling order of nodes.

Regards
Julian



On Wed, Mar 2, 2016 at 7:13 PM, Roy Teeuwen <ro...@teeuwen.be> wrote:
> Hello all,
>
> I have a node A with subnodes B,C,D and am creating a JCR query to retrieve some of the subnodes by using the following XPATH query:
>
> /jcr:root/A//element(*, nt:unstructured) [
>         jcr:contains(property, ‘value’)
> ]
>
> The problem that I am facing at this moment that the order it retrieves the elements seem te be random. Sometimes it gives back B,D and sometimes D,B.
> What I would like is that it always gives back the order that the nodes have below A has in the jcr repository, is it possible to pass this to the query (either XPath or an SQL for the same result)
>
> Thanks
> Roy