如何使用 XSD 枚举验证 XML 属性?

发布于 2024-09-05 18:23:50 字数 337 浏览 1 评论 0原文

我有以下 XML 序列,

<property name="a"  value="x" />
<property name="b"  value="xx" />

我想将 "a" 限制为值列表,例如 [ x, y, z] 并将 "b" 限制为另一个列表,例如 [ xx, yy, zz]

是否可以使用 XSD 来执行此操作,如果可以,如何执行?

如果这是不可能的,您建议如何更改 XML 格式以使其为 XSD 验证做好准备? (我确实有两个以上的属性名称/值对)

I have the following XML sequence

<property name="a"  value="x" />
<property name="b"  value="xx" />

I want to limit "a" to a list of values, like [ x, y, z] and "b" to another list like [xx, yy, zz]

Is it possible to do this using XSD, and if it is how?

If this is not possible, how do you recommend to change the XML format in order to make it ready for XSD validation? (I do have more than two property name/value pairs)

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

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

发布评论

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

评论(1

守护在此方 2024-09-12 18:23:50

XSD 使用嵌套完成您想要的(条件验证)。

因此,虽然您不能使用 XSD 使一个属性的值以另一个属性的值为条件(如 Fyodor Soikin 在他的回答中说),您可以使用 XSD 使属性的值以元素本身为条件:

<propertyNameA value="x"/>
<propertyNameB value="xx"/>

在这种情况下,每个属性都是其自己的元素,每个“值”属性都有自己的枚举。

它为您提供了额外的好处,即能够限制您可以拥有的每个命名属性的数量,但缺点是您需要管理更多元素。

根据您的具体情况,以这种方式使用 XSD 可能是最佳选择,或者实现另一个可以执行您所需的名称/值验证的系统可能会更好。

XSD accomplishes what you want (conditional validation) using nesting.

So while you can't use XSD to make the values of one attribute conditional upon the values of another (as Fyodor Soikin says in his answer), you can use XSD to make the values of an attribute conditional upon the element itself:

<propertyNameA value="x"/>
<propertyNameB value="xx"/>

In this case each property is its own element, and each "value" attribute has its own enumeration.

It gives you the added benefit of being able to restrict how many of each named property you can have, but the disadvantage is you need to manage more elements.

Depending on the specifics of your situation, using XSD this way might be the best option, or it might be better to implement another system that can do the name/value validation you need.

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