我有这个小的XML模式。是否可以扩展"内容类型"而不是复制其内容?
我认为一个陷阱是"contentWithInputType"元素可以包含更多的"contentWithInputType"元素,但"contentType"元素只能包含"contentType"元素。
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://meinnamespace.meinefirma.de" targetNamespace="http://meinnamespace.meinefirma.de" elementFormDefault="qualified">
<xs:element name="ROOTNODE">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="content" type="contentType" />
<xs:element name="contentWithInput" type="contentWithInputType" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="contentType" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="span" type="contentType" />
<xs:element name="b" type="contentType" />
<xs:element name="i" type="contentType" />
</xs:choice>
</xs:complexType>
<xs:complexType name="contentWithInputType" mixed="true">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="span" type="contentWithInputType" />
<xs:element name="b" type="contentWithInputType" />
<xs:element name="i" type="contentWithInputType" />
<xs:element name="input" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:schema>
你能不能有:
<xs:complexType name="contentWithInputType" mixed="true">
<xs:sequence>
<xs:element name="content" type="contentType" />
<xs:element name="input" type="contentWithInputType" />
</xs:sequence>
</xs:complexType>
这在语义上类似于您的要求。
最终,您不能像在 obejct 方向上扩展类型一样在 XSD 中"扩展"类型。
也许你应该使用替换组。声明一个名为inline
的抽象元素;使其成为contentType
中唯一允许的元素;然后用substitutionGroup="inline"
定义元素span
、i
、b
等。
另一种有用的技术是命名模型组。定义一个包含要重复使用的内容模型片段的xs:group
,然后在需要的地方使用xs:group ref="group-name"
引用它。