默认命名空间中的 XML 属性名称和元素? [英] XML attribute names and elements in default namespace?

查看:29
本文介绍了默认命名空间中的 XML 属性名称和元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应如何解释 XML 命名空间规范中的以下内容?

How should the following in XML namespace specification be interpreted?

默认命名空间声明适用于其范围内的所有不带前缀的元素名称.默认命名空间声明不直接应用于属性名称;无前缀属性的解释由它们出现的元素决定.

A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

我的直觉理解是,无前缀的属性应该被解释为属于它们所属元素的命名空间.然而,下面的例子似乎证明了这一点:

My intuitive understanding is that unprefixed attributes should be interpreted as belonging to the namespace of the element they belong to. However, the following example seems to prove this false:

架构:

<xs:schema xmlns:myns="http://test.com/xsd/foo" elementFormDefault="qualified" 
           targetNamespace="http://test.com/xsd/foo" version="1.0" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:attribute name="Id" type="xs:string" />
    <xs:element name="Foo">
        <xs:complexType>
            <xs:attribute ref="myns:Id" />
    </xs:complexType>
    </xs:element>
</xs:schema>

通过验证的示例:

<a:Foo xmlns:a="http://test.com/xsd/foo" a:Id="123" />

验证失败的示例:

<Foo xmlns="http://test.com/xsd/foo" Id="123" />

是什么?

推荐答案

这里有两个单独的问题.第一个是关于 XML,第二个是关于 XML Schema (XSD).

THere are two separate questions here. The first is about XML, the second is about XML Schema (XSD).

在 XML 中(特别是在 XML 命名空间建议中定义的)中,无前缀的属性不在命名空间中.一些评论者将此解释为未定义名称空间(即应用程序定义),但通常且更简单的解释是它为空:没有名称空间.在这种情况下,如果您要求命名空间 URI,大多数 API 规范将返回 null,有些将返回零长度字符串.

In XML (specifically, as defined in the XML Namespaces recommendation), an unprefixed attribute is in no namespace. Some commentators interpret this as meaning the namespace is undefined (i.e. application-defined) but the usual and easier interpretation is that it is null: there is no namespace. Most of the API specifications will return null if you ask for the namespace URI in this situation, some will return a zero-length string.

在 XSD 中,全局属性声明在声明它的 XML 架构文档的目标命名空间中定义属性,如果包含的架构文档未指定目标命名空间,则在无命名空间中定义属性.出于这个原因,使用全局属性声明是不常见的.一种常见的技术是声明属性组(有时,属性组包含单个属性);在此类属性组中声明的任何属性都是局部声明而不是全局声明,因此它们不会继承包含架构文档的目标命名空间,除非您说 attributeFormDefault="qualified",这将是一件相当奇怪的事情.

In XSD, a global attribute declaration defines an attribute in the target namespace of the XML Schema document in which it is declared, or in no namespace if the containing schema document specifies no target namespace. For this reason it is unusual to use global attribute declarations. A common technique is to declare attribute groups (sometimes, attribute groups containing a single attribute); any attributes declared in such attribute groups are local declarations rather than global declarations, so they do not inherit the target namespace of the containing schema document unless you say attributeFormDefault="qualified", which would be a rather weird thing to do.

这篇关于默认命名空间中的 XML 属性名称和元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆