开发者

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

开发者 https://www.devze.com 2023-03-01 11:01 出处:网络
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 valid

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">
    <开发者_StackOverflow;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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号