Search code examples
regexxmlxsdxsd-validationxml-validation

XSD 1.0 regex to not match two specific hexadecimal strings


I've been trying to come up with a regex in an XML Schema to match a hex string in the range of 000001-FFFFFE. I can get either 000000 or FFFFFF to not match, but not both within one expression.

Strings that should match: EFEFEF, EEEEEF, FFFFFE, 101010, 10000, 000001

Strings that shouldn't match: 000000, FFFFFF

Anyone have any ideas? I've been beating my head against my keyboard for a few days over this.

In short, all 6-digit hexadecimal values except 000000 and FFFFFF should be allowed.


Solution

  • The following XSD, requiring only XSD 1.0,

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:simpleType name="OneHexDigitNot0">
        <xs:restriction base="xs:string">
          <xs:pattern            value="[1-9A-F][0-9A-F]{5}"/>
          <xs:pattern value="[0-9A-F]{1}[1-9A-F][0-9A-F]{4}"/>
          <xs:pattern value="[0-9A-F]{2}[1-9A-F][0-9A-F]{3}"/>
          <xs:pattern value="[0-9A-F]{3}[1-9A-F][0-9A-F]{2}"/>
          <xs:pattern value="[0-9A-F]{4}[1-9A-F][0-9A-F]{1}"/>
          <xs:pattern value="[0-9A-F]{5}[1-9A-F]"/>
        </xs:restriction>
      </xs:simpleType>
      <xs:simpleType name="OneHexDigitNot0AndOneHexDigitNotF">
        <xs:restriction base="OneHexDigitNot0">
          <xs:pattern            value="[0-9A-E][0-9A-F]{5}"/>
          <xs:pattern value="[0-9A-F]{1}[0-9A-E][0-9A-F]{4}"/>
          <xs:pattern value="[0-9A-F]{2}[0-9A-E][0-9A-F]{3}"/>
          <xs:pattern value="[0-9A-F]{3}[0-9A-E][0-9A-F]{2}"/>
          <xs:pattern value="[0-9A-F]{4}[0-9A-E][0-9A-F]{1}"/>
          <xs:pattern value="[0-9A-F]{5}[0-9A-E]"/>
        </xs:restriction>
      </xs:simpleType>
    
      <xs:element name="r" type="OneHexDigitNot0AndOneHexDigitNotF"/>
    </xs:schema>
    

    will successfully validate XML documents with an r root element and 6-digit hexadecimal content, except 000000 and FFFFFF, as requested.