问题
When specifying a list of enumeration values for a DTD attribute definition, is it possible to use multi-word values?
<!ATTLIST SystemName Case ("MIXED RESPECT"|"MIXED IGNORE"|"LOWER RESPECT"|"LOWER IGNORE"|"UPPER RESPECT"|"UPPER IGNORE") "MIXED IGNORE">
I tried putting the values in quotes (as shown), and not in quotes. I keep reading about notations and nmtokens but every single place I look seems to say "see the blah-blah spec" but nary an example. Examples being worth about a million pages of specs.
回答1:
No, enumerated values cannot contain space; each enumerated value must be a legal NMTOKEN, which means it must be a sequence of characters which can occur in an XML Name. So MIXED, Mixed, RESPECT, MIXED-RESPECT, and MIXED_RESPECT are legal values in an enumeration, but not "MIXED RESPECT". Sorry.
The reason, for what it's worth, is historical: in SGML (from which XML was derived), an optional feature of the language allowed attribute names to be omitted if they were declared with an enumerated list of possible values. So instead of
<SystemName Case="MIXED_RESPECT">...
an author could write
<SystemName MIXED_RESPECT>...
And instead of <table border="noborder">
or <table border="border">
authors could just write <table border>
or <table noborder>
. In order to ensure that the values were parsable in context, they were constrained to be legal NMTOKENs.
One downside of the feature was that in order to ensure that this magic trick was always possible, SGML forbade the enumerated values of any two attributes to overlap, so you couldn't have two attributes with yes
and no
as the legal values (because then a parser seeing <table yes>
might not know whether it means border="yes"
or compact="yes"
).
The minimization feature was dropped in XML (like all the other minimization features that made it such an adventure to write an SGML parser), but the constraint which made it possible was retained, in order to ensure that all XML DTDs would be legal as SGML DTDs.
来源:https://stackoverflow.com/questions/12494690/specify-a-multi-word-enumeration-value-in-an-xml-dtd