The value of
NMTOKEN
follows the 开发者_如何学JAVAsame rules as XML name, except withNMTOKEN
any of the allowed characters can be the first character. Unlike XMLstring
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 astring
type?NMTOKENS
enables us to specify severalNMTOKEN
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 bothNMTOKENS
type andstring
type can hold exactly the same values.So when is this type useful?
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.
I think, NMTOKEN are useful for defining values, that respresent HTML5 compatible id values or HTML class names. Contrary to previous HTML versions, HTML5 permits ids that begin with a digit.
NMTOKENS are useful for (comma separated) lists of HTML5 ids or HTML classes.
NMTOKENS is an very useful data type, they allow alphabets, digits and '.', ':' and '-' special characters Refer.
In my experience they are very handy to define multi-value attributes, which can have the above characters. In XML the other method of defining multi-valued attributes would involve restricting a string type with regex, which is more involving (flexible however) than using the NMTOKENS type.
Of course, a different data modelling approach could be taken with elements but NMTOKENS is the shortest mechanism to declare a multi-values attribute.
精彩评论