I'm using XQuery to select a list of nodes from an XML according to some conditions. The result is a list of a couple of nodes, but I only want to return one (first or last) of them.
First I use XPath to select some elements, then I want to filter out any element that does not contain a value (ie. is empty). For that I am using the functx:if-not-empty function. What I get is a sequence of items and I only need one of them. This is my code so far:
declare function xf:createHeader($var as element(sgi:inputType))
{
<ns0:Header>
<ns0:MSG_TYPE>MESSAGETYPE</ns0:MSG_TYPE>
<ns0:MSG_NAMESPACE>{ fn:namespace-uri($var) }</ns0:MSG_NAMESPACE>
<ns0:UNIT>
{
let $x := $var/DataArea/OrganisationalUnit[@type = "Responsible"]/PartnerID[@agencyID = "TT4"]
for $y in $x
where functx:if-not-empty($y)
return data($y)
}
</ns0:UNIT>
</ns0:Header>
};