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
Dear Goetz,
Thanks for your mail.
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.
I tried the query, but I get a result in both cases, no matter if I use compare() or "=". Did you refer to the first or second of your two local:getLabel functions? Could you possibly minimize the example even more?
Thanks in advance, Christian
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
Hi Goetz,
I also had a quick try myself. It works without any problems on my setup (without the 'compare' function) and gives me the following results:
*local:getLabel('_and', 'FR') --> et* *local:getLabel('_and') --> und*
Setup: *OS X 10.10*
*java version "1.8.0_25"* *BaseX 8.0.3 GUI*
You find the code as attachment ... does this help?
Cheers, Lukas
On Tue, Mar 24, 2015 at 3:33 PM, Christian Grün christian.gruen@gmail.com wrote:
Dear Goetz,
Thanks for your mail.
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.
I tried the query, but I get a result in both cases, no matter if I use compare() or "=". Did you refer to the first or second of your two local:getLabel functions? Could you possibly minimize the example even more?
Thanks in advance, Christian
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
basex-talk@mailman.uni-konstanz.de