The value of NMTOKEN follows the same rules as XML name, except with NMTOKEN any of the allowed characters can be the first character. Unlike XML string type, NMTOKEN values can’t contain any whitespace. Still I fail to see its usefulness.
So when and why would we use NMTOKEN type instead of a string type?
NMTOKENS enables us to specify several NMTOKEN values (separated by whitespace) in a single string. This type makes even less sense, since it also allows a value to contain whitespace characters, which essentially means that both NMTOKENS type and string type can hold exactly the same values.
So when is this type useful?
Description. The lexical and value spaces of xs:NMTOKEN are the set of XML 1.0 “name tokens,” i.e., tokens composed of characters, digits, “.”, “:”, “-”, and the characters defined by Unicode, such as “combining” or “extender”.
An NMTOKEN (name token) is any mixture of Name characters. It cannot contain whitespace (although leading or trailing whitespace will be trimmed/ignored).
NMTOKEN and NMTOKENS exist in XML Schema only for compatibility with DTDs, the predecessor of XML Schema, which had those as attributes types. DTD has few other types so those don't stand out as redundant there.
So use NMTOKEN and NMTOKENS when you're converting a DTD to an XML Schema.
Addendum: Those and other all caps types fall into the same category. They are marked in the XML Schema Rec with the text, "For compatibility NMTOKEN should be used only on attributes."
If they exactly fit your needs, it might be easiest to use just them, but the XML Schema way is to derive from xs:string or xs:token and constrain as needed with a pattern facet. Or, more commonly in my experience, use xs:string or xs:token as-is and call it close enough.
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