Search code examples
xpathxquerystring-length

Error while using fn:string-length in Xquery


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()
}

Solution

  • 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)