如何“使用"属性(必需/可选)取决于另一个值? [英] How to make "use" attribute (required/optional) depend on another value?

查看:53
本文介绍了如何“使用"属性(必需/可选)取决于另一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 XSD XML 架构中做一些特定的事情.我想将一个元素设置为 use="required",前提是另一个元素的属性有特定的值.

I'm trying to do some specific stuff in an XSD XML Schema. I want to set an element as use="required", only if there a specific value to an attribute of another element.

示例:

<udi att1="all">
   <name att2="dsd"/>
   <phone att3="dsd"/>
</udi>

我希望 phone 元素是必需的,仅当 att1 值为 all 时.

I want the phone element to be required, only if the att1 value is all.

有人告诉我 XPath 可以解决这个问题 - 或者有更好的方法吗?

I was told that XPath could work for this - or is there a better way?

推荐答案

XML 不是关系型的,它是分层的.模式旨在强制执行嵌套规则,而不是兄弟之间的规则.XSD 根本无法做到这一点.

XML is not relational, it's hierarchical. Schemas are designed to enforce rules on nesting, not on rules between siblings. It simply is not something you can do with XSD.

不过,您可以在解析或创建符合格式的文档时执行此操作.在这种情况下,当您解析 XML 文档时,您会强制执行您在问题中所述的约束.

You can do it when you're parsing or creating documents conforming to the format, however. In this case, while you're parsing through the XML document, you enforce constraints like the one you have stated in your question.

这篇关于如何“使用"属性(必需/可选)取决于另一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆