XSD - 定义多个具有不同 ID 属性的必需元素,其中之一具有子元素
我正在尝试为一些 XML 开发一个 XSD,大致如下所示:
<root>
<thing id="1"/>
<thing id="2"/>
<thing id="3">
<subelement>
...
</subelement>
</thing>
</root>
其中每个 id 的事物都需要一次,并且 id 为“3”的事物需要特定的子元素。
我能够通过创建一个抽象复杂类型并扩展它来创建每个 ID 的一个事物的要求(尽管它尚未经过测试),
<xs:element name="thing" type="tns:abstractType" minOccurs="3" maxOccurs="3">
<xs:unique name="EachIdRequired">
<xs:selector xpath="thing" />
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
<xs:complexType name="abstractType" abstract="true/>
<xs:complexType name="idOne">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="1" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="idTwo">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="2" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="idThree">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="3" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
但是,我不确定如何也满足 complexType
complexType 也具有所需的子元素。我见过同时定义属性和子元素的元素的唯一方法是使用扩展,但是 idThree 已经扩展了抽象类型。
我想要的事情可以通过其他方式实现吗?我很怀疑,但我想确定一下。 XSD 似乎有些限制。
I am trying to develop an XSD for some XML that looks roughly like this:
<root>
<thing id="1"/>
<thing id="2"/>
<thing id="3">
<subelement>
...
</subelement>
</thing>
</root>
Where a thing of each id is required once, and the thing with the id of "3" requires specific subelements.
I was able to create the requirement of one thing of each ID by creating a abstract complex type and extending off of that (though it's still untested),
<xs:element name="thing" type="tns:abstractType" minOccurs="3" maxOccurs="3">
<xs:unique name="EachIdRequired">
<xs:selector xpath="thing" />
<xs:field xpath="@id"/>
</xs:unique>
</xs:element>
<xs:complexType name="abstractType" abstract="true/>
<xs:complexType name="idOne">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="1" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="idTwo">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="2" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="idThree">
<xs:complexContent>
<xs:extension base="tns:abstractType">
<xs:attribute name="id" fixed="3" type="xs:integer" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
However, I am unsure how to also have the requirement of the complexType
of id 3 to also have the required subelements. The only way I have seen an element defined with both an attribute and subelements is the use of an extension, however idThree
is already extending the abstract type.
Is what I am going for possible in some other way? I am doubtful but I want to be sure. XSD seems somewhat limiting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 XML 模式中不可能。这种结构的最佳模式语言是 Relax NG。请参阅http://relaxng.org/
Not possible in XML Schema. The best Schema langage for such a structure is Relax NG. See http://relaxng.org/