Problem using Count() function with TAPI4J

Hello,

I’m new in the TaminoAPI4Java with XQuery.

I need to obtain the number of documents that satisfies a XQuery. I’ve tried using the count function but, although the XQuery is correctly parsed, the result is’nt the desired.

I better show you a simple example for an imaginary Entity called Press:

The XQuery is:
for $p in input()/Press, $f in $p/fecha
where $f=“2003/05/03”
return $p

In my app, in order to obtain the number of documents, I transform the String as follows:
count(
for $p in input()/Press, $f in $p/fecha
where $f=“2003/05/03”
return $p)

This XQuery with the Tamino Interactive Interface returns:
xq:value27</xq:value>

But the TaminoAPI4J returns:
xq:value/

The piece of code that performs the XQuery is:
(…)
tsAccessor = connection.newStreamAccessor( TAccessLocation.newInstance( inoCol ) );
TXQuery xQuery = TXQuery.newInstance (myQuery);
tis = tsAccessor.xquery( xQuery );
(…)


Do you know why? Can you help me to do this?

Thanks in advance,

Pau.


Pau Garc

Hi,

I am quite new to Tamino myself, but I have been using the count function before, it worked.

Please try the following:

for $p in input()/Press, $f in $p/fecha
where $f=“2003/05/03”
return count($p)

Let me know how u r doing!

Hi Dax42,

I had already tried this, but it is a different query: The number of ocurrences of each document, that is, a sequence of 1’s.

<xq:result xmlns:xq=“http://namespaces.softwareag.com/tamino/XQuery/result”>
xq:value1</xq:value>
xq:value1</xq:value>

xq:value1</xq:value>
xq:value1</xq:value>
</xq:result>

Thanks for your interest,


Pau Garc

Ah, of course.

“for” gives you the documents one by one, so instead you could try using “let” which should give you all documents at once and therefore the appropriate number.

I have just tried the new query you suggest:

count(let $p:=input()/Prensa where $p/fecha=“2003/05/03” return $p)

But the result is still wrong: xq:value/

:frowning:


Pau Garc

Hi,

I know I am repeating myself, but did you also try
let $p:=input()/Prensa where $p/fecha=“2003/05/03” return count($p)
?

If that doesn’t work (it should, since the counting worked with “for”, just not the way you wanted it to), I don’t know any further.
Also, I think if that doesn’t work the problem lies within other parts of your code…

Hi,

Yes I also tried that query and it also returned the “xq:value/” empty string. And the code seems to be correct because after doing the count I process the same query without the “count” function and I get the desired result.

This is the piece of code which performs the query:

TXQuery xQuery = TXQuery.newInstance(myXQuery);
txoAccessor = connection.newXMLObjectAccessor( TAccessLocation.newInstance( inoCol ) , TJDOMObjectModel.getInstance() );
tResponse = txoAccessor.xquery( xQuery );
String sResult = tResponse.getQueryContentAsString()


Any other suggestion?

Thanks in advance,

Pau Garc

Hello Pau,

Try it like this:

let $a:=count(for $p in input()/Press, $f in $p/fecha
where $f=“2003/05/03” return $p)
return $a

Rob

hello

I dont know, if you could solve your problem…but the point is:
The method getQueryContentAsString is not working for getting the result of the count.
Better use this:
String myResult = “”;
TXQuery countQuery = TXQuery.newInstance(“count(” + xquery.getExpression() + “)”);
TResponse response = accessor.xquery(countQuery);
TXMLObjectIterator xmlIterator = response.getXMLObjectIterator();
while (xmlIterator.hasNext()) {
TXMLObject xmlObject = xmlIterator.next();
Element myElement = ((Element)xmlObject.getElement());
myResult = myElement.getFirstChild().getNodeValue();
}

best regards
Reto Peter, Software AG Schweiz

Hello,

I solved the problem in a similar way.

Thanks for your interest.

Best Regards,

Pau Garc

Hola pau, me ocurre el mismo problema que a te ocurria a ti, como lo has solucionado??, he probado todo lo que dicen aqui pero no me ha funcionado nada :frowning:

gracias y un saludo