XML Schema How to Declare Price and Currency

戏子无情 提交于 2019-12-05 21:04:21

问题


I'm creating an XML schema that stores information about houses.

I want to store the price and the 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>

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>

The issue that I have with this:

  • I'm not exactly sure if this will restrict the attribute element to pounds, euros or dollars
  • I can't seem to specify a type on the price to a double, as I would like due to the error:

    Element 'price' has both a 'type' attribute and a 'anonymous type' child. Only one of these is allowed for an element.

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?


回答1:


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>



回答2:


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>



回答3:


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

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




回答4:


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.




回答5:


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!



来源:https://stackoverflow.com/questions/8883514/xml-schema-how-to-declare-price-and-currency

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