Using XML Schema how do I define an attribute and restrict text?
-
14-11-2019 - |
Question
From this example:
<cost isoCode="GBP">27.45</cost>
How would I define the attribute type and restrict '27.45' to a float type?
I've been attempting with a mixed ComplexType but not had any luck!
Thanks.
Solution
You can do this by using xs:simpleContent
. Below is the starting point.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="cost">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:float">
<xs:attribute name="isoCode" type="isoCodeType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:simpleType name="isoCodeType">
<xs:restriction base="xs:string">
<xs:enumeration value="GBP" />
<xs:enumeration value="other" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow