You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by tb...@dff.st on 2001/05/09 13:44:32 UTC

number of nodes

Hi there!
Xalan works perfectly fine for returning a nodelist that matches a given xpath.

However, using the count function (like "count(/node1/node2)") within
an XPath-
Statement throws an exception, probably due to the fact that what we need here
is an integer rather than a nodelist. Is there any way to retrieve such a
number?

Best regards
  Tilman

-------------------------------------------------
This mail sent through IMP: imp.dff.local

Re: number of nodes

Posted by Gary L Peskin <ga...@firstech.com>.
Tilman --

Thanks for the example.  Now I understand what you're trying to do.

The selectNodeIterator method evaluates the XPath expression which must
evaluate to a node-set and then returns that as a NodeIterator.  In your
case, your expression does not evaluate to a node-set but rather to a
number so this is why you're getting the exception.

Try
  XObject xobj = XPathAPI.eval(document, XPath);

This will do what you want.

HTH,
Gary

tbudde@dff.st wrote:
> 
> Ok... we tried the following:
> 
> NodeIterator ni = XPathAPI.selectNodeIterator(document, XPath);
> 
> ..with a document like:
> document = <test><bla>test</bla><bla>test2</bla></test>
> 
> ...and a XPath like:
> XPath = "count(/test/bla)"
> 
> What we get is:
> 
> exception: Can not convert #NUMBER to a NodeList!
> org.apache.xpath.XPathException: Can not convert #NUMBER to a NodeList!
>         at org.apache.xpath.objects.XObject.error(XObject.java:558)
>         at org.apache.xpath.objects.XObject.nodeset(XObject.java:321)
>         at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:162)
>         at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:139)
>         at st.dff.desire.businesslogic.BusinessObject.getElementNode
> (BusinessObject.java:115)
>         at st.dff.desire.businesslogic.BusinessObject.getElementString
> (BusinessObject.java:141)
>         at main.tester.main(tester.java:27)
> 
> So what we need to be returned is an integer, but what we receive is a
> Nodelist, and those two are hard to match. Sure, it would be rather easy to
> count the number of returned nodes instead. But since the count function
> belongs to the standard, we'd rather like to use that!
> 
> Until Later
>   Tilman

Re: number of nodes

Posted by tb...@dff.st.
Ok... we tried the following:

NodeIterator ni = XPathAPI.selectNodeIterator(document, XPath);

..with a document like:
document = <test><bla>test</bla><bla>test2</bla></test>

...and a XPath like:
XPath = "count(/test/bla)"

What we get is:

exception: Can not convert #NUMBER to a NodeList!
org.apache.xpath.XPathException: Can not convert #NUMBER to a NodeList!
        at org.apache.xpath.objects.XObject.error(XObject.java:558)
        at org.apache.xpath.objects.XObject.nodeset(XObject.java:321)
        at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:162)
        at org.apache.xpath.XPathAPI.selectNodeIterator(XPathAPI.java:139)
        at st.dff.desire.businesslogic.BusinessObject.getElementNode
(BusinessObject.java:115)
        at st.dff.desire.businesslogic.BusinessObject.getElementString
(BusinessObject.java:141)
        at main.tester.main(tester.java:27)

So what we need to be returned is an integer, but what we receive is a 
Nodelist, and those two are hard to match. Sure, it would be rather easy to 
count the number of returned nodes instead. But since the count function 
belongs to the standard, we'd rather like to use that!

Until Later
  Tilman

> Can you send your XML and XSLT as well as the exception that's
> occurring
> and a stack trace so that we can see what's actually happening?  Your
> original email did not contain enough details to figure out what's
> going
> on.
> 
> Thanks,
> Gary
> 
> tbudde@dff.st wrote:
> > Hi there!
> > Xalan works perfectly fine for returning a nodelist that matches a given 
xpath.
> > 
> > However, using the count function (like "count(/node1/node2)")
> within
> > an XPath-
> > Statement throws an exception, probably due to the fact that what we
> need here
> > is an integer rather than a nodelist. Is there any way to retrieve
> such a
> > number?


-------------------------------------------------
This mail sent through IMP: imp.dff.local

Re: number of nodes

Posted by Gary L Peskin <ga...@firstech.com>.
Can you send your XML and XSLT as well as the exception that's occurring
and a stack trace so that we can see what's actually happening?  Your
original email did not contain enough details to figure out what's going
on.

Thanks,
Gary

tbudde@dff.st wrote:
> 
> Hi there!
> Xalan works perfectly fine for returning a nodelist that matches a given xpath.
> 
> However, using the count function (like "count(/node1/node2)") within
> an XPath-
> Statement throws an exception, probably due to the fact that what we need here
> is an integer rather than a nodelist. Is there any way to retrieve such a
> number?
> 
> Best regards
>   Tilman
> 
> -------------------------------------------------
> This mail sent through IMP: imp.dff.local