是否可以将显式属性验证和anyAttribute结合起来
是否可以在 XML 模式中定义必须存在某些特定的 XML 属性,同时我希望允许将来扩展此列表?
在这里,如果我们有以下假设的 XML 声明部分:
<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>
那么根据此模式,以下 XML 文档片段是有效的:
<MyType firstAttr="123" secondAttr="abc" />
我想要的是能够成功验证以下 XML 片段:
<MyType firstAttr="123" secondAttr="abc" ThirdAttr="some new value" />
两个主要问题是:
- 我不'不想每次需要引入一些新属性时都更改 XML 架构,因为我们不想强制所有客户端更新到软件的最新版本,而且其中一些客户端很长时间都不会更新其应用程序时间;
- 我不能只在 XML 模式中使用
anyAttribute
因为我想在使用 XML 文档之前对其进行验证。如果我只指定anyAttribute
元素,那么我不知道缺少一些必需的属性。据我了解,XML 不允许在架构中使用attribute
和anyAttribute
元素(至少我无法使用 .net 使此类架构正常工作)XmlDocument
类)。
如果能够使用 attribute
元素显式指定某些属性,那就太理想了,这样我就可以确切地知道这些属性存在于 XML 文档中,但同时我可以扩展 XML使用 anyAttribute
元素的文档。
怎么办呢?
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
anyAttribute
in XML schema because I want to validate XML document before working with it. If I specify justanyAttribute
element, then I wouldn't know that some required attributes are missing. And as far as I understand XML doesn't allow to useattribute
andanyAttribute
elements in schema (at least I wasn't able to make such schema to work using .netXmlDocument
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
xs:anyattribute
的processContents
值可以为strict
、lax
或skip
,strict
是默认值。strict
:必须有相应的全局属性声明,并且将根据该声明验证该属性。lax
:如果有相应的全局属性声明,则验证该属性;否则跳过它skip
:即使有声明也不验证属性如果您的架构的下一个版本看起来像
(您没有使用全局属性),那么
skip 可能最好确保添加的属性不会意外地针对碰巧具有相同名称且可能具有不同类型的全局属性声明进行验证。
xs:anyattribute
can have aprocessContents
value of eitherstrict
,lax
orskip
, withstrict
being 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
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.好的,我已经解决了问题。该架构可能如下所示:
关键是指定
processContents="lax"
或processContents="skip"
。如果省略将processContents
设置为lax
或skip
,则验证失败。如果有人知道这背后的逻辑,请发表评论。Ok, I've solved the problem. The schema can look like this:
The key was to specify
processContents="lax"
orprocessContents="skip"
. If omit settingprocessContents
tolax
orskip
then validation fails. If anybody knows the logic behind this, put some comments, please.