sorting thrown off by composing xml

I’ve got a a relatively straightforward query for sorting documents by date and returning the latest one:
declare namespace tf = “http://namespaces.softwareag.com/tamino/TaminoFunction
(
for $d in input()/blah
where $d[blah=‘BLAH’]
return $d
sort by ($d/time/@value descending)
)[position() le 1]

The value attribute of the time element is an xml schema dateTime type, and everything works dandily. Until, that is, I want to compose xml in the “return” clause, a la the following:

declare namespace tf = “http://namespaces.softwareag.com/tamino/TaminoFunction
(
for $d in input()/blah
where $d[blah=‘BLAH’]
return {$d}
sort by ($d/time/@value descending)
)[position() le 1]

which, crucially, seems to totally ignore the “sort by” clause (though the position statement still works).

Any suggestions??

Thanks in advance for any help.

Hi,

have you tried in this way?

declare namespace tf = “http://namespaces.softwareag.com/tamino/TaminoFunction
(
for $d in input()/blah sort by ($d/time/@value descending)
where $d[blah=‘BLAH’]
return {$d}
)[position() le 1]

Regards

Hi,

The problem might be caused by the fact that your queries are invalid. This means the “sort by” clause references a variable that is not valid. Unfortunately Tamino 4.2 does not throw any error in this case, but shows somehow unpredictable behavior. To get a properly working query you should apply the following rewriting:

declare namespace tf = “http://namespaces.softwareag.com/tamino/TaminoFunction
(
for $d in input()/blah
where $d[blah=‘BLAH’]
return $d
sort by (./time/@value descending)
)[position() le 1]

For the second query the rewriting is a little bit harder, since the context item is a newly constructed element, you have to extract the sort value from. Due to the fact that during XML element construction all type information get lost you have to reintroduce the type information by applying a constructor function:

declare namespace xs = “XML Schema
declare namespace tf = “http://namespaces.softwareag.com/tamino/TaminoFunction
(
for $d in input()/blah
where $d[blah=‘BLAH’]
return {$d}
sort by (xs:time(./blah/time/@value) descending)
)[position() le 1]

In Tamino 4.4 the situation is much easier, since it provides the “order by” clause. Here the queries can be written in the following way:

(
for $d in input()/blah
where $d[blah=‘BLAH’]
order by $d/time/@value descending
return $d
)[position() le 1]

(
for $d in input()/blah
where $d[blah=‘BLAH’]
order by $d/time/@value descending
return {$d}
)[position() le 1]

Please note that the namespace declarations are now gone. In Tamino 4.4 you do not have to provide all the standard declarations for namespace prefixes like tf and xs.

Best Regards,

Thorsten