Is it possible to define in XML schema that there must be some certain XML attributes, and at the same time I want to allow to extend this list in future?
Here, if we have the following hypothetical part of XML declaration:
<xs:element name="MyTypeInstance" type="MyType" />
<xs:complexType name="MyType">
<xs:attribute name="FirstAttr" type="xs:int" use="required"/>
<xs:attribute name="SecondAttr" type="xs:string" use="required"/>
</xs:complexType>
Then the following XML document fragment is valid according to this schema:
<MyType firstAttr="123" secondAttr="abc" />
What I want is to be able to successfully validate the following XML fragment:
<MyType firstAttr="123" secondAttr="abc" ThirdAttr="some new value" />
The two main problems are:
- I don’t want to change XML schema every time I need to introduce some new attribute because we don’t want to force all of our client to update to the latest version of our software, and some of them don’t update their apps for a long time;
- I can’t just use
anyAttributein XML schema because I want to validate XML document before working with it. If I specify justanyAttributeelement, then I wouldn’t know that some required attributes are missing. And as far as I understand XML doesn’t allow to useattributeandanyAttributeelements in schema (at least I wasn’t able to make such schema to work using .netXmlDocumentclass).
It would be ideal if it would be possible to specify some attributes explicitly using attribute element, so I would know exactly that these attributes are present in XML document, but at the same time I would let to extend XML document using anyAttribute element.
How can it be done?
xs:anyattributecan have aprocessContentsvalue of eitherstrict,laxorskip, withstrictbeing the default.strict: there must be a corresponding global attribute declaration and the attribute will be validated against that declarationlax: if there is a corresponding global attribute declaration, validate the attribute; otherwise skip itskip: do not validate the attribute even if there is a declarationIf your next version of the schema will look like
(you’re not using global attributes), then
skipis probably best to make sure the added attribute doesn’t accidentally validate against a global attribute declaration that happens to have the same name and possibly a different type.