问题如下:
我有以下 XML 片段:
<time format="minutes">11:60</time>
问题是我无法同时添加属性和限制。属性格式只能具有值分钟、小时和秒。时间具有限制模式 \d{2}:\d{2}
<xs:element name="time" type="timeType"/>
...
<xs:simpleType name="formatType">
<xs:restriction base="xs:string">
<xs:enumeration value="minutes"/>
<xs:enumeration value="hours"/>
<xs:enumeration value="seconds"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="timeType">
<xs:attribute name="format">
<xs:simpleType>
<xs:restriction base="formatType"/>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
如果我创建一个复杂类型的 timeType,我可以添加属性,但不能添加限制,如果我创建一个简单类型,我可以添加限制但不能添加属性。有什么办法可以解决这个问题吗?这不是一个很奇怪的限制,不是吗?
最佳答案
要添加属性,您必须通过扩展派生,要添加方面,您必须通过限制派生。因此,对于元素的子内容,必须分两步完成。该属性可以内联定义:
<xsd:simpleType name="timeValueType">
<xsd:restriction base="xsd:token">
<xsd:pattern value="\d{2}:\d{2}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="timeType">
<xsd:simpleContent>
<xsd:extension base="timeValueType">
<xsd:attribute name="format">
<xsd:simpleType>
<xsd:restriction base="xsd:token">
<xsd:enumeration value="seconds"/>
<xsd:enumeration value="minutes"/>
<xsd:enumeration value="hours"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
关于xsd - 在 Xml 架构中向简单类型添加属性或向复杂类型添加限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/626319/