是否可以将显式属性验证和anyAttribute结合起来

发布于 2024-11-14 04:19:30 字数 1132 浏览 3 评论 0原文

是否可以在 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" />

两个主要问题是:

  1. 我不'不想每次需要引入一些新属性时都更改 XML 架构,因为我们不想强制所有客户端更新到软件的最新版本,而且其中一些客户端很长时间都不会更新其应用程序时间;
  2. 我不能只在 XML 模式中使用 anyAttribute 因为我想在使用 XML 文档之前对其进行验证。如果我只指定 anyAttribute 元素,那么我不知道缺少一些必需的属性。据我了解,XML 不允许在架构中使用 attributeanyAttribute 元素(至少我无法使用 .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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

太阳公公是暖光 2024-11-21 04:19:30

xs:anyattributeprocessContents 值可以为 strictlaxskipstrict 是默认值。

  • strict:必须有相应的全局属性声明,并且将根据该声明验证该属性。
  • lax:如果有相应的全局属性声明,则验证该属性;否则跳过它
  • skip:即使有声明也不验证属性

如果您的架构的下一个版本看起来像

<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>

(您没有使用全局属性),那么skip 可能最好确保添加的属性不会意外地针对碰巧具有相同名称且可能具有不同类型的全局属性声明进行验证。

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.

时光清浅 2024-11-21 04:19:30

好的,我已经解决了问题。该架构可能如下所示:

<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>

关键是指定 processContents="lax"processContents="skip"。如果省略将 processContents 设置为 laxskip,则验证失败。如果有人知道这背后的逻辑,请发表评论。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文