@@ -539,14 +539,15 @@ private boolean defaultNamespaceDecl(final boolean fixed) throws QueryException
539539 final boolean elem = wsConsumeWs (ELEMENT );
540540 if (!elem && !wsConsumeWs (FUNCTION )) return false ;
541541 wsCheck (NAMESPACE );
542- final byte [] uri = uriLiteral ();
542+ final byte [] uri = elem ? uriLiteral ( ANY_URI ) : uriLiteral ();
543543 if (eq (XML_URI , uri )) throw error (BINDXMLURI_X_X , uri , XML );
544544 if (eq (XMLNS_URI , uri )) throw error (BINDXMLURI_X_X , uri , XMLNS );
545545
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 );
@@ -697,7 +698,6 @@ private void schemaImport() throws QueryException {
697698 }
698699 final byte [] uri = uriLiteral ();
699700 if (prefix != null && uri .length == 0 ) throw error (NSEMPTY );
700- if (!Uri .get (uri ).isValid ()) throw error (INVURI_X , uri );
701701 addLocations (new TokenList ());
702702 throw error (IMPLSCHEMA );
703703 }
@@ -715,7 +715,6 @@ private void moduleImport() throws QueryException {
715715
716716 final byte [] uri = uriLiteral ();
717717 if (uri .length == 0 ) throw error (NSMODURI );
718- if (!Uri .get (uri ).isValid ()) throw error (INVURI_X , uri );
719718 if (sc .imports .contains (token (uri ))) throw error (DUPLMODULE_X , uri );
720719 sc .imports .add (uri );
721720
@@ -754,8 +753,7 @@ private boolean addLocations(final TokenList list) throws QueryException {
754753 if (add ) {
755754 do {
756755 final byte [] uri = uriLiteral ();
757- if (!Uri .get (uri ).isValid () || IO .get (string (uri )) instanceof IOContent )
758- throw error (INVURI_X , uri );
756+ if (IO .get (string (uri )) instanceof IOContent ) throw error (INVURI_X , uri );
759757 list .add (uri );
760758 } while (wsConsume ("," ));
761759 }
@@ -955,7 +953,7 @@ private static boolean reserved(final QNm name) {
955953 * @throws QueryException query exception
956954 */
957955 private void typeDecl (final AnnList anns ) throws QueryException {
958- final QNm qn = eQName (sc .elemNS , TYPENAME );
956+ final QNm qn = eQName (sc .elemNsAny ? XS_URI : sc . elemNS , TYPENAME );
959957 if (declaredTypes .contains (qn )) throw error (DUPLTYPE_X , qn .string ());
960958 if (NSGlobal .reserved (qn .uri ())) throw error (TYPERESERVED_X , qn .string ());
961959 wsCheck (AS );
@@ -2459,12 +2457,14 @@ private ExprInfo simpleNodeTest(final Kind kind, final boolean all) throws Query
24592457 } else {
24602458 NameTest .Scope scope = NameTest .Scope .FULL ;
24612459 if (!name .hasPrefix ()) {
2460+ pos = p ;
24622461 if (consume (":*" )) {
24632462 // name test: prefix:*
24642463 name = new QNm (concat (name .string (), cpToken (':' )));
24652464 scope = NameTest .Scope .URI ;
24662465 } else if (!eqName ) {
2467- scope = NameTest .Scope .FLEXIBLE ;
2466+ scope = kind == Kind .ELEMENT && sc .elemNsAny ? NameTest .Scope .LOCAL
2467+ : NameTest .Scope .FLEXIBLE ;
24682468 }
24692469 }
24702470 // name test: prefix:name, name, Q{uri}name
@@ -2875,11 +2875,15 @@ private byte[] stringLiteral() throws QueryException {
28752875
28762876 /**
28772877 * Parses the "URILiteral" rule.
2878+ * @param special tokens that are allowed though not valid URI literals
28782879 * @return query expression
28792880 * @throws QueryException query exception
28802881 */
2881- private byte [] uriLiteral () throws QueryException {
2882- return normalize (stringLiteral ());
2882+ private byte [] uriLiteral (final byte []... special ) throws QueryException {
2883+ final byte [] uri = normalize (stringLiteral ());
2884+ for (final byte [] sp : special ) if (eq (uri , sp )) return uri ;
2885+ if (!Uri .get (uri ).isValid ()) throw error (INVURI_X , uri );
2886+ return uri ;
28832887 }
28842888
28852889 /**
@@ -2895,6 +2899,7 @@ private byte[] bracedURILiteral() throws QueryException {
28952899 entity (token );
28962900 }
28972901 final byte [] ns = normalize (token .toArray ());
2902+ if (!Uri .get (ns ).isValid ()) throw error (INVURI_X , ns );
28982903 if (eq (ns , XMLNS_URI )) {
28992904 pos = p ;
29002905 throw error (ILLEGALEQNAME_X , ns );
@@ -3493,7 +3498,7 @@ private SeqType castTarget() throws QueryException {
34933498 if (wsConsume ("(" )) {
34943499 type = choiceItemType ().type ;
34953500 } else {
3496- final QNm name = eQName (sc .elemNS , TYPEINVALID );
3501+ final QNm name = eQName (sc .elemNsAny ? XS_URI : sc . elemNS , TYPEINVALID );
34973502 if (!name .hasURI () && eq (name .local (), token (ENUM ))) {
34983503 if (!wsConsume ("(" )) throw error (WHICHCAST_X , BasicType .similar (name ));
34993504 type = enumerationType ();
@@ -3582,8 +3587,8 @@ private SeqType itemType() throws QueryException {
35823587 }
35833588 }
35843589 } else {
3585- // attach default element namespace
3586- if (!name .hasURI ()) name .uri (sc .elemNS );
3590+ // attach default element namespace, or schema namespace if default element namespace is ##any
3591+ if (!name .hasURI ()) name .uri (sc .elemNsAny ? XS_URI : sc . elemNS );
35873592 // basic type
35883593 type = BasicType .get (name , false );
35893594 // declared type
0 commit comments