Search code examples
regexxsdspecial-characters

Allow all characters except # and $ in XSD


We have a requirement to allow all the characters (including special chars) except #, $ and space to an XSD element.

I’ve tried the regex as [^$#\s]* but didn’t work. Can you please help with the resolution as I'm not able to figure out.


Solution

  • I tried your regex in a XSD and it works as expected.

    <?xml version="1.0" encoding="utf-16"?>
    <xs:schema xmlns="http://Scratch.SO53903548" targetNamespace="http://Scratch.SO53903548" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="Root">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="SpecialString2">
              <xs:simpleType>
                <xs:restriction base="xs:string">
                  <xs:pattern value="[^$#\s]*" />
                </xs:restriction>
              </xs:simpleType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    Will quite happily validate the below, but fail on $,# or space

    <ns0:Root xmlns:ns0="http://Scratch.SO53903548">
     <SpecialString2>thequickbrownfoxjumpedoverthelazydog@THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG!~`@%^&amp;*()-_+=</SpecialString2>
    </ns0:Root>