将属性添加到简单types或对Xml架构中的复杂types的限制

问题如下:

我有以下的XML片段:

<time format="minutes">11:60</time> 

问题是我不能同时添加属性和限制。 属性格式只能具有分钟,小时和秒的值。 时间有restrictionpattern \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> 

如果我创build一个复杂types的timeType,我可以添加一个属性,但不是限制,如果我做一个简单的types,我可以添加限制,但不是属性。 有没有办法解决这个问题。 这不是一个很奇怪的限制,还是它?

要添加必须通过扩展派生的属性,添加方面必须通过限制派生。 因此,必须分两步完成元素的子内容。 该属性可以内联定义:

 <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> 

我想提出我的例子更详细的解释什么实际上需要混合inheritancetypes与限制,而添加属性。

这是你定义你的inheritancetypes的地方(在我的情况下,它是基于字符长度1024限制的xs:string)。 您不能将此作为您的字段的标准types,因为您将要为该字段添加“属性”。

  <xs:simpleType name="string1024Type"> <xs:restriction base="xs:string"> <xs:maxLength value="1024"/> </xs:restriction> </xs:simpleType> 

这是你的元素根据你的私有types定义的地方(在我的情况下,它是“string1024Type”)和附加的必要属性:

 <xs:element maxOccurs="unbounded" name="event"> <xs:complexType> <xs:simpleContent> <xs:extension base="string1024Type"> <xs:attribute default="list" name="node" type="xs:string"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> 

这两个块可以在你的模式完全独立的地方。 重要的一点是只能再一次 – 你不能在同一个元素定义中混合inheritance和归属。