Skip to content

Commit b5274f1

Browse files
[test] Add fn:id tests that correspond to fn:element-with-id tests
1 parent 8aa4695 commit b5274f1

2 files changed

Lines changed: 162 additions & 3 deletions

File tree

exist-core/src/main/java/org/exist/dom/memtree/DocumentImpl.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,18 +701,35 @@ public void selectDescendantAttributes(final NodeTest test, final Sequence resul
701701
}
702702
}
703703

704+
/**
705+
* Gets a specified node of this document.
706+
*
707+
* @param id the ID of the node to select
708+
* @return the specified node of this document, or null if this document
709+
* does not have the specified node
710+
*/
704711
public NodeImpl selectById(final String id) {
705712
return selectById(id, false);
706713
}
707714

708-
public NodeImpl selectById(final String id, boolean typeConsidered) {
715+
/**
716+
* Gets a specified node of this document.
717+
*
718+
* @param id the ID of the node to select
719+
* @param typeConsidered if true, this method should consider node
720+
* type attributes (i.e. <code>xsi:type="xs:ID"</code>);
721+
* if false, this method should not consider
722+
* node type attributes
723+
* @return the specified node of this document, or null if this document
724+
* does not have the specified node
725+
*/
726+
public NodeImpl selectById(final String id, final boolean typeConsidered) {
709727
if(size == 1) {
710728
return null;
711729
}
712730
expand();
713731
final ElementImpl root = (ElementImpl) getDocumentElement();
714-
if ( // (rootConsidered && Objects.equals(root.getAttributeValue("xsi:type"), "xs:ID")) ||
715-
hasIdAttribute(root.getNodeNumber(), id)) {
732+
if (hasIdAttribute(root.getNodeNumber(), id)) {
716733
return root;
717734
}
718735
final int treeLevel = this.treeLevel[root.getNodeNumber()];
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
(:
2+
: eXist-db Open Source Native XML Database
3+
: Copyright (C) 2001 The eXist-db Authors
4+
:
5+
: info@exist-db.org
6+
: http://www.exist-db.org
7+
:
8+
: This library is free software; you can redistribute it and/or
9+
: modify it under the terms of the GNU Lesser General Public
10+
: License as published by the Free Software Foundation; either
11+
: version 2.1 of the License, or (at your option) any later version.
12+
:
13+
: This library is distributed in the hope that it will be useful,
14+
: but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
: Lesser General Public License for more details.
17+
:
18+
: You should have received a copy of the GNU Lesser General Public
19+
: License along with this library; if not, write to the Free Software
20+
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
:)
22+
xquery version "3.1";
23+
24+
module namespace fni="http://exist-db.org/xquery/test/function_id";
25+
26+
declare namespace test="http://exist-db.org/xquery/xqsuite";
27+
28+
declare variable $fni:data :=
29+
document{
30+
<employee xml:id="ID21256"
31+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
32+
xmlns:xs="http://www.w3.org/2001/XMLSchema">
33+
<empnr xsi:type="xs:ID">E21256</empnr>
34+
<first>John</first>
35+
<last>Brown</last>
36+
</employee>
37+
};
38+
39+
declare variable $fni:ids2 :=
40+
<IDS2 xmlns = "http://www.w3.org/XQueryTest/ididrefs"
41+
xmlns:i = "http://www.w3.org/XQueryTest/ididrefs"
42+
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
43+
xsi:schemaLocation = "http://www.w3.org/XQueryTest/ididrefs id.xsd">
44+
45+
<Element-with-ID-attribute id="alpha"><data>derived from Phoenician Aleph</data></Element-with-ID-attribute>
46+
<Element-with-ID-attribute id="beta"><data>derived from Phoenician Beth</data></Element-with-ID-attribute>
47+
<Element-with-Restricted-ID-attribute id="gamma"><data>derived from Phoenician Gimel</data></Element-with-Restricted-ID-attribute>
48+
<Element-with-Restricted-ID-attribute id="delta"><data>derived from Phoenician Daleth</data></Element-with-Restricted-ID-attribute>
49+
<Element-as-ID>epsilon</Element-as-ID>
50+
<Element-as-ID>zeta</Element-as-ID>
51+
<Element-as-Restricted-ID>eta</Element-as-Restricted-ID>
52+
<Element-as-Restricted-ID>theta</Element-as-Restricted-ID>
53+
<Element-with-ID-child><id>iota</id><data>Derived from Phoenician Yodh</data></Element-with-ID-child>
54+
<Element-with-ID-child><id>kappa</id><data>Derived from Phoenician Kaph</data></Element-with-ID-child>
55+
<Element-with-Restricted-ID-child><id>lambda</id><data>Derived from Phoenician Lamedh</data></Element-with-Restricted-ID-child>
56+
<Element-with-Restricted-ID-child><id>mu</id><data>Derived from Phoenician Mem</data></Element-with-Restricted-ID-child>
57+
<Element-with-complex-ID-child><id charmed="false">nu</id><data>Derived from Phoenician Nun</data></Element-with-complex-ID-child>
58+
<Element-with-ID-list-child><id>xi</id><data>Derived from Phoenician Samekh</data></Element-with-ID-list-child>
59+
<Element-with-ID-list-child><id>ping pong</id><data>not an id, as not a singleton value</data></Element-with-ID-list-child>
60+
<Element-with-ID-union-child><id>omicron</id><data>Derived from Phoenician Ayin</data></Element-with-ID-union-child>
61+
<Element-with-ID-union-child><id>853</id><data>Not an id, wrong member of union</data></Element-with-ID-union-child>
62+
63+
<IDREF>zeta</IDREF>
64+
<IDREFS>gamma kappa</IDREFS>
65+
<IDREF-List>epsilon mu alpha</IDREF-List>
66+
<IDREF-Union>eta 234 delta</IDREF-Union>
67+
<Restricted-IDREF>iota</Restricted-IDREF>
68+
<List-of-Restricted-IDREF>lambda beta iota</List-of-Restricted-IDREF>
69+
70+
<IDREF-content>zeta</IDREF-content>
71+
<IDREFS-content>gamma kappa</IDREFS-content>
72+
<IDREF-List-content>epsilon mu alpha</IDREF-List-content>
73+
<IDREF-Union-content>eta 234 delta</IDREF-Union-content>
74+
<Restricted-IDREF-content>iota</Restricted-IDREF-content>
75+
<List-of-Restricted-IDREF-content>lambda beta iota</List-of-Restricted-IDREF-content>
76+
77+
<Nillable-IDREF>omicron</Nillable-IDREF>
78+
<Nillable-IDREF xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
79+
80+
<Nillable-ID xsi:nil="true"/>
81+
82+
<Restricted-NCName-or-IDREF-list>Q</Restricted-NCName-or-IDREF-list>
83+
<Restricted-NCName-or-IDREF-list>Q omicron</Restricted-NCName-or-IDREF-list>
84+
85+
<W i:IDREF="epsilon"/>
86+
<W i:IDREFS="delta eta"/>
87+
<W i:IDREF-List="zeta iota"/>
88+
<W i:IDREF-Union="gamma 976 delta alpha"/>
89+
<W i:Restricted-IDREF="beta"/>
90+
<W i:List-of-Restricted-IDREF="lambda kappa"/>
91+
<W i:Restricted-NCName-or-IDREF-list="Z"/>
92+
<W i:Restricted-NCName-or-IDREF-list="Z epsilon"/>
93+
94+
</IDS2>;
95+
96+
declare
97+
%test:assertEquals("employee")
98+
function fni:id-with-is-id-attribute() {
99+
$fni:data/id('ID21256')/name()
100+
};
101+
102+
declare
103+
%test:assertEquals("empnr")
104+
function fni:id-with-is-id-type() {
105+
$fni:data/id('E21256')/name()
106+
};
107+
108+
declare
109+
%test:assertEquals("Element-with-ID-attribute", "Element-with-ID-attribute", "id", "id")
110+
function fni:fn-id-1() {
111+
$fni:ids2/id('alpha beta iota kappa')/local-name()
112+
};
113+
114+
declare
115+
%test:assertEquals("id", "id")
116+
function fni:fn-id-2() {
117+
$fni:ids2/id('lambda mu')/local-name()
118+
};
119+
120+
declare
121+
%test:assertEquals("id")
122+
function fni:fn-id-3() {
123+
$fni:ids2/id('nu')/local-name()
124+
};
125+
126+
declare
127+
%test:assertEquals("id", "0")
128+
function fni:fn-id-4() {
129+
$fni:ids2/id('xi')/local-name(), count($fni:ids2/id('ping'))
130+
};
131+
132+
declare
133+
%test:assertEquals("id", "0")
134+
function fni:fn-id-5() {
135+
$fni:ids2/id('omicron')/local-name(), count($fni:ids2/id('853'))
136+
};
137+
138+
declare
139+
%test:assertError("XPTY0004")
140+
function fni:fn-id-context-item-not-node() {
141+
(1 to 5)[fn:id("argument1")]
142+
};

0 commit comments

Comments
 (0)