XQuery 1.0:XML查询语言 工作草案-4

TransWiki - W3CHINA.ORG开放翻译计划(OTP)

摘要_文档状态_目录 第1节 第2节 3.1~3.3节 3.4~3.6节 3.7节 3.8~3.13节 第4节 附录A 附录B,C,D 附录E,F,G,H,I


目录

4 Modules and Prologs 模块与序言










[1]    Module (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Module)    ::=    VersionDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-VersionDecl)? (MainModule (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-MainModule) | LibraryModule (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-LibraryModule))
[3]    MainModule (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-MainModule)    ::=    Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Prolog) QueryBody (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-QueryBody)
[4]    LibraryModule (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-LibraryModule)    ::=    ModuleDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ModuleDecl) Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Prolog)
[6]    Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Prolog)    ::=    (Setter (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Setter) Separator (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Separator))* ((Import (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Import) | NamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-NamespaceDecl) | VarDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-VarDecl) | FunctionDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-FunctionDecl)) Separator (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Separator))*
[7]    Setter (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Setter)    ::=    XMLSpaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-XMLSpaceDecl) | DefaultCollationDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DefaultCollationDecl) | BaseURIDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-BaseURIDecl) | ConstructionDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ConstructionDecl) | DefaultNamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DefaultNamespaceDecl) | DefaultOrderingDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-DefaultOrderingDecl)
[8]    Import (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Import)    ::=    SchemaImport (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-SchemaImport) | ModuleImport (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ModuleImport)
[9]    Separator (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-Separator)    ::=    ";"
[25]    QueryBody (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QueryBody)    ::=    Expr (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Expr)

[Definition: A query can be assembled from one or more fragments called modules. Each module is either a main module (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-main-module) or a library module (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-library-module).] Each module, in turn, consists of one or more parts called module resources (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource). [Definition: Each module resource is a fragment of XQuery code that conforms to the Module (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Module) grammar and can independently undergo the static analysis phase (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-analysis) described in 2.2.3 Expression Processing.]

[定义:一个查询可以由一个或多个称为模块(modules)的片断装配而成。每个模块是一个主模块(main module)或者库模块(library module)。]每个模块依次由一个或多个称为模块资源(module resources)的部分组成。[定义:每个模块资源(module resource)是一段XQuery代码,符合模块(Module)语法并且能独立地经过2.2.3 Expression Processing中说明的静态分析阶段(static analysis phase)。]

[Definition: A main module consists of single module resource containing a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) followed by a Query Body (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-queryBody).] A query has exactly one main module. In a main module, the Query Body (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-queryBody) can be evaluated, and its value is the result of the query.

[定义:一个主模块(main module)由包含一个序言(Prolog)及其后的一个查询体(Query Body)的单个模块资源构成。] 一个查询有一个确定的主模块。在一个主模块中,查询体(Query Body)能被计算,其值是查询的结果。

[Definition: A module that does not contain a Query Body (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-queryBody) is called a library module.] A library module may consist of multiple module resources (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource), each containing a module declaration (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-declaration) followed by a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog). Multiple module resources are identified as being parts of the same library module if they name the same target namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-target-namespace) in their module declarations. A library module cannot be evaluated directly; instead, it provides function and variable declarations that can be imported into other modules.

[定义:不包含查询体(Query Body)的模块称为库模块(library module)。] 一个库模块可能由多个模块资源(module resources)组成,每个模块资源包含一个模块声明(module declaration)及其后的序言(Prolog)。多个模块资源如果在其模块声明中命名同样的目标名字空间(target namespace),则被被视为同一库模块的部分。一个库模块不能直接被计算;相反的,它提供可以被导入到其他模块的函数和变量声明。

No module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) may contain both a module declaration (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-declaration) and a Query Body (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-queryBody).

没有模块资源(module resource)能既包含模块声明(module declaration)又包含查询体(Query Body)。

[Definition: A Prolog is a series of declarations and imports that define the processing environment for the module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) that contains the Prolog.] Each declaration or import is followed by a semicolon. [Definition: Certain declarations in a Prolog are called setters because they set the value of some property that influences query processing, such as construction mode, ordering mode, or default collation.] These setters, if present, must occur at the beginning of the Prolog, in any order. A Prolog may also include imports of schemas and modules, and declarations of namespace prefixes, variables, and functions. [Definition: Each imported schema or module is identified by its target namespace, which is the namespace of the objects (such as elements or functions) that are defined by the schema or module.]

[定义:序言(Prolog)是一连串的声明和导入,为包含序言的模块资源(module resource)定义处理环境] 每个声明或者导入跟着一个分号。[定义:序言中的某些声明称为给定器(setters),因为他们设置某些影响查询处理的性质的值,例如构造模式、排序模式、或者缺省校对。] 这些给定器如果存在,必须出现在序言的开始,以任何次序。一个序言也可以包含模式和模块导入,和名字空间前缀、变量和函数的声明。 [定义:每个导入模式或模块由其目标命名空间(target namespace)识别, 就是由模式或模块定义的对象(如元素或函数)的命名空间。]

[Definition: The Query Body, if present, consists of an expression that defines the result of the query.] Evaluation of expressions is described in 3 Expressions. A module can be evaluated only if it has a Query Body.

[定义:查询体(Query Body)如果存在,由一个规定查询结果的表达式组成。] 表达式的计算在中3 Expressions说明。一个模块只有当它有查询体时才被计算。

4.1 Version Declaration 版本声明



[2]    VersionDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-VersionDecl)    ::=    "xquery" "version" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral) Separator (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Separator)

[Definition: Any module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) may contain a version declaration. If present, the version declaration occurs at the beginning of the module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) and identifies the applicable XQuery syntax and semantics for the module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource).] The version number "1.0" indicates the requirement that the module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) must be processed by an XQuery Version 1.0 processor. If the version declaration is not present, the version is presumed to be "1.0". An XQuery implementation must raise a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0031 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0031)] when processing a module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) labeled with a version that the implementation does not support. It is the intent of the XQuery working group to give later versions of this specification numbers other than "1.0", but this intent does not indicate a commitment to produce any future versions of XQuery, nor if any are produced, to use any particular numbering scheme.
The following is an example of a version declaration:



[定义:任意模块资源(module resource)可以包含一个版本声明(version declaration)。如果存在,版本声明出现在模块资源(module resource)的开始,且为模块资源(module resource)识别可用的XQuery语法和语义。] 版本号“1.0”表明模块资源(module resource)必须由一个XQuery版本1.0处理器来处理的需求。如果版本声明不存在,则版本被假设为“1.0”。当一个XQuery实现处理一个用其不支持的版本标志的模块资源(module resource)时,必须返回一个静态错误(static error)。[err:XQ0031] XQuery工作组这样做的意图是给出这个规范数字不同于“1.0”的随后的版本,但是这个意图不表明承诺提出任何XQuery的未来版本,如果提出来也不一定使用任一特定的数字模式。

下面是一个版本声明的例子:

xquery version "1.0";


4.2 Module Declaration 模块声明



[5]    ModuleDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-ModuleDecl)    ::=    "module" "namespace" NCName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NCName) "=" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral) Separator (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-Separator)

[Definition: A module declaration serves to identify a module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) as part of a library module (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-library-module). A module declaration consists of the keyword module followed by a namespace prefix and a string literal which must contain a valid URI. [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)]] The URI identifies the target namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-target-namespace) of the library module, which is the namespace for all variables and functions exported by the library module. The name of every variable and function declared in a library module must have a namespace URI that is the same as the target namespace of the module.[err:XQ0048 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0048)]

[定义:模块声明(module declaration)用于标识一个模块资源(module resource)为一个库模块(library module)的一部分。一个模块声明由关键字module及其后一个命名空间前缀和一个包含合法的URI的串文字组成。[err:XQ0046]] URI标识库模块的目标命名空间(target namespace),这是由这个库文件导入的所有变量和函数的命名空间。库模块中声明的每一个变量和函数的名字必须有一个与模块的目标命名空间同样的命名空间URI。[err:XQ0048]

Any module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) may import a library module by means of a module import (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-import) that specifies the target namespace of the library module to be imported. When a module imports one or more library modules, the variables and functions declared in the imported modules are added to the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) and (where applicable) to the dynamic context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-dynamic-context) of the importing module.

任意一个模块资源(module resource)都可以以模块导入(module import)的方式导入一个库模块,模块导入(module import)指定要导入的库模块的目标命名空间。当一个模块导入一或多个库模块时,被导入模块的变量和函数声明将加到此模块的静态语境(static context)和(如果可用)动态语境(dynamic context)中。

The following is an example of a module declaration:

下面是一个模块声明的例子:


module namespace math = "http://example.org/math-functions";


4.3 Xmlspace Declaration Xmlspace声明



[11]    XMLSpaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-XMLSpaceDecl)    ::=    "declare" "xmlspace" ("preserve" | "strip")

[Definition: The xmlspace declaration in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) controls whether boundary whitespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-boundary-whitespace) is preserved by direct element constructors (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-direct-elem-const) during execution of the query.] If xmlspace preserve is specified, boundary whitespace is preserved. If xmlspace strip is specified or if no xmlspace declaration is present, boundary whitespace is stripped (deleted). A further discussion of whitespace in constructed elements can be found in 3.7.1.4 Whitespace in Element Content.


[定义:序言(Prolog)中的xmlspace声明(xmlspace declaration)控制边界空格(boundary whitespace)是否被直接元素构造器(direct element constructors)在查询执行时保留。] 如果指定xmlspace preserve,则边界空格被保留。如果指定xmlspace strip或者不存在xmlspace声明,边界空格被去除(删除)。构造元素中空格的进一步说明可以在3.7.1.4 Whitespace in Element Content中找到。

The following example illustrates an xmlspace declaration:


下面是一个xmlspace声明的例子:


declare xmlspace preserve;


4.4 Default Collation Declaration 缺省校对声明



[14]    DefaultCollationDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DefaultCollationDecl)    ::=    "declare" "default" "collation" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral)

A Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) may declare a default collation (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-collation), which is the name of the collation to be used by functions and operators that require a collation if no other collation is specified. For example, the gt operator on strings is defined by a call to the fn:compare function, which takes an optional collation parameter. Since the gt operator does not specify a collation, the fn:compare function implements gt by using the default collation specified in the Prolog. The default collation is identified by a literal string which must contain a valid URI. [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)]


序言(Prolog)会声明一个缺省校对(default collation),这是如果没有其他指定校对时,要求校对的函数和操作符使用的校对的名字。例如,作用于字符串的操作符gt由调用函数fn:compare来定义,其带有可选的校对参数。既然操作符gt没有指定一个校对,函数fn:compare使用在序言中指定的缺省校对执行。缺省校对通过一个必须包含一个合法URI的文字串识别。[err:XQ0046]

If a Prolog specifies no default collation, the Unicode codepoint collation (http://www.w3.org/2004/07/xpath-functions/collation/codepoint) is used unless the implementation provides a different default collation.


如果一个序言没有指定缺省校对,则使用Unicode codepoint collation (http://www.w3.org/2004/07/xpath-functions/collation/codepoint)除非实现提供一个不同的缺省校对。

The following example illustrates a default collation declaration:


下面是一个缺省校对声明的例子:


declare default collation

         "http://example.org/languages/Icelandic";


If a Prolog specifies more than one default collation, or the value specified does not identify a collation known to the implementation, a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised.[err:XQ0038 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0038)]


如果一个序言指定一个以上的缺省声明,或者指定值不能被一个实现识别为已知校对,将引发一个静态错误(static error)。[err:XQ0038]

4.5 Base URI Declaration 基URI声明



[15]    BaseURIDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-BaseURIDecl)    ::=    "declare" "base-uri" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral)

A base URI declaration specifies the base URI property of the static context, which is used when resolving relative URIs within a module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource). A static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0032 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0032)] is raised if more than one base URI declaration is found in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog). A static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)] is raised if the string literal in a base URI declaration does not contain a valid URI. Note that the fn:doc function resolves a relative URI using the base URI of the calling module.

基URI声明(base URI declaration)指定静态语境的基URI(base URI)性质,在解析模块资源(module resource)内的相对URI时被使用。如果一个序言(Prolog)中发现一个以上的基URI声明,将引发一个静态错误(static error)[err:XQ0032]。 如果一个基URI声明中的串文字不包含一个合法URI,将引发一个静态错误(static error)[err:XQ0046]。注意,函数fn:doc使用调用模块的基URI解析相对URI。

The following is an example of a base URI declaration:

下面是一个基URI声明的例子:


declare base-uri "http://example.org";


4.6 Construction Declaration 构造声明



[20]    ConstructionDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-ConstructionDecl)    ::=    "declare" "construction" ("preserve" | "strip")

A construction declaration in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) sets the construction mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-construction-mode) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) to preserve or strip. The construction mode governs the behavior of element constructors. If construction mode is preserve, the type of a constructed element node is xs:anyType, and the attributes and descendants of the constructed element retain their original types. If construction mode is strip, the type of the constructed element node and all its descendants is xdt:untyped, and attributes of the constructed element have type xdt:untypedAtomic. Element constructors are described in 3.7.1 Direct Element Constructors and 3.7.3.1 Computed Element Constructors.

序言(Prolog)中的构造声明(construction declaration)设置静态语境(static context)中的构造模式(construction mode)为preserve或strip。构造模式支配元素构造器的行为。如果构造模式为preserve,则一个构造元素节点为xs:anyType,而且这个构造元素节点的属性和子孙节点保持其原来的类型。如果构造模式为strip,构造元素节点及其所有子节点的类型为xdt:untyped, 构造元素的属性类型为xdt:untypedAtomic. 元素构造器在3.7.1 Direct Element Constructors和3.7.3.1 Computed Element Constructors中说明。

The following example illustrates a construction declaration:

下例说明一个构造声明:


declare construction strip;


If a Prolog specifies more than one construction declaration, a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised.[err:XQ0067 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0067)]

如果一个序言指定多于一个构造声明,将会引发一个静态错误(static error) [err:XQ0067]

4.7 Default Namespace Declaration 缺省命名空间声明



[12]    DefaultNamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DefaultNamespaceDecl)    ::=    (("declare" "default" "element") | ("declare" "default" "function")) "namespace" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral)

Default namespace declarations can be used in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) to facilitate the use of unprefixed QNames. The string literal used in a default namespace declaration must be a valid URI or a zero-length string. [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)] The following kinds of default namespace declarations are supported:

缺省命名空间声明(Default namespace declarations)用在序言(Prolog)中,来使没有前缀的QName便于使用。在缺省命名空间声明中使用的串文字必须是一个合法的URI或者一个零长串。[err:XQ0046] 下列种类的缺省命名空间声明是支持的:


A default element/type namespace declaration declares a namespace URI that is associated with unprefixed names of elements and types. This declaration is recorded as the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context). A Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) may contain at most one default element/type namespace declaration.[err:XQ0066 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0066)] If the StringLiteral in a default element/type namespace declaration is a zero-length string, the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns) is set to "none," and unprefixed names of elements and types are considered to be in no namespace. The following example illustrates the declaration of a default namespace for elements and types:

• 缺省元素/类型命名空间声明(default element/type namespace declaration)声明一个与无前缀的元素和类型名关联的命名空间URI。此声明在静态语境(static context)中记录为缺省元素/类型命名空间(default element/type namespace)。一个序言(Prolog)最多只可以包含一个缺省元素/类型命名空间声明。[err:XQ0066] 如果缺省元素/类型命名空间声明中的串文字(StringLiteral)是一个零长串,缺省元素/类型命名空间(default element/type namespace)被设为“none,”且元素和类型的无前缀名字被认为不在命名空间中。下面的例子说明一个缺省元素/类型命名空间的声明:


declare default element namespace "http://example.org/names";


A default element/type namespace declaration may be overridden by a namespace declaration attribute (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr) in a direct element constructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-direct-elem-const) or by a local namespace declaration (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-local-namespace-decl) in a computed element constructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-computed-elem-const).

缺省元素/类型命名空间声明可能被直接元素构造器(direct element constructor)中的命名空间声明属性(namespace declaration attribute)或者计算元素构造器(computed element constructor)中的本地命名空间声明(local namespace declaration)覆盖。


A default function namespace declaration declares a namespace URI that is associated with unprefixed names of functions in function calls. This declaration is recorded as the default function namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-fn-ns) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context). A Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) may contain at most one default function namespace declaration.[err:XQ0066 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0066)] If the StringLiteral in a default function namespace declaration is a zero-length string, a static error is raised. [err:XQ0063 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0063)] If no default function namespace is declared, the default function namespace is the namespace of XPath/XQuery functions, http://www.w3.org/2004/07/xpath-functions (however, this default may be overridden by an implementation.) The following example illustrates the declaration of a default function namespace:

• 缺省函数命名空间声明(default function namespace declaration)声明一个与函数调用中的无前缀函数名相关联的命名空间URI。 此声明在静态语境(static context)中记录为缺省函数命名空间(default function namespace)。一个序言(Prolog)最多只可以包含一个缺省函数命名空间声明 [err:XQ0066] 。 如果一个缺省函数命名空间声明中串文字(StringLiteral)为一零长串,将引发一个静态错误 [err:XQ0063] 。 如果没有声明缺省函数命名空间,则缺省函数命名空间为XPath/XQuery函数的命名空间,http://www.w3.org/2004/07/xpath-functions (然而,这个缺省值可能被一个实现覆盖。)下面的例子说明缺省函数命名空间的声明:


declare default function namespace

     "http://example.org/math-functions";


The effect of declaring a default function namespace is that all functions in the default function namespace, including implicitly-declared constructor functions, are aliased with a name that has the original local name, but no namespace URI. The function may then be called using either its original name or its alias—that is, the namespace prefix becomes optional. When a function call uses a function name with no prefix, the local name of the function must match a function (including implicitly-declared constructor functions) in the default function namespace.[err:XP0017 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0017)]

声明一个缺省函数命名空间的效果是,缺省函数命名空间中包括隐含声明的构造函数在内的所有函数,都以具有原来的本地名但是没有命名空间URI的名字为别名。于是函数可以用其原来的名字或者它的别名调用——就是说,命名空间前缀成为可选项。当一个函数调用使用一个没有前缀的函数名时,这个函数的本地名必须和一个在缺省函数命名空间中的函数(包括隐含声明的构造函数)相匹配。[err:XP0017]

Unprefixed attribute names and variable names are in no namespace.

无前缀属性名和变量名不属于命名空间。

4.8 Default Ordering Declaration 缺省排序声明



[13]    DefaultOrderingDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-DefaultOrderingDecl)    ::=    "declare" "ordering" ("ordered" | "unordered")

[Definition: A default ordering declaration sets the ordering mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-ordering-mode) in the static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context).] This ordering mode applies to all expressions in a module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) (including both the Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) and the Query Body (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-queryBody), if any), unless overridden by an ordered or unordered expression. A static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0065 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0065)] is raised if more than one default ordering declaration is found in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog).

[定义:缺省排序声明(default ordering declaration)设置静态语境(static context)中的排序模式(ordering mode)。] 这个排序模式应用于一个模块资源(module resource)(包括序言(Prolog)和查询体(Query Body),如果有的话)中的所有表达式,除非被一个ordered 或 unordered表达式覆盖。如果一个序言(Prolog)中发现多于一个的缺省排序声明,将引发一个静态错误(static error)[err:XQ0065].

Ordering mode (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-ordering-mode) affects the behavior of path expressions, union, intersect, and except expressions, and FLWOR expressions that have no order by clause. If ordering mode is ordered, node sequences returned by path, union, intersect, and except expressions are in document order (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-document-order); otherwise the order of these return sequences is implementation-dependent (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-implementation-dependent). The effect of ordering mode on FLWOR expressions is described in 3.8 FLWOR Expressions.

排序模式(Ordering mode)影响没有order by子句的路径表达式、union、intersect和、except表达式和FLWOR表达式的行为。如果排序模式为ordered,由路径、union、intersect和、except表达式返回的节点序列为文档顺序(document order);否则,这些返回序列的次序为实现相关的(implementation-dependent)。排序模式对FLWOR表达式的影响在3.8 FLWOR Expressions中说明。

The following example illustrates a default ordering declaration:

下例说明一个缺省排序声明:

declare ordering unordered;


4.9 Schema Import 模式导入




[16]    SchemaImport (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-SchemaImport)    ::=    "import" "schema" SchemaPrefix (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-SchemaPrefix)? StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral) (("at" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral)) ("," StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral))*)?
[17]    SchemaPrefix (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-SchemaPrefix)    ::=    ("namespace" NCName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NCName) "=") | ("default" "element" "namespace")

[Definition: A schema import imports the element, attribute, and type definitions from a schema into the in-scope schema definitions (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-issd).] The schema to be imported is identified by its target namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-target-namespace). The schema import may bind a namespace prefix to the target namespace of the imported schema, or may declare that target namespace to be the default element/type namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-def-elemtype-ns). The schema import may also provide optional hints for locating the schema.

[定义:模式导入(schema import)把元素、属性和类型定义从模式导入到模式定义作用域(in-scope schema definitions)中。] 将被导入的模式由其目标命名空间(target namespace)标识。模式导入会给已导入模式绑定一个命名空间前缀,或者会声明目标命名空间为缺省元素/类型命名空间(default element/type namespace)模式导入也会为模式的定位提供可选提示。

The string literals in a schema import must be valid URIs. [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)] The first of these string literals specifies the target namespace of the schema to be imported. The string literals that follow the at keyword are optional location hints, and can be interpreted or disregarded in an implementation-dependent way. Multiple location hints might be used to indicate more than one possible place to look for the schema or multiple physical resources to be assembled to form the schema.

一个模式导入中的串文字必须是合法的URI。 [err:XQ0046] 这些串文字的开始指定了将要导入的模式的目标命名空间。接在关键字后面的串文字是可选的位置提示,可以以依赖实现的方式被解释或者不予处理。多个位置提示被用来指示多于一个的可能位置,来寻找模式或者多个组成模式的实际资源。

A schema import that specifies a zero-length string as target namespace is considered to import a schema that has no target namespace. Such a schema import may not bind a namespace prefix [err:XQ0057 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0057)], but it may set the default element/type namespace to "no namespace", enabling the definitions in the imported namespace to be referenced. If the default element/type namespace is not set to "no namespace", there is no way to reference the definitions in an imported schema that has no target namespace.

指定一个零长串为目标命名空间的模式导入,被认为是导入一个没有命名空间的模式。这样的模式导入可以不必绑定一个命名空间前缀 [err:XQ0057],但是它会设置缺省元素/类型命名空间为“no namespace”,允许导入的名字空间中的定义被引用。如果缺省元素/类型命名空间没有设置为“no namespace”,就没有办法引用没有目标命名空间的导入模式中的定义。

It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0058 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0058)] if more than one schema import in the same Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) specifies the same target namespace. It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0059 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0059)] if the implementation is not able to process a schema import by finding a valid schema with the specified target namespace. It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0035 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0035)] if multiple imported schemas, or multiple physical resorces within one schema, contain definitions for the same name in the same symbol space (for example, two definitions for the same element name, even if the definitions are consistent). However, it is not an error to import the schema with target namespace http://www.w3.org/2001/XMLSchema (predeclared prefix xs), even though the built-in types defined in this schema are implicitly included in the in-scope type definitions. (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-is-types)

如果同一个序言(Prolog)中多于一个的模式导入指定同一个目标命名空间,将引发静态(static error)错误。[err:XQ0058] 如果实现不能通过查找一个具有指定目标命名空间的合法的模式处理一个模式导入,将引发静态错误(static error)[err:XQ0059]。如果多个导入的模式或者多个同一模式内的实际资源,包含对相同符号空间(例如,对同样的元素名的两个定义,甚至是一致的定义)中相同名字的定义,将引发一个静态错误(static error)[err:XQ0035]。然而,以目标命名空间http://www.w3.org/2001/XMLSchema(预定义前缀xs)导入的模式,不产生错误,即使这个模式中定义的内部类型隐含的包括在类型定义作用域(in-scope type definitions)中。

The following example imports the schema for an XHTML document, specifying both its target namespace and its location, and binding the prefix xhtml to this namespace:

下面的例子为一个XHTML文档导入模式,指定其目标命名空间及其位置,并绑定前缀xhtml到此命名空间:


import schema namespace xhtml="http://www.w3.org/1999/xhtml"

   at "http://example.org/xhtml/xhtml.xsd";


The following example imports a schema by specifying only its target namespace, and makes it the default element/type namespace:

下面的例子通过仅仅指定其目标命名空间导入一个模式,并使其成为默认元素/类型命名空间:


import schema default element namespace "http://example.org/abc";


The following example imports a schema that has no target namespace, providing a location hint, and sets the default element/type namespace to "no namespace" so that the definitions in the imported schema can be referenced:

下面的例子导入一个没有目标命名空间的模式,提供一个位置提示,并设置缺省元素/类型命名空间为“no namespace”以便导入模式中的定义可以被引用:


import schema default element namespace ""
at "http://example.org/xyz.xsd";


The following example imports a schema that has no target namespace and sets the default element/type namespace to "no namespace". Since no location hint is provided, it is up to the implementation to find the schema to be imported.

下面的例子导入一个没有目标命名空间的模式,并设置缺省元素/类型命名空间为“no namespace”。由于没有提供位置提示,它可用于实现来查找被导入的模式。


import schema default element namespace "";


4.10 Module Import 模块导入



[18]    ModuleImport (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-ModuleImport)    ::=    "import" "module" ("namespace" NCName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NCName) "=")? StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral) (("at" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral)) ("," StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral))*)?

[Definition: A module import imports the function declarations and variable declarations from a library module (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-library-module) into the function signatures (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-function-signature) and in-scope variables (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-variables) of the importing module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource).] The module to be imported is identified by its target namespace (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-target-namespace). The module import may bind a namespace prefix to the target namespace of the imported module, and it may provide optional hints for locating the module resources (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) of the imported module.

[定义:模块导入(module import)从一个库模块(library module)导入函数声明和变量声明到被导入模块资源(module resource)的函数签名(function signatures)和变量作用域(in-scope variables)中。] 要被导入的模块由其目标命名空间(target namespace)确定。模块导入会把一个命名空间前缀与被导入模块的目标命名空间绑定,而且它会为定位被导入模块的模块资源(module resources)提供可选的提示。

The string literals in a module import must be valid URIs. [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)] The first of these string literals specifies the target namespace of the module to be imported. The string literals that follow the at keyword are optional location hints, and can be interpreted or disregarded in an implementation-defined (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-implementation-defined) way. If the imported module consists of multiple module resources (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource), all the function and variable declarations in those module resources are imported.

一个模块导入中的串文字必须是合法的URI。[err:XQ0046] 这些串文字的开始指定了被导入模块的目标命名空间。在关键字后面的串文字为可选的定位提示,能够以一种实现定义(implementation-defined)的方式被解释或者不予处理。如果被导入模块由多个模块资源(module resources)组成,这些模块资源中的所有函数和变量声明都将被导入。

It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0047 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0047)] if more than one module import in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) specifies the same target namespace. It is a static error [err:XQ0056 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0056)] if the target namespace of the module to be imported is the same as the target namespace of the importing module. It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0059 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0059)] if the implementation is not able to process a module import by finding a valid module definition with the specified target namespace. It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) if multiple imported modules, or multiple module resources (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) within an imported module, contain definitions for the same name in the same symbol space (for example, two definitions for the same function, even if the definitions are consistent).[err:XQ0034 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0034)][err:XQ0049 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0049)]

如果在一个序言(Prolog)中有多于一个的模块导入指定同一个目标命名空间,将引发一个静态错误(static error)[err:XQ0047]。如果被导入模块和导入模块的目标命名空间相同,将引发一个静态错误[err:XQ0056] 。如果实现不能通过查找一个由指定目标命名空间定义的合法模块,来处理一个模块导入,将引发静态错误(static error)[err:XQ0059]。如果多个被导入模块或者一个被导入模块内的多个模块资源(module resources),包含对同一符号空间(例如,同一函数的两个定义,甚至是一致的定义)的同一名字的定义,将引发一个静态错误(static error)[err:XQ0034][err:XQ0049]。

Each module resource (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-resource) has its own static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context). A module import (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-module-import) imports only functions and variable declarations; it does not import other objects from the imported module, such as its in-scope schema definitions (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-issd) or statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces). Module imports are not transitive—that is, importing a module provides access only to function and variable declarations contained directly in the imported module. For example, if module A imports module B, and module B imports module C, module A does not have access to the functions and variables declared in module C. Two modules may import each other.

每个模块资源(module resource)有其自身的静态语境(static context)。一个模块导入(module import)仅导入函数和变量声明;不从被导入模块中导入其他对象,比如其模式定义作用域(in-scope schema definitions)或者静态已知命名空间(statically known namespaces)。模块导入不可传递——就是说,导入一个模块只提供对直接包含在被导入模块中的函数及变量声明的访问。例如,如果模块A导入模块B,并且模块B导入模块C,模块A没有访问C中声明的函数和变量的权力。两个模块可以互相导入。

It is a type error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-type-error) [err:XQ0036 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0036)] to import a module if the importing module's in-scope type definitions (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-is-types) do not include definitions for the type names that appear in variable declarations, function parameters, or function returns found in the imported module. It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XQ0037 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0037)] to import a module that contains function declarations or variable declarations whose names are already declared in the static context of the importing module.

如果导入模块的类型定义作用域(in-scope type definitions)不包括被导入模块中出现的变量声明、函数参数或函数返回值的类型名的定义,这样的模块导入会引发一个类型错误(type error)[err:XQ0036]。导入一个包含其名字已经在导入模块的静态语境中声明过的函数声明或者变量声明的模块,是一个静态错误(static error)[err:XQ0037]。

To illustrate the above rules, suppose that a certain schema defines a type named triangle. Suppose that a library module imports the schema, binds its target namespace to the prefix geometry, and declares a function with the following function signature (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-function-signature): math:area($t as geoetry:triangle) as xs:double. If a query wishes to use this function, it must import both the library module and the schema on which it is based. Importing the library module alone would not provide access to the definition of the type geometry:triangle used in the signature of the area function.

为了说明上述规则,假定某一个模式定义了一个类型名triangle。假定一个库模块导入此模式,将其目标命名空间与前缀geometry绑定,并用下面的函数签名(function signature)声明了一个函数:math:area($t as geoetry:triangle) as xs:double。如果查询希望使用这个函数,它必须既导入库模块,又导入库文件基于的模式。仅仅导入库模块不提供对在函数area的签名中使用的geometry:triangle类型定义的访问。

The following example illustrates a module import:

下面的例子说明一个模块导入:


import module namespace math = "http://example.org/math-functions";


4.11 Namespace Declaration 命名空间声明



[10]    NamespaceDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NamespaceDecl)    ::=    "declare" "namespace" NCName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-NCName) "=" StringLiteral (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-StringLiteral)

[Definition: A namespace declaration declares a namespace prefix and associates it with a namespace URI, adding the (prefix, URI) pair to the set of statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces).] The string literal used in a namespace declaration must be a valid URI [err:XQ0046 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0046)] or a zero-length string. The namespace declaration is in scope throughout the query in which it is declared, unless it is overridden by a namespace declaration attribute (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr) in a direct element constructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-direct-elem-const) or by a local namespace declaration (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-local-namespace-decl) in a computed element constructor (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-computed-elem-const).
The following query illustrates a namespace declaration:

[定义:命名空间声明(namespace declaration)说明一个命名空间前缀并将其与一个命名空间URI关联,添加(前缀,URI)对到静态已知命名空间(statically known namespaces)集合中。] 在命名空间声明中使用的串文本必须是一个合法的URI [err:XQ0046] 或者一个零长串。命名空间声明的作用域贯穿其声明的整个查询,除非它被一个直接元素构造器(direct element constructor)中的命名空间声明属性(namespace declaration attribute)或者一个计算元素构造器(computed element constructor)中的本地命名空间声明(local namespace declaration)所覆盖。
下面的查询说明一个命名空间声明:


declare namespace foo = "http://example.org";
<foo:bar> Lentils </foo:bar>


In the query result, the newly created node is in the namespace associated with the namespace URI http://example.org.
Multiple declarations of the same namespace prefix in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) result in a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error).[err:XQ0033 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0033)] However, a declaration of a namespace in a Prolog can override a prefix that has been predeclared in the static context.
It is a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) [err:XP0008 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0008)] to use a QName with a namespace prefix that has not been declared.

在查询结果中,新创建的节点是在与命名空间URI http://example.org相关联的命名空间中的。
一个序言(Prolog)中的同一个命名空间前缀的多个声明,导致一个静态错误(static error)[err:XQ0033]。然而,一个序言中的一个命名空间的声明,能覆盖一个已经在静态语境中声明过的前缀。
使用一个有一个已经被声明过的命名空间前缀的QName,将引发一个静态错误(static error)。[err:XP0008]

XQuery has several predeclared namespace prefixes that are present in the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces) before each query is processed. These prefixes may be used without an explicit declaration. They may be overridden by namespace declarations (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-declaration) in a Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog) or by namespace declaration attributes (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr) or local namespace declarations (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-local-namespace-decl) on constructed elements (except for the prefix xml, which may not be redeclared.) The predeclared namespace prefixes are as follows:

XQuery有几个预声明的命名空间前缀,它们于每个查询被处理前,在静态已知命名空间(statically known namespaces)中给出。这些前缀可以在没有显式声明的情况下使用。它们会被序言(Prolog)中的命名空间声明(namespace declarations)所覆盖,或者被关于构造元素的命名空间属性(namespace declaration attributes)或本地命名空间声明(local namespace declarations)所覆盖(除了前缀xml,它可以不必重新声明)。预声明的命名空间前缀如下:


xml = http://www.w3.org/XML/1998/namespace


xs = http://www.w3.org/2001/XMLSchema


xsi = http://www.w3.org/2001/XMLSchema-instance


fn = http://www.w3.org/2004/07/xpath-functions


xdt = http://www.w3.org/2004/07/xpath-datatypes


local = http://www.w3.org/2004/07/xquery-local-functions (see 4.13 Function Declaration.)
Additional predeclared namespace prefixes may be added to the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces) by an implementation.
The namespace prefix xmlns also has a special significance (it identifies a namespace declaration attribute (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-namespace-decl-attr)), and it may not be redeclared.
If the StringLiteral part of a namespace declaration is a zero-length string, the following rules apply:

附加的预定义命名空间前缀可以由实现加到静态已知命名空间(statically known namespaces)。
命名空间前缀也有一个特殊的意义(它标识一个命名空间声明属性(namespace declaration attribute)),并且它不可以被重新声明。
如果一个命名空间声明的串文字部分为一零长串,则应用下列规则:


If the implementation supports [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)], any existing namespace binding for the given prefix is removed from the statically known namespaces (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-namespaces). This feature provides a way to remove predeclared namespace prefixes such as local.

• 如果实现支持 [XML Names 1.1] ,任何现有的为给定前缀绑定的命名空间从静态已知命名空间(statically known namespaces)中删除。这个特性提供了一个方式,来删除象local这样的预声明命名空间前缀。


If the implementation does not support [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)], a static error is raised. [err:XQ0053 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0053)]

• 如果实现不支持 [XML Names 1.1],引发一个静态错误。[err:XQ0053]


It is implementation-defined whether a given implementation supports [XML Names 1.1 (http://www.w3.org/TR/2004/WD-xquery-20040723/#XMLNAMES11)].

• 一个特定的实现是否支持[XML Names 1.1],是由实现定义的。

When element or attribute names are compared, they are considered identical if the local parts and namespace URIs match on a codepoint basis. Namespace prefixes need not be identical for two names to match, as illustrated by the following example:

当元素或者属性名相比较时,如果它们的本地部分和命名空间URI在码点基础上匹配,则认为相同。两个名字的匹配不必命名空间前缀相同,如下例所示:


declare namespace xx = "http://example.org";

let $i := <foo:bar xmlns:foo = "http://example.org">

              <foo:bing> Lentils </foo:bing>
</foo:bar>

return $i/xx:bing


Although the namespace prefixes xx and foo differ, both are bound to the namespace URI "http://example.org". Since xx:bing and foo:bing have the same local name and the same namespace URI, they match. The output of the above query is as follows.

尽管命名空间前缀xx和foo不同,但都与命名空间URI“http://example.org”绑定。因为xx:bing和foo:bing有相同的本地名和相同的命名空间URI,他们相匹配。上述查询的输出如下:


<foo:bing> Lentils </foo:bing>


4.12 Variable Declaration 变量声明





[19]    VarDecl (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-VarDecl)    ::=    "declare" "variable" "$" VarName (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-VarName) TypeDeclaration (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-TypeDeclaration)? ((":=" ExprSingle (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-ExprSingle)) | "external")
[136]    VarName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-VarName)    ::=    QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-QName)
[109]    TypeDeclaration (http://www.w3.org/TR/2004/WD-xquery-20040723/#prod-xquery-TypeDeclaration)    ::=    "as" SequenceType (http://www.w3.org/TR/2004/WD-xquery-20040723/#doc-xquery-SequenceType)

A variable declaration adds the static type of a variable to the in-scope variables (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-variables), and may also add a value for the variable to the variable values (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-variable-values). If the expanded QName (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-expanded-qname) of the variable is the same as that of another variable in in-scope variables (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-in-scope-variables), a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised.[err:XQ0049 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0049)]

变量声明(variable declaration)添加一个变量的静态类型到变量作用域(in-scope variables)中,也可以为变量添加一个值到变量值(variable values)。如果变量作用域(in-scope variables)中变量的扩展QName(expanded QName)和另一个变量的相同,将引发一个静态错误(static error)[err:XQ0049]。

If a variable declaration includes a type, that type is added to the static context as the type of the variable. If a variable declaration includes an expression but not an explicit type, the static type of the variable is inferred from the static type of the expression. If a variable declaration includes both a type and an expression, the static type of the expression must be compatible with the declared static type; otherwise a type error is raised.[err:XP0004 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXP0004)]

如果变量声明包含一个类型,那个类型和变量类型一样被添加到静态语境中。如果变量声明中包含一个不是显式类型的表达式,变量的静态类型将通过表达式的静态类型来推断。如果一个变量声明既包含一个类型又包括一个表达式,表达式的静态类型必须与已声明的静态声明兼容;否则将引发一个类型错误[err:XP0004]。

[Definition: If a variable declaration includes an expression, the expression is called an initializing expression.] The initializing expression for a given variable must be evaluated before the evaluation of any expression that references the variable. The static context (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-context) for an initializing expression includes all functions that are declared or imported anywhere in the Prolog (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-prolog), but it includes only those variables and namespaces that are declared or imported earlier in the Prolog than the variable that is being initialized. If an initializing expression cannot be evaluated because of a circularity (for example, it depends on a function that in turn depends on the value of the variable that is being initialized), a static error (http://www.w3.org/TR/2004/WD-xquery-20040723/#dt-static-error) is raised.[err:XQ0054 (http://www.w3.org/TR/2004/WD-xquery-20040723/#ERRXQ0054)]

[定义:如果一个变量声明包含一个表达式,这个表达式称为初始化表达式(initializing expression)。] 一个给定变量的初始化表达式必须在任何一个引用此变量的表达式求值之前被求值。一个初始化表达式的静态语境(static context)包括所有在序言(Prolog)中任何位置声明或导入的函数,但是它只包括那些在序言中声明或导入的比将要被初始化的变量早的变量和命名空间。如果一个初始化表达式由于一个环(例如,它依赖的函数转过来依赖于将要被初始化的变量的值)而不能被求值,将引发一个静态错误(static error)[err:XQ0054]。

If the variable declaration includes the keyword external, a value must be provided for the variable by the external environment before the query can be evaluated. If an external variable declaration also includes a declared type, the value provided by the external environment must match the declared type, using the matching rules in 2.4.4 SequenceType Matching (see 2.2.5 Consistency Constraints). If an external variable declaration does not include a declared type, the type and a