Luk*_*uke 8 xml schema xsd currency
我正在创建一个存储房屋信息的XML模式.
我想存储price和currency.
在我看来,通过将货币作为价格元素的属性来声明这一点是有道理的.
另外,我想限制可输入的currency值为磅,欧元或美元.
例如:
<price currency="euros">10000.00</price>
Run Code Online (Sandbox Code Playgroud)
所以目前我在我的XML Schema中声明这个:
<!-- House Price, and the currency as an attribute -->
<xs:element name="price">
<xs:complexType>
<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:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)
我对此有这样的问题:
pounds, euros or dollars我似乎无法type将价格指定为双倍,因为我希望由于错误:
Element 'price' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.
我应该保持简单并将它们声明为单独的元素:
<price>10000.00</price>
<currency>euros</currency>
Run Code Online (Sandbox Code Playgroud)
......还是我走在正确的道路上?
小智 14
取自Michael Kay发布的链接并应用于您的问题.(注意:使用'decimal'类型而不是'double'来避免精度错误.)
<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:simpleType name="currencyType">
<xs:restriction base="xs:string">
<xs:enumeration value="pounds"/>
<xs:enumeration value="euros"/>
<xs:enumeration value="dollars"/>
</xs:restriction>
</xs:simpleType>
---- Example ----
<price currency="euros">10000.00</price>
Run Code Online (Sandbox Code Playgroud)
以下定义price元素具有一个属性xs:double值,currency该属性的值仅限于以下值之一:英镑、欧元或美元。
<xs:element name="price">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:double">
<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>
Run Code Online (Sandbox Code Playgroud)