Validate XML element or attribute against multiple patterns in a XSD

Every now and then you need to validate an element or attribute agains multiple patterns.
Say an element is allowed to contain a date both in ‘YYYYnn’ format (where nn is the week number) and ‘seasonYY’ format. To solve this I use the following xsd pattern:

           <xs:pattern value="(19|20[0-9]{2}[0-5][0-9]) | (summer[0-9]{2}) | (winter[0-9]{2}) | (fall[0-9]{2}) | (spring[0-9]{2})"/>

The solution to my problem lies in the ‘|’ and parenthesis. I group the different patterns in parenthesis and separate the patterns with the OR-operator ‘|’. This way I can validate both syntaxes YYYYnn and seasonYY – only one needs to be true

There are of course other ways (shorter) to write the pattern but I choose this way because I believe it is easier to understand.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.