XML模式如何声明价格和货币 [英] XML Schema How to Declare Price and Currency
问题描述
我正在创建一个存储有关房屋信息的XML模式。
I'm creating an XML schema that stores information about houses.
我想存储价格
和 currency
。
在我看来,通过将货币作为价格元素的属性来声明这一点很有意义。
It makes sense in my mind to declare this by having the currency as an attribute of the price element.
此外,我想将可以作为货币
输入的值限制为英镑,欧元或美元。
Also, I want to restrict the values that can be entered as the currency
to pounds, euros or dollars.
EG:
<price currency="euros">10000.00</price>
因此,目前我在XML模式中将其声明为:
So at the moment I'm declaring this in my XML Schema as:
<!-- 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>
我遇到的问题:
-
我不确定这是否会将attribute元素限制为
英镑,欧元或美元
我似乎无法在价格上指定类型
为两倍,这是由于以下原因我想要的:
I can't seem to specify a type
on the price to a double, as I would like due to the error:
元素 price同时具有 type属性和 anonymous type子元素。元素中仅允许使用其中的一个。
我应该保持简单,并将它们声明为单独的元素:
Should I just keep it simple and declare them as separate elements:
<price>10000.00</price>
<currency>euros</currency>
...还是我在正确的道路上?
...or am I on the right path?
推荐答案
以下内容将 price
元素定义为具有 xs:double
值具有 currency
属性的人的值被限制为以下任一值:英镑,欧元或美元。
The following defines the price
element to have an xs:double
value with a currency
attribute who's values are restricted to a value of either: pounds, euros, or dollars.
<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>
这篇关于XML模式如何声明价格和货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!