问题描述
问题如下:
我有以下XML代码段:
I have the following XML snippet:
<time format="minutes">11:60</time>
问题是我不能同时添加属性和限制.该属性格式只能具有分钟,小时和秒的值.时间具有限制模式\d{2}:\d{2}
The problem is that I can't add both the attribute and the restriction at the same time. The attribute format can only have the values minutes, hours and seconds. The time has the restriction pattern \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,我可以添加一个属性,但不能添加限制;如果我创建简单类型,我可以添加限制,但不能添加属性.有什么办法可以解决这个问题.这不是一个很奇怪的限制,不是吗?
If I make a complex type of timeType, I can add an attribute, but not the restriction, and if I make a simple type, I can add the restriction but not the attribute. Is there any way to get around this problem. This is not a very strange restriction, or is it?
推荐答案
要添加必须通过扩展导出的属性,要添加必须通过限制导出的构面.因此,对于元素的子内容,必须分两个步骤完成.该属性可以内联定义:
To add attributes you have to derive by extension, to add facets you have to derive by restriction. Therefore this has to be done in two steps for the element's child content. The attribute can be defined inline:
<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>
这篇关于在Xml模式中将属性添加到简单类型或将限制添加到复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!