5

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.

ecbrodie
  • 11,246
  • 21
  • 71
  • 120
  • 1
    Are these particular elements merely an example for combining restrictions? If not, and you merely want to test minLength, maxLength, or that it is a combination of digits, then you could put that into the regex specified for `xsd:pattern` -- for a minLength of 5 and maxLength of 36 the expression would be `...value="[0-9]{5,36}" />` – Code Jockey Oct 21 '11 at 16:29
  • 1
    If you have tried your example above, and it is not working, and you are asking why, I would guess it's because the regex only allows a single digit, to allow any number of digits, it should be `"[0-9]*"` – Code Jockey Oct 21 '11 at 16:32

1 Answers1

5

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")

Paul Butcher
  • 6,902
  • 28
  • 39
  • For a more complete answer, consider addressing whether or not the pattern expression in the question would reject something that is not a single digit - I can't be sure, but based upon what I've seen, the regex is run against the entire string during validation, so I think the pattern should be `"[0-9]*"` not `"[0-9]"` – Code Jockey Oct 21 '11 at 16:45
  • Good point, I was only considering the question for its own sake, rather than with particular consideration for the example given. The type given in the example is still restricted to 36 characters or fewer by maxLength, but since it is also mandated to be exactly one character long, this restriction is irrelevant. – Paul Butcher Oct 21 '11 at 18:54
  • Since the example provided is itself demonstrating the concept discussed in the second-to-last paragraph, I would change it to use that instead of the 36/37 example... just a thought :D – Code Jockey Oct 21 '11 at 19:43
  • The example demonstrates the exact opposite of that concept. My penultimate paragraph describes types that will never validate, OP's example describes a type where one constraint is rendered irrelevant by another. – Paul Butcher Oct 21 '11 at 20:50
  • 1
    Ah - indeed! Somehow I got in my head that the `maxLength` portion of the OP example was a `length` specification - I must need more sleep! – Code Jockey Oct 21 '11 at 21:01