Freigeben über


XInclude and schema validation

A lot of people are excited about XInclude and want to start using it in their projects.  However, there is an issue with using both XInclude and xsd validation at the same time.  The issue is that XInclude adds xml:* attributes to the instance documents while xsd spec forces you to explicitly declare these attributes in your schema.  Daniel Cazzulino, an XML MVP, blogged about this a few months ago: "W3C XML Schema and XInclude: impossible to use together???"

To solve this problem, we are introducing a new system.xml validation flag AllowXmlAttributes in VS2005.  This flag instructs the engine to allow xml:* attributes in the instance documents even if they are not defined in the schema.  The attributes will be validated based on their data type.  What follows is a brief description of how the validation engine behaves when this flag is turned on (which is by default).

The flag is added to the XmlSchemaValidationFlags.

When schema doesn’t import xml namespace

  1. If AllowXmlAttributes is set to true (default) and instance document contains xml attributes, the validator will load a snapshot of the default XML namespace schema and use it for validation.  If xml attributes used in the instance document are not found in the default schema, the document will not validate.
  2. If AllowXmlAttributes is set to false and instance document contains xml attributes, validation engine will report an error.

When schema imports xml namespace without a location

  1. If AllowXmlAttributes is set to true (default) and instance document contains xml attributes, the validator will use the default xml namespace schema for validation.  If xml attributes used in the instance document are not found in the default schema, the document will not validate.
  2. If AllowXmlAttributes is set to false and instance document contains xml attributes that are not explicitly allowed by the schema, validation engine will report an error.
  3. If AllowXmlAttributes is set to false and instance document contains xml attributes that are explicitly allowed by the schema, validation engine will validate the attributes the same way it validates other attributes using the default xml namespace schema.

When schema imports a custom xml namespace schema with a custom schemaLocation

  1. If AllowXmlAttributes is set to true (default) and instance document contains xml attributes, the validator will use the custom xml namespace schema for validation.  If xml attributes used in the instance document are not found in the specified schema, the document will not validate.
  2. If AllowXmlAttributes is set to false and instance document contains xml attributes that are not explicitly allowed by the schema, validation engine will report an error.
  3. If AllowXmlAttributes is set to false and instance document contains xml attributes that are explicitly allowed by the schema, validation engine will validate the attributes the same way it validates other attributes using specified xml namespace schema.

When schema defines an xml:* attribute with use=”required”

This case applies to both AllowXmlAttributes=true and AllowXmlAttributes=false.  Required xml:* attributes are treated as other attributes and validated using specified xml namespace schema.

When schema defines an xml:* attribute with use=”prohibited”

This case applies to both AllowXmlAttributes=true and AllowXmlAttributes=false.  If instance document contains xml:* attributes that are explicitly prohibited by the schema, validation engine will report an error.

When schema specifies an attribute wildcard

  1. If instance document contains an xml attribute and the schema specifies an attribute wildcard with processContents either omitted or set to “strict”, validation will use the xml namespace schema present in the XmlSchemaSet.  If no xml namespace schema was specified, validation will behave as follows:
    1. If AllowXmlAttributes is set to true, validation will use the default xml namespace schema to perform validation
    2. If AllowXmlAttributes is set to false, validation will error.
  2. If instance document contains an xml attribute and the schema specifies an attribute wildcard with processContents set to “skip”, validation will skip attribute validation.
  3. If instance document contains an xml attribute and the schema specifies an attribute wildcard with processContents set to “lax”, validation will use the xml namespace schema present in the XmlSchemaSet.  If no xml namespace schema was specified, validation will behave as follows:
    1. If AllowXmlAttributes is set to true, validation will use the default xml namespace schema to perform validation
    2. If AllowXmlAttributes is set to false, attribute validation will not be performed.

Qeustions? Comments? Let us know
Stan Kitsis

Comments

  • Anonymous
    May 11, 2005
    Glad this was caught in time to be accounted for. I like the proposed solution and the rules seem to make sense to me.

    Just my 2¢,
    Drew
  • Anonymous
    May 15, 2005
    As TechEd and the Saxon.NET Birds of a Feather session quickly approach it seems its time to start pushing hard at both finalization of the bits as well as sample code to help gain some momentum as we begin to...