WITH XMLNAMESPACESWITH XMLNAMESPACES

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions)

声明一个或多个 XML 命名空间。Declares one or more XML namespaces.

主题链接图标 Transact-SQL 语法约定Topic link icon Transact-SQL Syntax Conventions

语法Syntax

  
WITH XMLNAMESPACES ( <XML namespace declaration item>  
[ { , <XML namespace declaration item> }...] )   
  
<XML namespace declaration item> ::=  
<xml_namespace_uri> AS <xml_namespace_prefix>  
| <XML default namespace declaration item>  
<xml_namespace_uri> ::= <character string literal>  
  
<xml_namespace_prefix> ::= <identifier>  
  
<XML default namespace declaration item> ::=  
DEFAULT <xml_namespace_uri>  
  

备注

若要查看 SQL Server 2014 及更早版本的 Transact-SQL 语法,请参阅早期版本文档To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.

参数Arguments

xml_namespace_urixml_namespace_uri
统一资源标识符 (URI),用于标识正在声明的 XML 命名空间。A Uniform Resource Identifier (URI) that identifies the XML namespace that is being declared. xml_namespace_uri 是 SQL 字符串。xml_namespace_uri is an SQL string.

xml_namespace_prefixxml_namespace_prefix
指定一个要映射并与在 xml_namespace_uri 中指定的命名空间 URI 值关联的前缀。Specifies a prefix to be mapped and associated with the namespace URI value specified in xml_namespace_uri. xml_namespace_prefix 必须为 SQL ServerSQL Server 标识符。xml_namespace_prefix must be a SQL ServerSQL Server identifier.

备注Remarks

在还包括公用表表达式的语句中使用 WITH XMLNAMESPACES 子句时,WITH XMLNAMESPACES 子句必须位于语句中的公用表表达式的前面。When you use the WITH XMLNAMESPACES clause in a statement that also includes a common table expression, the WITH XMLNAMESPACES clause must precede the common table expression in the statement.

下面是在使用 WITH XMLNAMESPACES 子句时所应用的常规语法规则:The following are general syntax rules that apply when you use the WITH XMLNAMESPACES clause:

  • 每个 XML 命名空间声明都必须包含至少一个 XML 默认命名空间声明项。Each XML namespace declaration must contain at least one XML default namespace declaration item.

  • 所使用的每个 XML 命名空间前缀都必须是非移植名称 (NCName),在这样的名称中冒号字符 (:) 不是名称的一部分。Each XML namespace prefix used must be a non-colonized name (NCName) in which the colon character (:) is not part of the name.

  • 不能两次定义同一个命名空间前缀。You cannot define a namespace prefix two times.

  • XML 命名空间前缀和 URI 是区分大小写的。XML namespace prefixes and URIs are case-sensitive.

  • 不能声明 XML 命名空间前缀 xmlnsThe XML namespace prefix xmlns cannot be declared.

  • 除了命名空间 URI xml 以及不能为其分配不同前缀的此 URI 以外,不能用其他命名空间覆盖 XML 命名空间前缀 'http://www.w3.org/XML/1998/namespace'The XML namespace prefix xml cannot be overridden with a namespace, other than the namespaces URI 'http://www.w3.org/XML/1998/namespace', and this URI that cannot be assigned a different prefix.

  • 当查询正在使用 ELEMENTS XSINIL 指令时,不能重新声明 XML 命名空间前缀 xsiThe XML namespace prefix xsi cannot be redeclared when the ELEMENTS XSINIL directive is being used on the query.

  • 无需声明“http://www.w3.org/2001/XMLSchema-instance”即可使用 xsi 标准命名空间。It is not necessary to declare the 'http://www.w3.org/2001/XMLSchema-instance' to use xsi standard namespace. 如果未指定,则它由 XML/XPATH 处理器隐式添加,并且只要 xml 文档中正确声明了“http://www.w3.org/2001/XMLSchema-instance”架构,xpath 表达式即可使用 xsi 前缀。It is implicitly added by the XML/XPATH processor if not specified and xpath expressions can use the xsi prefix as long as the 'http://www.w3.org/2001/XMLSchema-instance' schema is properly declared in the xml document.

  • URI 字符串值按照当前数据库排序规则代码页进行编码,并且将内部转换为 Unicode。URI string values are encoded according to the current database collation code page and are internally translated to Unicode.

  • XML 命名空间 URI 将按照用于 xs:anyURI 的 XSD 空格折叠规则进行空格折叠。The XML namespace URI will be white-space collapsed following the XSD white-space collapse rules that are used for xs:anyURI. 另外,不会对 XML 命名空间 URI 值执行实体化和反实体化。Also, note that no entitization or deentitization are performed on XML namespace URI values.

  • 系统将检查 XML 命名空间 URI 中是否有无效的 XML 1.0 字符,如果发现这样的字符(例如,U+0007),将引发错误。The XML namespace URI will be checked for XML 1.0 characters that are not valid, and an error will be raised if one is found (such as, U+0007).

  • XML 命名空间 URI(折叠所有空格后)不能是零长度的字符串,否则会发生“无效的空命名空间 URI”错误。The XML namespace URI (after all white space is collapsed) cannot be a zero-length string or an "invalid empty namespace URI" error occurs.

  • XMLNAMESPACES 关键字保留在 WITH 子句的上下文中。The XMLNAMESPACES keyword is reserved in the context of the WITH clause.

示例Examples

有关示例,请参阅使用 WITH XMLNAMESPACES 将命名空间添加到查询For examples, see Add Namespaces to Queries with WITH XMLNAMESPACES.

另请参阅See Also

XQuery 语言参考 (SQL Server)XQuery Language Reference (SQL Server)