Home > database >  Append restriction pattern via XSD redefine
Append restriction pattern via XSD redefine

Time:11-16

I have a base XSD (base.xsd):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="TenantId">
    <xs:annotation>
      <xs:documentation>
        Represents a tenant id.
      </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Za-z0-9\.]{3,62}" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

that I'm trying to modify this in extension.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:redefine schemaLocation="./base.xsd">
        <xs:simpleType name="TenantId">
            <xs:restriction base="TenantId">
                <xs:pattern value="\{Settings:.*\}" />
            </xs:restriction>
        </xs:simpleType>
    </xs:redefine>
</xs:schema>

to add an additional allowed pattern. I'm validating XML against extension.xsd and strings of the form '{Settings:Something}' are still being flagged as not matching "[A-Za-z0-9.]{3,62}". How do I append additional allowable restriction patterns without modifying the base.xsd file?

CodePudding user response:

Using xs:redefine, you can refine types by restriction or extension.

With restriction, you can only reduce the set of allowed values, you cannot increase it.

Extension allows you to permit extra content, but it doesn't allow you to permit additional values for existing content.

Basically, a schema is a set of rules that your data must conform to, and xs:redefine is designed so that the data will still conform to those rules. You can't use it in the way you are attempting.

If you have XSD 1.1 you could use xs:override, which has a different philosophy.

  • Related