我有以下XML标记
<price currency="euros">20000.00</price>
Run Code Online (Sandbox Code Playgroud)
如何将currency属性限制为以下之一:
和价格加倍?
当我尝试在两者上输入类型时,我只是得到一个错误,这是我到目前为止所得到的:
<xs:element name="price">
<xs:complexType>
<xs:attribute name="currency" type="xs:string">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
use*_*692 112
您的价格定义似乎缺少数值.请尝试以下方法:
<xs:simpleType name="curr">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
<xs:element name="price">
<xs:complexType>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="curr"/>
</xs:extension>
</xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
kjh*_*hes 21
这个旧问题的现有答案都没有解决真正的问题.
真正的问题是,在XSD中xs:complexType不能直接拥有xs:extension一个孩子.修复是先使用xs:simpleContent.细节如下......
你的XML,
<price currency="euros">20000.00</price>
Run Code Online (Sandbox Code Playgroud)
将对以下任何一个更正的XSD有效:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="currencyType">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="currency" type="currencyType"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
price,从xs:string到xs:decimal,但这不是绝对必要的,并没有真正的问题.xs:decimal,但这也不是真正的问题.真正的问题是,在XSD中xs:complexType不能直接拥有xs:extension孩子; xs:simpleContent首先需要.
一个相关的问题(没有被问到,但可能混淆了其他答案):
price如果它具有属性,怎么能被限制?
在这种情况下,需要一个单独的全球定义priceType; 仅使用本地类型定义不可能这样做.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="priceType">
<xs:restriction base="xs:decimal">
<xs:minInclusive value="0.00"/>
<xs:maxInclusive value="99999.99"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="priceType">
<xs:attribute name="currency">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
您需要创建一个类型并创建该类型的属性:
<xs:simpleType name="curr">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds" />
<xs:enumeration value="euros" />
<xs:enumeration value="dollars" />
</xs:restriction>
</xs:simpleType>
Run Code Online (Sandbox Code Playgroud)
然后:
<xs:complexType>
<xs:attribute name="currency" type="curr"/>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)