通过 XSD 限制基于另一个元素的 XML 元素

发布于 2024-10-01 09:04:06 字数 172 浏览 2 评论 0原文

我相信这与 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 技术交流群。

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

发布评论

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

评论(2

青朷 2024-10-08 09:04:06

类似的问题已经被问过好几次了..在 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

意中人 2024-10-08 09:04:06

我建议为下面定义的每种情况创建 XSD:

类似情况:

  1. 根据任何其他元素/属性的值或存在来验证元素。
  2. 根据任何其他元素/属性的值或存在来验证元素的值。
  3. 根据任何其他元素/属性的值或存在来验证属性。
  4. 不允许在验证中忽略(随机)元素/属性。 (我的意思是说 的使用有限制。)
  5. 具有不同数据类型的同名元素/属性。
  6. 根据任何其他元素/属性的值或存在来验证子元素。

并为此创建自己的 XML。

What I would suggest is to create XSD for each cases defined below:

Cases like:

  1. Validating Element depending on the value or presence of any other element/attribute.
  2. Validating value of an element depending on the value or presence of any other element/attribute.
  3. Validating attribute depending on the value or presence of any other element/attribute.
  4. Ignoring (random) elements/attributes from validation isn't allowed. (I mean to say is usage of has restrictions.)
  5. Elements/attributes of same name having different DataTypes.
  6. Validating child elements on the basis of value or presence of any other Elements/Attributes.

And create there own XML for that.

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