Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a schema restriction that allows an enumeration value or pattern match?

Tags:

regex

xsd

I am defining a simpleType that has a restriction to either be a value from an enumeration or a value matching a pattern. I realize I can do it all from the pattern but I want to have the picklist that the enumeration provides.

This is what I expected to be able to do:

<xs:simpleType name="both">
  <xs:restriction base="xs:string">
    <xs:enumeration value="one" />
    <xs:enumeration value="two" />
    <xs:pattern value="[0..9]+" />
  </xs:restriction>
<xs:simpleType>

But that fails since a value can't match both constraints. If I modify the pattern to allow for any enumerated value then it will fail it if only matches the pattern.

like image 518
Jim McKeeth Avatar asked Jun 20 '12 21:06

Jim McKeeth


1 Answers

Turns out I need a union. Define the enumerated type as a separate type:

<xs:simpleType name="enumeration">
  <xs:restriction base="xs:string">
    <xs:enumeration value="one" />
    <xs:enumeration value="two" />

  </xs:restriction>
<xs:simpleType>

Then create the target type as an enumeration:

<xs:simpleType name="both">
  <xs:union memberTypes="enumeration">
    <xs:simpleType>
      <xs:restriction base="xs:string">
        <xs:pattern value="[0..9]+" />
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>

So now I get the pick list, or match the pattern. Exactly what I wanted!

Update: Can actually define both simple types as children of the union or via the memberTypes attribute.

like image 197
Jim McKeeth Avatar answered Sep 20 '22 14:09

Jim McKeeth