【发布时间】:2019-02-10 14:57:44
【问题描述】:
我的新 XML 私有语言包含元素 <figure>,代表插图(图像 + 标题)。
每当插图引用本地数据库中的某些图像时,我只想输入
<figure id="9809" width="full" />
识别图像编号 9809 及其相关标题。
另一方面,如果图像来自外部,我将需要稍微不同的语法:
<figure href="https://some-url-here" width="full">Some ad hoc catpion</figure>
到目前为止,我已经声明了一个结合了这两种行为的元素,如下所示:
<!-- Figures -->
<xs:simpleType name="FigureWidthEnum">
<xs:restriction base="xs:token">
<xs:enumeration value="center" />
<xs:enumeration value="half" />
<xs:enumeration value="full" />
</xs:restriction>
</xs:simpleType>
<xs:element name="figure">
<xs:complexType mixed="true">
<xs:complexContent>
<xs:extension base="Inline">
<xs:attribute name="href" type="URI" />
<xs:attribute name="id" type="xs:nonNegativeInteger" />
<xs:attribute name="width" type="FigureWidthEnum" default="full" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
它工作得很好,但是一个新的编辑器可能会弄乱这 3 个属性并键入不可能的东西,我不想那么容易通过 Schema Validator。例如:
<figure id="9809" width="full" href="https://some-unexpected-url">Some unexpected caption that should not be here</figure>
我想为<figure> 有两个完全独立的sintaxes,好像我可以用相同的名称声明这两个元素:
<xs:element name="figure">
<xs:complexType mixed="true">
<xs:complexContent>
<xs:extension base="Inline">
<xs:attribute name="href" type="URI" />
<xs:attribute name="width" type="FigureWidthEnum" default="full" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:element>
<xs:element name="figure">
<xs:complexType>
<xs:attribute name="id" type="xs:nonNegativeInteger" />
<xs:attribute name="width" type="FigureWidthEnum" default="full" />
</xs:complexType>
</xs:element>
事实上这是不可能的。
可以通过某种方式完成吗?
【问题讨论】: