【问题标题】:Using XML Schema how do I define an attribute and restrict text?使用 XML Schema 如何定义属性和限制文本?
【发布时间】:2012-02-29 07:52:55
【问题描述】:

从这个例子:

<cost isoCode="GBP">27.45</cost>

如何定义属性类型并将“27.45”限制为浮点类型?

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

谢谢。

【问题讨论】:

  • 我建议永远不要使用 float 之类的货币价值;而是使用小数或整数。

标签: xml xsd complextype


【解决方案1】:

您可以使用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>

【讨论】:

    猜你喜欢
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多