Validate pattern OR nothing in XSD

Say you have a XML like this:

:
20110506
:

Where <date> is allowed to be empty OR follow pattern ’19|20[0-9]{2}[0-1][0-9][0-3][0-9]’. How do you validate it? The solution is too simple and that is why I didn’t figure it out myself. I googled it and soon found a much smarter guy than me that said to do it like this:


:

    
                   
            <xs:pattern value="(19|20[0-9]{2}[0-1][0-9][0-3][0-9])|"/>
        
    

:

This validates YYYYMMDD type dates and also validate an empty date tag. The solution lies in the last OR operand ‘|’. You see, either the left one (the pattern) is true or the right one (which is empty). Cleaver ah? I certainly thinks soo

Comments are closed.