I tried to implement a simple Xml-based dictionary.
Here is a fragment of the Xml data I’m using:
<?xml version="1.0" encoding="utf-8"?>
<LABELS>
<LABEL label="_and">
<TERM LG="EN">and</TERM>
<TERM LG="FR">et</TERM>
<TERM LG="DE">und</TERM>
<TERM LG="BG">и</TERM>
<TERM LG="CS">a</TERM>
<TERM LG="DA">og</TERM>
<TERM LG="EL">και</TERM>
<TERM LG="ES">y</TERM>
<TERM LG="ET">ja</TERM>
<TERM LG="FI">ja</TERM>
<TERM LG="HU">és</TERM>
<TERM LG="IT">e</TERM>
<TERM LG="LT">ir</TERM>
<TERM LG="LV">līdz</TERM>
<TERM LG="MT">u</TERM>
<TERM LG="NL">en</TERM>
<TERM LG="PL">i</TERM>
<TERM LG="PT">e</TERM>
<TERM LG="RO">şi</TERM>
<TERM LG="SK">a</TERM>
<TERM LG="SL">in</TERM>
<TERM LG="SV">och</TERM>
</LABEL>
</LABELS>
Say it’s saved on C:\test\labels.xml.
Now, I defined the following module:
BEGIN_CODE>>>
(:: default language:)
declare variable $lang as xs:string := 'DE';
(:: dictionary :)
declare variable $labels := doc('C:\test\Labels.xml');
(:: get localized label (default language) :)
declare function local:getLabel(
$key as xs:string*
) as xs:string* {
$labels/LABELS/LABEL[compare(@label, $key) = 0]/TERM[compare(@LG, $lang) = 0]/text()
};
(:: get localized label (specific language) :)
declare function local:getLabel(
$key as xs:string*,
$lang as xs:string*
) as xs:string* {
$labels/LABELS/LABEL[compare(@label, $key) = 0]/TERM[compare(@LG, $lang) = 0]/text()
};
(:: get localized label (specific language) - debug version :)
declare function local:getLabelDebug(
$key as xs:string*,
$lang as xs:string*
) {
let $terms := $labels/LABELS/LABEL[compare(@label, $key) = 0]/TERM
for $t in ($terms)
return <t>{xs:string($t/@LG)}: {$t/text()} (comparison: {$t/@LG = $lang})</t>
};
(: does not work as I expect: let $retVal := local:getLabel('_and', 'FR') because it returns nothing :)
(:
using "for $t in ($terms[compare(@LG, $lang) = 0])" yields empty result
:)
let $retVal := local:getLabelDebug('_and', 'FR')
return <r>{$retVal}</r>
<<<END_CODE
The first function appears to work as expected. However, instead of “[@LG = $lang]” I had to use [compare(@LG, $lang) = 0]” – I don’t know why, but otherwise, it does not return any results.
On the other hand, using “[@label = $key]” on the same line worked.
The second function does not return anything at all.
The debug version of the latter shows that all the needed information is in place. Obviously, usage of the same variable name globally and locally is not an issue (I’ve tried it out).
What’s wrong with the code? Is there an issue with the current version (8.0.3)? My installation runs on a Windows 8.1 machine.
Some help is appreciated.
Kind regards,
Goetz