tags:

views:

28

answers:

3

Take a look at this example.

Now in the attribute.xsd there is an gender attribute defined. But as I understand I can use this attribute wherever the family.xsd would have anyAttribute.

Is it possible to restrict gender attribute to a certain parent based on parent's name?

A: 

My knowledge of XML technologies is rather brief so take this with a pinch of salt.

You can use gender (male|female) in place of other attributes, as it would be syntactically correct XML.

However if you are checking if your XML is valid and well formed, then you need to write a DTD or preferably a schema document, to check where the gender attribute is allowed to be used

JonWillis
huh? this question is about schemas already.
Kugel
face palm, Sorry, I glanced over the W3C link to quickly to realise it wasn't the XMl tutorial.
JonWillis
A: 

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

  1. Is there a way to use <xs:anyAttribute> but actually allow only certain kind of attributes
  2. 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

  1. ##other, which allows any namespaced attribute but not an attribute belonging to the target namespace of the schema
    or
  2. 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.

jasso
A: 

I solved this using <xs:redefine> which lets you redefine types and groups.

Kugel