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:
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:
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:
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.