Search code examples
xmlxsdxsd-validationxml-validation

XSD restrict string length to one of two values?


I need an XSD restriction stating that the size has to be either 24 or 44. Doesn't matter which characters are in it, just the size. Currently I have the following:

<xsd:restriction base="xsd:string">
  <xsd:length value="44"/>
</xsd:restriction>

Solution

  • You can use an XSD pattern regex to constrain a string to have one of two lengths:

      <xsd:restriction base="xsd:string">
        <xsd:pattern value=".{24}|.{44}"/>
      </xsd:restriction>
    

    Remember that XSD pattern regex are implicitly anchored at the start (^) and end ($) of the string.