Skip to content

Commit b364aa7

Browse files
committed
[fix] castable as for untyped atomic value(s)
It appears perfectly acceptable for UntypedAtomicValue.convertTo to convert to subtypes of Type.STRING as StringValue(… value, requiredType). The conversion was omitted, and the omission was breaking “castable as” for Type.NCNAME in particular. These conversions are already acceptable for StringValue(s), and are borrowed from there.
1 parent 1385801 commit b364aa7

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

exist-core/src/main/java/org/exist/xquery/value/UntypedAtomicValue.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public static AtomicValue convertTo(UntypedAtomicValue strVal, String value, int
6565
return strVal == null ? new UntypedAtomicValue(expression, value) : strVal;
6666
case Type.STRING:
6767
return new StringValue(expression, value);
68+
case Type.NORMALIZED_STRING:
69+
case Type.TOKEN:
70+
case Type.LANGUAGE:
71+
case Type.NMTOKEN:
72+
case Type.NAME:
73+
case Type.NCNAME:
74+
case Type.ID:
75+
case Type.IDREF:
76+
case Type.ENTITY:
77+
return new StringValue(expression, value, requiredType);
6878
case Type.ANY_URI:
6979
return new AnyURIValue(expression, value);
7080
case Type.BOOLEAN:
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
(:
3+
: eXist-db Open Source Native XML Database
4+
: Copyright (C) 2001 The eXist-db Authors
5+
:
6+
7+
: http://www.exist-db.org
8+
:
9+
: This library is free software; you can redistribute it and/or
10+
: modify it under the terms of the GNU Lesser General Public
11+
: License as published by the Free Software Foundation; either
12+
: version 2.1 of the License, or (at your option) any later version.
13+
:
14+
: This library is distributed in the hope that it will be useful,
15+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
: Lesser General Public License for more details.
18+
:
19+
: You should have received a copy of the GNU Lesser General Public
20+
: License along with this library; if not, write to the Free Software
21+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22+
:)
23+
(:~
24+
: @see https://github.com/eXist-db/exist/issues/4518
25+
:)
26+
xquery version "3.1";
27+
28+
module namespace ca="http://exist-db.org/xquery/test";
29+
30+
declare namespace test="http://exist-db.org/xquery/xqsuite";
31+
32+
(: atomic values not explicitly converted to strings were falsely returning false to _castable_as_
33+
in other words, all these tests were returning nc="false"
34+
:)
35+
declare
36+
%test:args("ixml") %test:assertEquals("<result name=""ixml"" nc=""true""/>")
37+
%test:args("comment") %test:assertEquals("<result name=""comment"" nc=""true""/>")
38+
%test:args("apple-jack") %test:assertEquals("<result name=""apple-jack"" nc=""true""/>")
39+
%test:args("123") %test:assertEquals("<result name=""123"" nc=""false""/>")
40+
%test:args("élève") %test:assertEquals("<result name=""élève"" nc=""true""/>")
41+
%test:args("pfx:suffix") %test:assertEquals("<result name=""pfx:suffix"" nc=""false""/>")
42+
function ca:n-castable($d) {
43+
let $doc := <e name="e"><f n="{$d}"/></e>
44+
let $el := $doc/descendant::*[exists(@n)]
45+
let $n := $el/@n
46+
return <result name="{$n}" nc="{$n castable as xs:NCName}"/>
47+
};
48+
49+
(: explicit string conversion was always working - for reference :)
50+
declare
51+
%test:args("ixml") %test:assertEquals("<result name=""ixml"" nc=""true""/>")
52+
%test:args("comment") %test:assertEquals("<result name=""comment"" nc=""true""/>")
53+
%test:args("apple-jack") %test:assertEquals("<result name=""apple-jack"" nc=""true""/>")
54+
%test:args("123") %test:assertEquals("<result name=""123"" nc=""false""/>")
55+
%test:args("élève") %test:assertEquals("<result name=""élève"" nc=""true""/>")
56+
%test:args("pfx:suffix") %test:assertEquals("<result name=""pfx:suffix"" nc=""false""/>")
57+
function ca:s-castable($d) {
58+
let $doc := <e name="e"><f n="{$d}"/></e>
59+
let $el := $doc/descendant::*[exists(@n)]
60+
let $n := $el/@n, $s := string($n)
61+
return <result name="{$n}" nc="{$s castable as xs:NCName}"/>
62+
};
63+
64+
(: sanity check that casting accepts the same values as castable as :)
65+
declare
66+
%test:args("ixml") %test:assertEquals("<result name=""ixml"" nc=""true""/>")
67+
%test:args("comment") %test:assertEquals("<result name=""comment"" nc=""true""/>")
68+
%test:args("apple-jack") %test:assertEquals("<result name=""apple-jack"" nc=""true""/>")
69+
%test:args("123") %test:assertError("err:")
70+
%test:args("élève") %test:assertEquals("<result name=""élève"" nc=""true""/>")
71+
%test:args("pfx:suffix") %test:assertError("err:")
72+
function ca:s-cast-castable($d) {
73+
let $doc := <e name="e"><f n="{$d}"/></e>
74+
let $el := $doc/descendant::*[exists(@n)]
75+
let $n := $el/@n, $ncname := xs:NCName($n)
76+
return <result name="{$n}" nc="{$ncname castable as xs:NCName}"/>
77+
};
78+
79+

0 commit comments

Comments
 (0)