user4637238
user4637238

Reputation: 77

XSDs - how to have a possibly negative integer that can also be blank

I'm trying to write some code in an XSD to allow for a single integer character that can be -1 to 9 or blank.

i've tried using

<xs:simpleType name="RdrInteger1Include-1AllowBlank">
    <xs:restriction base="xs:integer">
        <xs:minInclusive value="-1"/>
        <xs:maxInclusive value="9"/>
        <xs:whiteSpace value="collapse"/>
        <xs:pattern value="[0-9]{0,1}"/>
    </xs:restriction>
</xs:simpleType>

though this just returns an error if a blank is used. Can anyone help, i'm a complete novice at this stuff?

Thanks

Upvotes: 2

Views: 592

Answers (2)

Michael Kay
Michael Kay

Reputation: 163322

Michael Sperberg-McQueen has given you one possible answer. Another possibility is to define a datatype that is a list of (your restricted) integers, where the length of the list is constrained to be 0 to 1 inclusive. Both solutions will validate the same input documents, but they will assign types to the data differently, which may affect the usability if you process the data using data binding technologies and/or schema-aware XSLT/XQuery. For example, if @x is an attribute of this type, then in schema-aware XPath, testing (@x=-1) will work as expected if you use my (list) solution, but will raise a type error for Michael's (union) solution in the case where the attribute value is empty.

Upvotes: 2

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

Make a union type with two members. One is the type you've defined above; the other is a restriction of xsd:string that accepts only the empty string, or a single blank, or a sequence of whitespace characters (whatever you understand by blank). Users of the type should point to the union, not to the restriction of integer.

Since the lexical spaces of the two member types are disjoint, it doesn't matter in practice which order you put them in when defining the union.

Upvotes: 2

Related Questions