Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

types

xsd

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?

like image 917
R. Martinho Fernandes Avatar asked Apr 28 '11 10:04

R. Martinho Fernandes


1 Answers

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.

like image 139
Andrew Skirrow Avatar answered Sep 28 '22 03:09

Andrew Skirrow