I have the following XML Tag
<price currency="euros">20000.00</price>
How do I restrict the currency attribute to one the following:
AND the price to a double?
I just get an error when I try to a type on both, here's what I've got so far:
<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>
Enumerations are a base simple type in the XSD specification containing a list of possible values. Single-valued enumerations are shown as restrictions of the base simple type xs:token , as illustrated below: ? < xs:simpleType name=”GraduationPlanTypeMapType”>
Defining XML Attributes An Attribute can appear 0 or 1 times within a given element in the XML document. Attributes are either optional or mandatory (by default they are optional). The "use" property in the XSD definition is used to specify if the attribute is optional or mandatory.
Overview of XSDAn XSD defines the structure of an XML document. It specifies the elements and attributes that can appear in an XML document and the type of data these elements and attributes can contain.
The numerical value seems to be missing from your price definition. Try the following:
<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>
None of the existing answers to this old question address the real problem.
The real problem was that xs:complexType
cannot directly have a xs:extension
as a child in XSD. The fix is to use xs:simpleContent
first. Details follow...
Your XML,
<price currency="euros">20000.00</price>
will be valid against either of the following corrected XSDs:
<?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>
<?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>
price
from xs:string
to xs:decimal
, but this is not strictly necessary and was not the real problem.xs:decimal
, but this too was not the real problem.The real problem was that xs:complexType
cannot directly have a xs:extension
as a child in XSD; xs:simpleContent
is needed first.
A related matter (that wasn't asked but may have confused other answers):
How could price
be restricted given that it has an attribute?
In this case, a separate, global definition of priceType
would be needed; it is not possible to do this with only local type definitions.
<?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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With