问题
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:
Schema:
<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>
Example that passes validation:
<a:Foo xmlns:a="http://test.com/xsd/foo" a:Id="123" />
Example that fails validation:
<Foo xmlns="http://test.com/xsd/foo" Id="123" />
What gives?
回答1:
THere are two separate questions here. The first is about XML, the second is about XML Schema (XSD).
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.
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.
回答2:
It appears that the attribute Id is interpreted outside any namespace and doesn't match the schema, since the attribute in the schema has a namespace. However, if the schema is declared differently, as follows:
<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:element name="Foo">
<xs:complexType>
<xs:attribute name="Id" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
The previously invalid document validates and vice versa. The cause seems to be that XML Schema defines that the namespace of an ettribute is either defined by targetNamespace
attribute of its parent <schema>
element, or if there is none, there is no target namespace. In the latter case the parent is <complexType>
instead of <schema>
, so the attribute doesn't get a namespace.
The note about interpretation of unprefixed attributes in my original question is still puzzling, however.
回答3:
Look at the detailed answer I posted earlier to a similar question:
What is the behavior of getAttributeNS?
Attributes do not have a namespace unless explicitly prefixed with one. The idea is that unlike elements that can get mixed in into other documents, attributes really only make sense in scope of the elements that carry them. There are few exceptions like xml:lang
but the vast majority is logically "scoped" with (and thus bound to) their owning elements. Hence no namespace until you really need one.
来源:https://stackoverflow.com/questions/11086899/xml-attribute-names-and-elements-in-default-namespace