I'm writing an XML schema and need to prevent the text of an element from matching certain values. (Eg. the variableName element cannot match 'int', 'byte', 'string' etc.)
I have tried using a restriction with a pattern element similar to "^(int|byte|string)", but without success.
Do you know the way to format the regular expression, or any other way to make this work?
To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint. Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.
Enumerations enable standard categorizations and tagging to enable standard reporting. In many cases, the controlled vocabulary is defined by rules or policies defined at the state or federal level. Enumerations are a base simple type in the XSD specification containing a list of possible values.
Overview of XSDAn XSD defines the structure of an XML document. It specifies the elements and attributes that can appear in an XML document and the type of data these elements and attributes can contain.
A simple type is a type that only contains text data when expressed according to XML 1.0. This type can be used with element declarations and attribute declarations. The embedded simple type is provided for in XML Schema Part 2. A restriction may be placed on an embedded simple type to create a new, unique simple type.
After triple-checking that XML Schema (XSD) regexes really don't support any of the features that would make this task easy (particularly lookaheads and anchors), I've come up with an approach that seems to work. I used free-spacing mode to make it easier to read, but that's another feature the XSD flavor doesn't support.
[^ibs].* |
i(.{0,1} | [^n].* | n[^t].* | nt.+) |
b(.{0,2} | [^y].* | y[^t].* | yt[^e].* | yte.+) |
s(.{0,4} | [^t].* | t[^r].* | tr[^i].* | tri[^n].* | trin[^g].* | tring.+)
The first alternative matches anything that doesn't start with the initial letter of any of the keywords. Each of the other top-level alternatives matches a string that starts with the same letter as one of the keywords but:
Note that XSD regexes don't support explicit anchors (i.e., ^
, $
, \A
, \z
), but all matches are implicitly anchored at both ends.
One potential problem I can see: if the list of keywords is long, you might run up against a limit on the sheer length of the regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With