使用XML模式如何定义属性和限制文本?

使用XML模式如何定义属性和限制文本?,xml,xsd,complextype,Xml,Xsd,Complextype,从这个例子中: <cost isoCode="GBP">27.45</cost> 27.45 如何定义属性类型并将“27.45”限制为浮点类型 我一直在尝试使用混合ComplexType,但没有任何运气 谢谢 您可以使用xs:simpleContent来实现这一点。以下是起点 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSc

从这个例子中:

<cost isoCode="GBP">27.45</cost>
27.45
如何定义属性类型并将“27.45”限制为浮点类型

我一直在尝试使用混合ComplexType,但没有任何运气


谢谢

您可以使用
xs:simpleContent
来实现这一点。以下是起点

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

我建议不要将浮动等用于货币价值;而是使用小数或整数。