From: Anthony M. <hi...@sp...> - 2012-07-12 20:18:05
|
First of all I am using eXist 1.4.1-rev15155-20110815 hosted on a Windows 7 box using jdk1.7.0_01. I noticed that I was getting results from a function call that were incorrect and I had been under the impression that the function call was working just fine. In troubleshooting I soon found that if I called the function directly I'd get one result, but the code that I was developing persisted in returning a different result. I finally narrowed it down to the following test case: If I execute the query: xquery version "1.0"; import module namespace mtrc="http://xquery.xtellus.com/modules/mtrc" at "xmldb:exist:///db/dev/modules/mtrc.xqm"; let $name := 'by-trace' return mtrc:get-colors($name) I get the following result (expected): <colors> <color>#FF0000</color> <color>#00FF00</color> <color>#0000FF</color> <color>#FF00FF</color> <color>#EEC900</color> <color>#B0C4DE</color> <color>#2E8B57</color> <color>#A020F0</color> <color>#6B8E23</color> <color>#F5DEB3</color> </colors> If I execute the query: xquery version "1.0"; import module namespace mtrc="http://xquery.xtellus.com/modules/mtrc" at "xmldb:exist:///db/dev/modules/mtrc.xqm"; let $name := 'by-trace' return typeswitch (element { 'foo' } {}) case element(foo) return mtrc:get-colors($name) default return () I get the following result (un-expected): <colors> <color>#FF0000</color> <color>#00FF00</color> <color>#0000FF</color> <color>#FF00FF</color> <color>#EEC900</color> <color>#</color> <color>#</color> <color>#A020F0</color> <color>#698B22</color> <color>#8B7E66</color> </colors> The mtrc:get-colors function: declare function mtrc:get-colors($name as xs:string) as node() { let $doc := doc('/db/dev/color-sets.xml')/* let $name := if ($doc/color-set[@name=$name]) then $name else 'by-trace' return <colors>{ for $color in co:hex-values($doc/color-set[@name=$name]/string()) return <color>{$color}</color> }</colors> }; The relevant part of the co: module: declare function co:first-color($name as xs:string, $list as xs:string) as node()? { let $nodes := if ($list ne '') then doc($co:color-path)/*/color-list[@src=$list]/color[ngram:starts-with(@name, $name)] else doc($co:color-path)//color[ngram:starts-with(@name, $name)] return $nodes[1] }; declare function co:hex-values($names as item()*) as xs:string* { co:hex-values($names, 'x11') }; declare function co:hex-values($names as item()*, $list as xs:string) as xs:string* { let $names := if ($names instance of xs:string) then tokenize($names, ',') else for $n in $names return string($n) return for $name in $names return concat('#', co:first-color($name, $list)/@hex/string()) }; Basically, the call to mtrc:get-colors winds up calling co:hex-values with a string argument that looks like: 'red,green,blue,magenta,gold2,lightsteelblue,sea green,purple,olive drab,wheat' and co:hex-values looks them up in a big list of colors and returns hex values. The same problem exists in version 1.4.2-rev16251-20120416 Other than a comment that the query to 8ms to execute there is nothing in the log to indicate what happened. What I am worried about is that it is a common pattern for me to end my queries with a typeswitch to return different formats of the results of the query, I can work around this case easy enough, but now I'm worried about the integrity of a considerable body of queries. Anthony |