问题
The ultimate goal is to prevent whitespace in a pool of validated XML content by simply not allowing bad xs:token
content to pass schema validation for relevant elements. Schema-invalid instances are not allowed into the pool.
If I declare an element's type to be xsd:token
in an XML Schema (1.1) and I try to validate an instance of this schema where the xsd:token
-typed element contains more than zero of the repudiated characters (tab, LF, CR) or a double, leading or trailing space, will said instance validate or not?
Assume: there's no other "restriction" (so to speak) on the content, only that it has to be an xsd:token
.
Extension just to be totally clear: "The setting xs:whiteSpace=collapse means that leading and trailing whitespace is removed and internal whitespace is reduced to a single x20 character" - I understand that this is a "pre-validation / internal" (so to speak) step for the XML validator; is that right?
回答1:
Your question reveals an incorrect assumption by talking of whitespace "restrictions". The xs:whiteSpace facet does not define restrictions, it define normalizations: ie. what happens to whitespace before validation is applied. In most cases whitespace is collapsed, which means that leading and trailing whitespace is removed, and internal whitespace is reduced to a single space character. If there is a pattern facet then it applies to the value after this whitespace normalization has been done.
For xs:token, note that the name of the type is highly misleading. An instance of xs:token can contain whitespace. The setting xs:whiteSpace=collapse means that leading and trailing whitespace is removed and internal whitespace is reduced to a single x20 character; the result will always be a valid instance of xs:token.
(Of course, the normalized value after validation is of interest only if you are processing the post-validation infoset, for example by using schema-aware XSLT or XQuery. If you are only doing validation to get an error if it's invalid, then xs:token and xs:string are completely equivalent.)
来源:https://stackoverflow.com/questions/40084691/will-an-instance-with-a-xsdtoken-type-element-containing-whitespace-pass-valida