XML Schema How to Declare Price and Currency

折月煮酒 提交于 2019-12-04 04:40:32

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>

Taken from the link posted by Michael Kay and applied to your problem. (Note: Use 'decimal' type instead of 'double' to avoid precision errors.)

<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>

You want a "complex type with simple content". You can find an example here:

http://www.w3schools.com/schema/el_simpleContent.asp

or as an extension to Michaels answer:

<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>

if you want it in one tag. that simply adds the attribute with enumeration to a decimal type, so your desired result would be achieved.

As a practice in use, take a look at the implementation of PayPal's SOAP API.

The service defines a set of codes for currencies (CurrencyCodeType defined within eBLBaseComponents.xsd) following the ISO-4217 standard.

AmountType (defined by CoreComponentTypes.xsd) is a composition of a double with an attribute of currencyType.

I'd recommend using a similar approach in your application, restricting the acceptable currencies in your business logic, rather than in the schema.

Cheers!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!