Select all leafs of a tree w/XPath

This is a discussion on Select all leafs of a tree w/XPath within the Apache forums in Application Servers & Tools category; Hello JackRabbit people, I have a problem with selection of all leaf nodes of my tree. The tree consists of nodes of type nt:unstructured named "testnode". I want to select all the nodes that have no children, that is, all the leafs. However, the "//testnode[not(testnode)]" query gives zero results. What is a bit more confusing, is that the "//testnode[testnode]" query returns all the elements of the tree, not just the ones that have at least one child of name "testnode". How should I select leafs? I'm sorry if the question is too dumb, but I'm not closely familiar nor to ...

Go Back   Application Development Forum > Application Servers & Tools > Apache

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 11-03-2008, 07:45 PM
Sergey Podatelev
Guest
 
Default Select all leafs of a tree w/XPath

Hello JackRabbit people,

I have a problem with selection of all leaf nodes of my tree.
The tree consists of nodes of type nt:unstructured named "testnode".

I want to select all the nodes that have no children, that is, all the
leafs.
However, the "//testnode[not(testnode)]" query gives zero results.

What is a bit more confusing, is that the "//testnode[testnode]" query
returns all the elements of the tree, not just the ones that have at least
one child of name "testnode".

How should I select leafs?

I'm sorry if the question is too dumb, but I'm not closely familiar nor to
XPath neither to JackRabbit.

--
sp

Reply With Quote
  #2  
Old 11-08-2008, 12:53 PM
Sergey Podatelev
Guest
 
Default Re: Select all leafs of a tree w/XPath

Seriously, guys, is there an easy way to select leafs or I have to implement
a particular property for this?

On Tue, Nov 4, 2008 at 3:45 AM, Sergey Podatelev <brightnesslevels@gmail.com
> wrote:


> Hello JackRabbit people,
>
> I have a problem with selection of all leaf nodes of my tree.
> The tree consists of nodes of type nt:unstructured named "testnode".
>
> I want to select all the nodes that have no children, that is, all the
> leafs.
> However, the "//testnode[not(testnode)]" query gives zero results.
>
> What is a bit more confusing, is that the "//testnode[testnode]" query
> returns all the elements of the tree, not just the ones that have at least
> one child of name "testnode".
>
> How should I select leafs?
>
> I'm sorry if the question is too dumb, but I'm not closely familiar nor to
> XPath neither to JackRabbit.
>
> --
> sp
>




--
sp

Reply With Quote
  #3  
Old 11-08-2008, 02:06 PM
Jukka Zitting
Guest
 
Default Re: Select all leafs of a tree w/XPath

Hi,

On Sat, Nov 8, 2008 at 6:53 PM, Sergey Podatelev
<brightnesslevels@gmail.com> wrote:
> Seriously, guys, is there an easy way to select leafs or I have to implement
> a particular property for this?


There is no query constraint for selecting leaf nodes.

Your idea of using a particular property that you only set on leaf
nodes is one way to do this. An alternative would be to use normal
tree traversal like this:

void visitLeaves(Node node) {
NodeIterator nodes = node.getNodes();
if (!nodes.hasNext()) {
// ... this is a leaf node, process it ...
} else {
while (nodes.hasNext()) {
visitLeaves(nodes.nextNode());
}
}
}

BR,

Jukka Zitting

Reply With Quote
  #4  
Old 11-08-2008, 05:25 PM
Phil Weighill-Smith
Guest
 
Default Re: Select all leafs of a tree w/XPath

Are you sure you can't use the XPath expression:

//*[count(child::*) = 0]

to select the element nodes that have no children (i.e. leaf nodes)?

Phil :n.

----- "Jukka Zitting" <jukka.zitting@gmail.com> wrote:

> Hi,
>
> On Sat, Nov 8, 2008 at 6:53 PM, Sergey Podatelev
> <brightnesslevels@gmail.com> wrote:
> > Seriously, guys, is there an easy way to select leafs or I have to

> implement
> > a particular property for this?

>
> There is no query constraint for selecting leaf nodes.
>
> Your idea of using a particular property that you only set on leaf
> nodes is one way to do this. An alternative would be to use normal
> tree traversal like this:
>
> void visitLeaves(Node node) {
> NodeIterator nodes = node.getNodes();
> if (!nodes.hasNext()) {
> // ... this is a leaf node, process it ...
> } else {
> while (nodes.hasNext()) {
> visitLeaves(nodes.nextNode());
> }
> }
> }
>
> BR,
>
> Jukka Zitting


Reply With Quote
  #5  
Old 11-10-2008, 03:59 AM
Alexander Klimetschek
Guest
 
Default Re: Select all leafs of a tree w/XPath

On Sat, Nov 8, 2008 at 11:25 PM, Phil Weighill-Smith
<phil.weighill-smith@volantis.com> wrote:
> Are you sure you can't use the XPath expression:
>
> //*[count(child::*) = 0]
>
> to select the element nodes that have no children (i.e. leaf nodes)?


No, JCR's subset of XPath does not support the count() method.

Regards,
Alex

--
Alexander Klimetschek
alexander.klimetschek@day.com

Reply With Quote
  #6  
Old 11-10-2008, 10:23 AM
Ivan Latysh
Guest
 
Default Re: Select all leafs of a tree w/XPath

Sergey Podatelev wrote:

> Seriously, guys, is there an easy way to select leafs or I have to implement
> a particular property for this?

Yes there are.

I have a Saxon adapter that allow you to run full XPath 2.0 and XQuery 1.0 on
JCR. The only caveat is the speed, since adapter uses JCR API only.

Let me know if you want to give it a shot.

--
Ivan Latysh
IvanLatysh@gmail.com

Reply With Quote
  #7  
Old 11-10-2008, 10:36 AM
Sergey Podatelev
Guest
 
Default Re: Select all leafs of a tree w/XPath

Thanks to everyone for answers.Ivan, I think I should go with an
extra-property method since speed is important for my purposes.
Thanks for suggestion, though (:

On Mon, Nov 10, 2008 at 6:23 PM, Ivan Latysh <ivanlatysh@gmail.com> wrote:

> Sergey Podatelev wrote:
>
> Seriously, guys, is there an easy way to select leafs or I have to
>> implement
>> a particular property for this?
>>

> Yes there are.
>
> I have a Saxon adapter that allow you to run full XPath 2.0 and XQuery 1.0
> on JCR. The only caveat is the speed, since adapter uses JCR API only.
>
> Let me know if you want to give it a shot.
>
> --
> Ivan Latysh
> IvanLatysh@gmail.com
>




--
sp

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 04:14 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.