1
votes

Is there a way to restrict certain words from being allowed by using the pattern restriction in XSD?

The XML file will contain certain variable names. All names are allowed except for 3 reserved names, let's call them red, green, and blue. The reserved names would most likely never be used anyway, but I need to validate that using the XSD file. Below is an example of what the XSD would look like if I was to allow only those names. Is there any way to negate this though?

<xs:simpleType name="ParameterName">
    <xs:restriction base="xs:string">
         <xs:pattern value="red|green|blue"/>
    </xs:restriction>
</xs:simpleType> 
1
This is similar, but the difference is that in my case I can allow "1red" or "red1" or anything that contains "red". The only thing I can't allow is "red" by itself. - webe0316
I'm struggling with this same question. Coincidentally, my type is also called ParameterName. I need it to match any [a-z]+ except the values "ts" and "type". It doesn't help that XML regexes are so limited: regular-expressions.info/xml.html - Kevin

1 Answers

0
votes

Try this:

<xs:simpleType name ="ParameterName">
    <xs:restriction base ="xs:string">
      <xs:pattern value ="^((?!red|green|blue)|((red|green|blue).+)).*$" />
    </xs:restriction>
</xs:simpleType>