Generally attributes are allowed only if a complex type is especially defined to allow the wanted attributes. On the other side generally <xs:anyAttribute>
is used only when you want to allow any attributes (possibly with namespace restrictions) in this complex type.
I see this question could be interpreted in two different ways
- Is there a way to use
<xs:anyAttribute>
but actually allow only certain kind of attributes
- Is there a way to use
<xs:anyAttribute>
but disallow one certain attribute
Actually I don't have any good answers, I only have untested hacks.
Case number 1
This can be achieved using namespace
attribute on <xs:anyAttribute>
. Default value for namespace
is ##any
which allows attributes from any namespace or without a namespace. Instead of ##any you can use either
##other
, which allows any namespaced attribute but not an attribute belonging to the target namespace of the schema
or
- a whitespace separated list of URIs of allowed namespaces. This list can also contain wildcards
##targetNamespace
(which allows attributes from the target namespace of the schema) and ##local
(which allows attributes without a namespace)
Disallowing only attributes without a namespace doesn't seem to be possible.
If a schema has a target namespace, then all the globally defined attributes belong to that target namespace. So the w3schools' example seems in fact to be incorrect because they defined the attribute gender
globally in a schema with a target namespace, yet they used it without a namespace prefix (default namespace doesn't apply to attributes).
Case number 2
This might be achieved with <xs:attribute name="gender" use="prohibited"/>
. Attributes with use="prohibited"
cannot be included. However I have never investigated is it allowed to use such attribute definition when the <xs:anyAttribute>
element is present. Attribute use
is not allowed on global attribute definitions so globally prohibiting the use of an attribute is not possible in this way.