Re: Mandatory attributes on root element

Hi Torbjørn,

> I want "xmlns:xsi" and "xsi:noNamespaceSchemaLocation" to be
> required abbributes.

While it looks like an attribute, xmlns:xsi is not treated like an
attribute by any namespace-aware application. It is a namespace
declaration, associating the prefix 'xsi' with the namespace that you
place in its value. In a way, you 'force' the xmlns:xsi namespace
declaration to be present if you ever use the 'xsi' prefix on another
attribute, so you really don't have to worry about that attribute.

> I have tried to put in the following lines:
>
> <xsd:attribute name="xmlns:xsi"  use="required"/>
> <xsd:attribute name="xsi:noNamespaceSchemaLocation"  use="required"/>

When you declare an attribute, the name attribute on xs:attribute must
only contain the local name of the attribute. The namespace for the
attribute comes from the target namespace of the schema in which the
attribute is declared (depending on the form [qualified or
unqualified] of the attribute).

So to declare an attribute called 'noNamespaceSchemaLocation' in the
http://www.w3.org/2001/XMLSchema-instance namespace, you must have a
schema that has that namespace as the target namespace, something
like:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.w3.org/2001/XMLSchema-instance">

<xs:attribute name="noNamespaceSchemaLocation" type="xs:anyURI" />
           
</xs:schema>

[You have to be careful doing this - the special status of the various
attributes in the XML Schema instance namespace requires that the
attribute declaration you give must match the attribute declaration
specified within the XML Schema Recommendation.]

You must then import that schema into your schema, with the following
top-level element:

<xs:import namespace="http://www.w3.org/2001/XMLSchema-instance"
           schemaLocation="schema-instance.xsd" />

You can then *refer to* that attribute declaration, as follows, in the
complex type for the relevant element:

  <xs:attribute ref="xsi:noNamespaceSchemaLocation" use="required" />

Note that the ref attribute is a qualified name; the 'xsi' prefix must
be associated with the http://www.w3.org/2001/XMLSchema-instance
namespace in the schema that you're using.


Having said all that, I don't think that you can define that the
xsi:noNamespaceSchemaLocation attribute is required on a particular
element. All the attributes in the XML Schema instance namespace are
special in terms of validation - you don't have to define that an
element has one of these attributes in order for the element to be
valid.

What's more, it looks like if you state that such an attribute is
required, then you will get a validation error. This is because when a
schema validator checks whether all required attributes are present,
it does so against a set of attributes that explicitly does not
include any of the XML Schema instance attributes. (In my tests,
Xerces and MSV say they can't find xsi:noNamespaceSchemaLocation on
the element, despite the fact that it is there; XSV validates the
document OK.)


So I guess the question is what you were hoping to achieve by making
the xsi:noNamespaceSchemaLocation attribute required? There might be
some other way to do it.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Tuesday, 22 January 2002 04:54:13 UTC