如何使用 XSD 枚举验证 XML 属性?
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XSD 使用嵌套完成您想要的(条件验证)。
因此,虽然您不能使用 XSD 使一个属性的值以另一个属性的值为条件(如 Fyodor Soikin 在他的回答中说),您可以使用 XSD 使属性的值以元素本身为条件:
在这种情况下,每个属性都是其自己的元素,每个“值”属性都有自己的枚举。
它为您提供了额外的好处,即能够限制您可以拥有的每个命名属性的数量,但缺点是您需要管理更多元素。
根据您的具体情况,以这种方式使用 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:
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.