Use an xs:simpleType
and regular expressions to restrict the base xs:string
type. You can have more than one xs:pattern
to keep the alternative patterns simple. The element has to match one of the patterns or validation will fail. Since the patterns are regular expressions, special characters like "[" and "]" have to be escaped when used as literals.
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="elem" type="myElemType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="myElemType">
<xs:attribute name="attrib" type="myAttribType"/>
</xs:complexType>
<xs:simpleType name="myAttribType">
<xs:restriction base="xs:string">
<xs:pattern value="\[EVENT\]"/><!-- "[EVENT]": okay -->
<xs:pattern value="\[PROTOCOL\]"/><!-- "[PROTOCOL]": okay -->
<xs:pattern value="[^\[].*"/><!-- Starts with anything but "[": okay -->
</xs:restriction>
</xs:simpleType>
</xs:schema>
XML:
<root>
<elem attrib="[EVENT]"/>
<elem attrib="[PROTOCOL]"/>
<elem attrib="SomeString"/>
<elem attrib="SomeString]"/>
<elem attrib=" [SomeString] "/>
<!-- All the above are okay; the ones below fail validation -->
<elem attrib="[SomeString]"/>
<elem attrib="[SomeString"/>
</root>
Modify the regular expressions to your heart's content, e.g., to fail the example with leading and/or trailing spaces.
Edited to reflect OP's comment that "[SomeString" should also be invalid.