我可以表达“可能/可能不包含”吗? XML 模式中跨越多个级别的规则?
我有一个通过 XML 传输的对象层次结构。 通过声明哪个元素可以包含哪些子元素,层次结构规则很容易在 XML 模式中表达。 XML 数据的一个简化示例是(实际对象数据包含在属性中,为简洁起见而省略):
<root>
<channel>
<router>
<message />
<message />
<message />
</router>
<router>
<message />
<message />
</router>
</channel>
<channel></channel>
</root>
现在我想介绍“文件夹”,即仅用于构造长子列表以便于使用的节点。 如果我创建一个通用
元素,它不会强加自己的规则,而是从树上的下一个非文件夹元素继承它们。
在我看来,我无法在 XSD 中表达这一点,或者可以吗?
我可以想到替代方案,例如为每个可能的规则设置特定的文件夹元素(丑陋),简单地忽略 XSD 中的规则并在代码中强制执行它们(对我来说没问题),或者以某种方式分离 XML 中的对象层次结构和中间节点(不太确定)如何)。 如果您对此有任何评论,我也将不胜感激。
I have a hierarchy of objects that are transmitted via XML. The hierarchy rules are easy to express in XML Schema by stating which element may contain which child elements. A simplifed example of the XML data would be (the actual object data is contained in attributes and omitted for brevity):
<root>
<channel>
<router>
<message />
<message />
<message />
</router>
<router>
<message />
<message />
</router>
</channel>
<channel></channel>
</root>
Now I'd like to introduce "folders", i. e. nodes that just serve to structure long lists of children for easier use. If I make a generic <folder>
element, it would not impose rules of its own, but inherit them from the next non-folder element up the tree.
It seems to me that I can't express that in XSD, or can I?
I can think of alternatives, like having specific folder elements for each possible rule (ugly), simply ignoring the rules in XSD and enforcing them in code (fine with me) or somehow separating object hierarchy and intermediate nodes in the XML (not quite sure how). I'd appreciate your comments on that, too, if you have any.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,你想要的东西不能用 XSD 表达。 不过,可以引入单级文件夹,或者更一般地说,引入 n 级文件夹,但不能引入任意递归文件夹(因为这些文件夹必须是全局元素,然后它们就不知道它们包含在什么内容中) )。
可以在 Schematron 中表达这样的约束; 反过来,可以将 schematron 约束嵌入到 XSD appinfo 元素中。 您的验证器可能支持也可能不支持嵌入 XSD 中的 Schematron。
You are right that what you want cannot be expressed in XSD. It would be possible to introduce single-level folders, though, or, more generally, n-level folders, but not arbitrarily-recursive ones (since those would have to be global elements, which then wouldn't know what they are contained in).
It is possible to express such a constraint in Schematron; in turn, it would be possible to embed the schematron constraint in an XSD appinfo element. Your validator may or may not support Schematron embedded in XSD.