robbieperry22
robbieperry22

Reputation: 2225

How to define XSD Restrictions for a range or a single number?

I have an XML field that can be either -1, or between 0-100.

If I wanted to define a range, I would simply do:

<xsd:restriction base="xsd:double">
    <xsd:minInclusive value="0.0"/>
    <xsd:maxInclusive value="100.0"/>
</xsd:restriction>

If I wanted to define a specific valid number, I could do:

<xsd:restriction base="xsd:double">
    <xsd:enumeration value="-1.0"></xsd:enumeration>
</xsd:restriction>

But how would I combine them, i.e. a single number, OR a number in the range?

Upvotes: 0

Views: 64

Answers (1)

zx485
zx485

Reputation: 29022

You can do this in two simpleType declarations that declare the value and the range separately, like this:

<xsd:simpleType name="rangeAndValue">
    <xsd:union memberTypes="range">
        <xsd:simpleType>
            <xsd:restriction base="xsd:double">
                <xsd:enumeration value="-1.0" />
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:union>
</xsd:simpleType>

<xsd:simpleType name="range">
    <xsd:restriction base="xsd:double">
        <xsd:minInclusive value="0.0"/>
        <xsd:maxInclusive value="100.0"/>
    </xsd:restriction>
</xsd:simpleType>

To use it with an element, set the type attribute:

<xsd:element name="elem" type="rangeAndValue" />

This should achieve what you want.


If you wanted to add another range, you could add it to the memberTypes attribute like this:

<xsd:union memberTypes="range range2">

with, for example, this range2 simpleType:

<xsd:simpleType name="range2">
    <xsd:restriction base="xsd:double">
        <xsd:minInclusive value="200.0"/>
        <xsd:maxInclusive value="300.0"/>
    </xsd:restriction>
</xsd:simpleType>

Upvotes: 1

Related Questions