开发者

Is it possible to combine explicit attributes validation and anyAttribute

开发者 https://www.devze.com 2023-03-11 12:28 出处:网络
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?

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开发者_如何学JAVA: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:

  1. 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;
  2. I can't just use anyAttribute in XML schema because I want to validate XML document before working with it. If I specify just anyAttribute element, then I wouldn't know that some required attributes are missing. And as far as I understand XML doesn't allow to use attribute and anyAttribute elements in schema (at least I wasn't able to make such schema to work using .net XmlDocument class).

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:anyattribute can have a processContents value of either strict, lax or skip, with strict being the default.

  • strict: there must be a corresponding global attribute declaration and the attribute will be validated against that declaration
  • lax: if there is a corresponding global attribute declaration, validate the attribute; otherwise skip it
  • skip: do not validate the attribute even if there is a declaration

If your next version of the schema will look like

<xs:complexType name="MyType">
  <xs:attribute name="FirstAttr" type="xs:int" use="required" />
  <xs:attribute name="SecondAttr" type="xs:string" use="required" />
  <xs:attribute name="ThirdAttr" type="xs:string" use="required" />
  <xs:anyAttribute processContents="lax" />
</xs:complexType>

(you're not using global attributes), then skip is 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.


Ok, I've solved the problem. The schema can look like this:

<xs:complexType name="MyType">
  <xs:attribute name="FirstAttr" type="xs:int" use="required" />
  <xs:attribute name="SecondAttr" type="xs:string" use="required" />
  <xs:anyAttribute processContents="lax" />
</xs:complexType>

The key was to specify processContents="lax" or processContents="skip". If omit setting processContents to lax or skip then validation fails. If anybody knows the logic behind this, put some comments, please.

0

精彩评论

暂无评论...
验证码 换一张
取 消