1
votes

I am trying to use fn:string-length function in Xquery and strangely I am getting this error.

line 19, column 66: {err}XP0004 [{bea-err}XP0004a]: Operator "gt" is not applicable for arguments of type {http://www.w3.org/2001/XMLSchema}integer and {http://www.w3.org/2001/XMLSchema}string

All I am trying to do is, to get a length of a XPath expression and compare it with some value.

{                 
  let $PlaceofDeliveryZoneCodeVal := $getRevenueRequest1/ns0:AlternativeCode/ns0:AlternativeCodeVal/text()
  return
    if(fn:string-length($PlaceofDeliveryZoneCodeVal) > "8") then
      <ns1:GeoId>
        <ns1:AlternativeCodeVal>{$PlaceofDeliveryZoneCodeVal}</ns1:AlternativeCodeVal>
      </ns1:GeoId>
    else()
}
1
As a side note: when comparing two atomics, value comparisons are less heavy than general comparisons (gt 8 instead of > 8). > actually performs a comparison on two sequences and is an existential quantification on their cartesian product, looking for an item on the left greater than an item on the right. It also has extra machinery w.r.t. untyped inputs. Having said that, many engines will be smart enough to figure out that both sides have a single item that is an integer, and simply compare them. - Ghislain Fourny

1 Answers

3
votes

The problem is that because the integer is wrapped in quotes, you are comparing an integer to a string:

(fn:string-length($PlaceofDeliveryZoneCodeVal) > "8") 

If you remove the quotes, the comparison will succeed:

(fn:string-length($PlaceofDeliveryZoneCodeVal) > 8)