Skip to content

Commit 728125c

Browse files
hide ##any default element namespace behind flag and fix resolution semantics
1 parent 8665f05 commit 728125c

7 files changed

Lines changed: 27 additions & 68 deletions

File tree

basex-core/src/main/java/org/basex/io/serial/MarkupSerializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ private ArrayList<QNm> qnames(final StringOption option) throws QueryIOException
466466
final ArrayList<QNm> list = new ArrayList<>();
467467
for(final byte[] name : distinctTokens(token(sopts.get(option)))) {
468468
try {
469-
list.add(QNm.parse(name, sc != null ? sc.elemNS : null, qc, sc, null));
469+
list.add(QNm.parse(name, sc == null ? null : sc.elemNS, qc, sc, null));
470470
} catch(final QueryException ex) {
471471
throw new QueryIOException(ex);
472472
}

basex-core/src/main/java/org/basex/query/QueryParser.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ private boolean defaultNamespaceDecl(final boolean fixed) throws QueryException
546546
if(elem) {
547547
if(!decl.add(ELEMENT)) throw error(DUPLNS);
548548
sc.elemNsFixed = fixed;
549-
sc.elemNS = uri.length == 0 ? null : uri;
549+
sc.elemNsAny = Token.eq(uri, QueryText.ANY_URI);
550+
sc.elemNS = sc.elemNsAny || uri.length == 0 ? null : uri;
550551
sc.dirNS = sc.elemNS;
551552
} else {
552553
if(!decl.add(FUNCTION)) throw error(DUPLNS);
@@ -952,7 +953,7 @@ private static boolean reserved(final QNm name) {
952953
* @throws QueryException query exception
953954
*/
954955
private void typeDecl(final AnnList anns) throws QueryException {
955-
final QNm qn = eQName(sc.elemNS, TYPENAME);
956+
final QNm qn = eQName(sc.elemNsAny ? XS_URI : sc.elemNS, TYPENAME);
956957
if(declaredTypes.contains(qn)) throw error(DUPLTYPE_X, qn.string());
957958
if(NSGlobal.reserved(qn.uri())) throw error(TYPERESERVED_X, qn.string());
958959
wsCheck(AS);
@@ -2465,8 +2466,8 @@ private ExprInfo simpleNodeTest(final Kind kind, final boolean all) throws Query
24652466
name = new QNm(concat(name.string(), cpToken(':')));
24662467
scope = NameTest.Scope.URI;
24672468
} else if(!eqName) {
2468-
scope = kind == Kind.ELEMENT && eq(sc.elemNS, ANY_URI) ? NameTest.Scope.LOCAL
2469-
: NameTest.Scope.FLEXIBLE;
2469+
scope = kind == Kind.ELEMENT && sc.elemNsAny ? NameTest.Scope.LOCAL
2470+
: NameTest.Scope.FLEXIBLE;
24702471
}
24712472
}
24722473
// name test: prefix:name, name, Q{uri}name
@@ -3475,7 +3476,7 @@ private SeqType castTarget() throws QueryException {
34753476
if(wsConsume("(")) {
34763477
type = choiceItemType().type;
34773478
} else {
3478-
final QNm name = eQName(eq(sc.elemNS, ANY_URI) ? XS_URI : sc.elemNS, TYPEINVALID);
3479+
final QNm name = eQName(sc.elemNsAny ? XS_URI : sc.elemNS, TYPEINVALID);
34793480
if(!name.hasURI() && eq(name.local(), token(ENUM))) {
34803481
if(!wsConsume("(")) throw error(WHICHCAST_X, BasicType.similar(name));
34813482
type = enumerationType();
@@ -3565,7 +3566,7 @@ private SeqType itemType() throws QueryException {
35653566
}
35663567
} else {
35673568
// attach default element namespace, or schema namespace if default element namespace is ##any
3568-
if(!name.hasURI()) name.uri(eq(sc.elemNS, ANY_URI) ? XS_URI : sc.elemNS);
3569+
if(!name.hasURI()) name.uri(sc.elemNsAny ? XS_URI : sc.elemNS);
35693570
// basic type
35703571
type = BasicType.get(name, false);
35713572
// declared type

basex-core/src/main/java/org/basex/query/StaticContext.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public final class StaticContext {
3838
public Collation collation;
3939
/** Default element/type namespace fixed flag. */
4040
public boolean elemNsFixed;
41+
/** Default element/type namespace ##any flag. */
42+
public boolean elemNsAny;
4143
/** Default element/type namespace. */
4244
public byte[] elemNS;
4345
/** Direct element constructor default element/type namespace (differs from above if "fixed"). */
@@ -92,13 +94,16 @@ public StaticContext(final QueryContext qc) {
9294
* @throws QueryException query exception
9395
*/
9496
void namespace(final String prefix, final String uri) throws QueryException {
95-
if(prefix.isEmpty()) {
96-
elemNS = uri.isEmpty() ? null : token(uri);
97+
final byte[] prefix1 = token(prefix);
98+
final byte[] u = token(uri);
99+
if(prefix1.length == 0) {
100+
elemNsAny = Token.eq(u, QueryText.ANY_URI);
101+
elemNS = elemNsAny || u.length == 0 ? null : u;
97102
dirNS = elemNS;
98-
} else if(uri.isEmpty()) {
99-
ns.delete(token(prefix));
103+
} else if(u.length == 0) {
104+
ns.delete(prefix1);
100105
} else {
101-
ns.add(token(prefix), token(uri), null);
106+
ns.add(prefix1, u, null);
102107
}
103108
}
104109

basex-core/src/main/java/org/basex/query/func/inspect/InspectStaticContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ public Value value(final QueryContext qc) throws QueryException {
5252
yield mb.map();
5353
}
5454
case ELEMENT_NAMESPACE ->
55-
sctx.elemNS == null ? Empty.VALUE : Uri.get(sctx.elemNS);
55+
sctx.elemNsAny ? Uri.get(ANY_URI) : sctx.elemNS == null ? Empty.VALUE : Uri.get(sctx.elemNS);
5656
case FUNCTION_NAMESPACE ->
57-
sctx.funcNS == null ? Str.get(QueryText.FN_URI) : Uri.get(sctx.funcNS);
57+
sctx.funcNS == null ? Str.get(FN_URI) : Uri.get(sctx.funcNS);
5858
case COLLATION ->
5959
Uri.get(sctx.collation == null ? COLLATION_URI : sctx.collation.uri());
6060
case ORDERING ->

basex-core/src/main/java/org/basex/query/util/parse/QNmCheck.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

basex-core/src/main/java/org/basex/query/util/parse/QNmResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void resolve(final QueryParser qp, final int npos, final byte[] elemNS)
6464
entry.name.uri(qp.qc.ns.resolve(entry.name.prefix(), qp.sc));
6565
if(npos == 0 && !entry.name.hasURI())
6666
throw qp.error(NOURI_X, entry.info, entry.name.prefix());
67-
} else if(entry.nsElem && !Token.eq(elemNS, QueryText.ANY_URI)) {
67+
} else if(entry.nsElem) {
6868
entry.name.uri(elemNS);
6969
}
7070
if(entry.name.hasURI()) entries.remove(i);

basex-core/src/test/java/org/basex/query/NamespaceTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,10 +729,16 @@ public final class NamespaceTest extends SandboxTest {
729729
// unprefixed type names => xs:*
730730
query(defAny + "42 instance of integer", true);
731731
query(defAny + "'42' cast as integer", 42);
732+
error(defAny + "declare type t as xs:integer; 42 instance of t", TYPERESERVED_X);
732733

733734
// other contexts fall back to no namespace
734735
query(defAny + "namespace-uri(element a {})", "");
735736
query(defAny + "<r xmlns:x='X'><a/><x:a/></r>/Q{}a", "<a/>");
737+
query(defAny + "declare option output:method 'text';serialize(<x>42</x>, "
738+
+ "{'cdata-section-elements': 'x'})", "<x><![CDATA[42]]></x>");
739+
740+
// inspect:static-context reports ##any
741+
query(defAny + "inspect:static-context((), 'element-namespace')", "##any");
736742

737743
// with fixed + ##any, constructor xmlns must not suppress ##any behavior
738744
query("declare fixed default element namespace '##any'; " +

0 commit comments

Comments
 (0)