XSD 按名称或备用属性定义元素 [英] XSD define element by name or alternate attribute

查看:16
本文介绍了XSD 按名称或备用属性定义元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑标签组"的两种可能实现:

Consider two possible implementations of a "tag group":

作者可以使用名为 的元素或任何具有 role="tag-group" 属性的元素.

Authors are allowed to use either an element named <tag-group> or any element with attribute role="tag-group".

我想编写一个 XSD 架构来验证任何一种情况.

I'd like to write an XSD schema that will validate either case.

理想情况下,模式还可以验证子项在给定父项中是否有效.例如,标签组"允许子元素是名为 的元素或任何具有 role="tag" 属性的元素.

Ideally, the schema can also validate that children are valid within a given parent. The "tag group", for example, allows children to be either an element named <tag> or any element with attribute role="tag".

所以理想的模式将验证,给定 ,子节点应该是 ;或者给定任何具有 role="tag-group" 的元素,子元素应该是具有 role="tag" 的任何元素.

So the ideal schema would validate, that given <tag-group>, the child should be <tag>; OR given any element with role="tag-group", the child should be any element with role="tag".

测试用例期望:

通过

<tag-group>
  <tag>foo</tag>
</tag-group>

通过

<foo role="tag-group">
  <bar role="tag">fum</bar>
</foo>

失败

<tag-group>
  <bar role="tag">fum</bar>
</tag-group>

失败

<foo role="tag-group">
  <tag>foo</tag>
</foo>

我是 XSD 的新手,如果您链​​接它们,我很乐意浏览参考文档.

I am new to XSD and happy to look through reference docs if you link them.

推荐答案

您越远离由元素名称定义的类型,您就越会发现自己通常与 XSD 背道而驰.

The farther you get from type being defined by element name, the more you'll find yourself generally going against the grain of XSD.

XSD 1.1 的 条件类型分配 (CTA) 允许内容模型因属性值而异.XSD 1.1 的断言允许基于数据依赖性的进一步类型变化.您可以尝试为这种额外类型的灵活性"制作断言,但我会提醒您,很快就会出现最好重新考虑您的设计的点.

XSD 1.1's Conditional Type Assignment (CTA) allow the content model to vary per attribute values. XSD 1.1's assertions allow further type variations based on data dependencies. You can try to craft assertions for such additional type "flexibility", but I'll caution you that there quickly comes a point where it's better to rethink your design.

号召性用语另见:

这篇关于XSD 按名称或备用属性定义元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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