R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234564

How to allow typed values to be empty with an XML schema?

I have some XML documents over which I have no control whatsoever. Their structure is well-defined, but it is described in a bunch of PDFs, which, despite being very exact, don't make automated validation very tractable. I'm trying to write a XML schema to make (most of) the rules in those PDFs executable.

All the elements are mandatory. But about half of them can be either empty or have simple typed content.

When defining datatypes for these elements, I defined two versions of each: a "normal" one, and another that can be empty. I did this by defining unions with an empty datatype:

<xs:simpleType name="empty">
  <xs:restriction base="xs:string">
    <xs:length value="0"/>
  </xs:restriction>
</xs:simpleType>

<xs:simpleType name="codPostal">
  <xs:restriction base="xs:string">
    <xs:pattern value="^[0-9]{4}-[0-9]{3}$"/>
  </xs:restriction>
</xs:simpleType>
<xs:simpleType name="opt_codPostal">
  <xs:union memberTypes="empty codPostal"/>
</xs:simpleType>

Is there a less repetitive way of doing this?

Upvotes: 7

Views: 13843

Answers (1)

Andrew Skirrow
Andrew Skirrow

Reputation: 3451

You can use xs:nillable.

In XSD

<xs:simpleType name="codPostal">
  <xs:restriction base="xs:string">
    <xs:pattern value="^[0-9]{4}-[0-9]{3}$"/>
  </xs:restriction>
</xs:simpleType>

<xs:element name="OptionalString" type="codPostal" nillable="true" />

In Document

<OptionalString xsi:nil="true" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

This is most useful for non-string types (e.g. datetime etc) as for strings you could just use zero length.

<OptionalString /> 

Unfortunately you need to specify the "nil" attribute on the document. As far as I know, the only non-intrusive way to do what you want is the union type approach that you've already chosen.

Upvotes: 7

Related Questions