我正在学习如何创建xml / xsd文档,而且我现在已经被卡住了。 我不知道如何在一个组件中连接这些限制:
<xsd:simpleType name="unitType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="g"/>
<xsd:enumeration value="Gigabyte"/>
<xsd:enumeration value="mAh"/>
<xsd:enumeration value="Year"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ProducedContent">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1970"/>
<xsd:maxInclusive value="2015"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="ProducedContent">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="unit" type="unitType" use="required"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
我知道它不起作用,但我必须对这个元素设置上述限制:
<Produced unit="Year">2014</Produced>
答案 0 :(得分:2)
您在此XML实例上请求的限制:
<Produced unit="Year">2014</Produced>
将由此XSD强制执行:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="1.0">
<xsd:element name="Produced">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="ProducedContent">
<xsd:attribute name="unit" type="unitType"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="unitType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="g"/>
<xsd:enumeration value="Gigabyte"/>
<xsd:enumeration value="mAh"/>
<xsd:enumeration value="Year"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ProducedContent">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1970"/>
<xsd:maxInclusive value="2015"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>