通过 XSD 限制基于另一个元素的 XML 元素
我相信这与 keyref 有关,但我不确定,而且我真的不确定它是否可以完成。
例如,假设我有 myElement1 和 myElement2。如果 XML 文件中没有 myElement2,则 myElement1 必须存在,否则是可选的。
有没有办法在我的 XSD 文件中强制进行这种类型的验证?
I believe this has to do with keyref
but I'm not for sure, and I am really not sure that it can be done at all.
For example, say I have myElement1 and myElement2. If there are no myElement2 in the XML file, then myElement1 must exist, otherwise it is optional.
Is there any way to force this type of validation in my XSD file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
类似的问题已经被问过好几次了..在 stackoverflow 或任何技术博客中..
但答案总是不可能,
类似情况:
(a) 根据任何其他元素/属性的值或存在来验证元素
(b) 根据任何其他元素/属性的值或存在来验证元素的值
(c) 根据任何其他元素/属性的值或存在来验证属性
(d) 在同一范围内定义具有不同类型/数据类型的同名元素/属性
(e)根据任何其他元素/属性的值或存在来验证子元素,
所有上述情况都是不可能实现的..
但是如果您可以实现一些转换事情..那么您可以获得帮助。
我的意思是“首先根据您的要求进行转换并修改 XML,
例如 :: 如果特定字段需要某个值.. 如果条件满足并且不通过,则按原样传递 XML如果没有..
在下一个块中,XSD 将进行验证,并且显然会出错:)您可以发现它缺少您的自定义验证..
无论如何,其目的是阻止或通过根据数据的xml..这就是你正在做的事情..
注意:Schematron 已进入最近好习惯!这是教程:http://zvon.org/xxl/SchematronTutorial/General/contents。 html
Similar questions have been asked several times .. in stackoverflow or any tech blogs ..
but the answer is always not possible,
cases like :
(a) validating Element depending on the value or presence of any other element/attribute
(b) validating value of an element depending on the value or presence of any other element/attribute
(c) validating attribute depending on the value or presence of any other element/attribute
(d) defining elements/attributes of same name having different Types/DataTypes under same scope
(e) validating child elements on the basis of value or presence of any other Elements/Attributes
all the above mentioned cases are not possible to achieve ..
but if you can implement some transformation thing .. then you can get help.
what I mean to say is "first go through transformation and modify the XML as per your requirement,
example :: if the particular field is expected with some value .. pass the XML as it is if the condition is satisfied and don't pass if it doesn't ..
in the next block XSD is going to validate and obviously errors out :) you can figure out that it is missing your custom validation ..
anyway the intention is to block or Pass the xml according to the data .. and that is what you are doing ..
Note: Schematron has been come into good practice recently! Here is the tutorial: http://zvon.org/xxl/SchematronTutorial/General/contents.html
我建议为下面定义的每种情况创建 XSD:
类似情况:
并为此创建自己的 XML。
What I would suggest is to create XSD for each cases defined below:
Cases like:
And create there own XML for that.