regexxsdunmarshallingxsd-validationsimpletype

xsd:simpleType: Can xsd:pattern and xsd:maxLength work together?


Let's say I am dealing with an xsd:simpleType that is a string, needs to be of a certain character set and of a specific maximum length, similar to the below code:

<xsd:simpleType name="MyType">
    <xsd:restriction base="xsd:string">
    <xsd:pattern value="[0-9]" />
    <xsd:maxLength value="36" />
    </xsd:restriction>
</xsd:simpleType>  

So my xsd type will be a string of only digits and maximum of 36 characters. My question is whether the xsd:pattern and xsd:maxLength (or any other similar tag like minLength) can work together. My intuition is no; either pattern or length-based xsd elements only in the xsd:restriction. Therefore, I would have to add the max length restriction into the pattern.

Please note that I did test this out by unmarshalling an xml on Java and the validation failed. Regardless, what I am looking for is information as two how and whether pattern and maxLength can work together.


Solution

  • All facets on a restriction are applied to a type. This means that your definition above does define a type that has the given pattern and is limited to 36 characters in length.

    Here's the relevant entry from the spec:

    Schema Component Constraint: Simple Type Restriction (Facets) For a simple type definition (call it R) to restrict another simple type definition (call it B) with a set of facets (call this S) all of the following must be true:

    1 The {variety} of R is the same as that of B.

    2 If {variety} is atomic, the {primitive type definition} of R is the same as that of B.

    3 The {facets} of R are the {facets} of B ·overlaid· with S.

    All facets are independently applied, and only if the value meets all restrictions will it be considered valid. This also includes any restrictions placed on the type from which the given type derives.

    It is therefore possible to create a simpleType that will always fail validation - If your pattern were to mandate that the value must be 37 characters long, and the maxLength is 36, then at least one of those facets will always fail.

    That said, multiple pattern elements in a given type are treated as alternatives, (See: "pattern" in "Constraining Facets")