У меня есть следующие требования, и я пытаюсь определить, как лучше всего смоделировать XSD для представления этих требований.
У меня есть много экземпляров элемента XML, скажем <box>
.Каждый <box>
имеет обязательный атрибут t="[box-type]"
, а каждый блок определенного типа, скажем, t="tall"
, имеет еще один обязательный атрибут v="10"
, который представляет высоту высокого поля.Все <box>
имеют атрибуты t
и v
, но ограничение на то, какие значения принимаются для их атрибутов v
, зависит от значения их атрибута t
.
Например,возьмите следующий XML:
<box t="tall" v="10"/>
<box t="named" v="George"/>
<box t="colored" v="green"/>
Теперь в моем XSD мне нужно иметь возможность представлять последовательность таких элементов.Я думал сделать что-то вроде следующего, который просто перечисляет все разрешенные типы ящиков в моей последовательности (в конце следующего фрагмента):
<xsd:simpleType name="box_types">
<xsd:restriction base="xsd:token">
<xsd:enumeration value="tall" />
<xsd:enumeration value="named" />
<xsd:enumeration value="colored" />
</xsd:restriction>
</xsd:simpleType>
<!--Box base-->
<xsd:complexType name="box_type">
<xsd:attribute name="t" use="required" type="box_types"/>
<xsd:attribute name="v" use="required"/>
</xsd:complexType>
<!--Box concrete types-->
<xsd:complexType name="tall_box_type">
<xsd:complexContent>
<xsd:extension base="box_type">
<xsd:attribute name="t" fixed="tall" use="required"/>
<xsd:attribute name="v" type="xsd:int" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="named_box_type">
<xsd:complexContent>
<xsd:extension base="box_type">
<xsd:attribute name="t" fixed="named" use="required"/>
<xsd:attribute name="v" type="xsd:string" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="colored_box_type">
<xsd:complexContent>
<xsd:extension base="box_type">
<xsd:attribute name="t" fixed="colored" use="required"/>
<xsd:attribute name="v" type="xsd:token" use="required"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!--And finally, the place where boxes show up-->
<xsd:complexType name="box_usage">
<xsd:sequence>
<xsd:element name="box" type="tall_box_type"/>
<xsd:element name="box" type="named_box_type"/>
<xsd:element name="box" type="colored_box_type"/>
</xsd:sequence>
</xsd:complexType>
К сожалению, это не правильный XSD -VS дает мне несколько ошибок, самая неудачная из которых Elements with the same name and in the same scope must have the same type
.Любой совет, как я могу представить эти ограничения связанного атрибута t / v в XSD?