简体   繁体   中英

Using XML Schema how do I define an attribute and restrict text?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM