XSD datatype
xs:token
A string trimmed of leading and trailing whitespace, with internal runs collapsed to one space. The right type for most identifiers.
xs:token sets whiteSpace to 'collapse': the value is trimmed at both ends and every internal run of whitespace becomes a single space. This happens before any other facet is checked, which is why an enumeration on a token matches values that arrived indented.
For codes, keys, SKUs and anything else that is compared rather than read, this is almost always the type you want instead of xs:string.
- Lexical space
- Any string; collapsed to a trimmed, single-spaced form during validation.
- Derives from
xs:normalizedString- Category
- String types
- Facets
- length, minLength, maxLength, pattern, enumeration, whiteSpace
What validates
Every literal below is put through the schema validator on each test run, against the schema at the foot of this page.
A-1001A-1001two wordsgold
What does not
Nothing — this type accepts any legal XML character content. That is worth knowing: it constrains nothing on its own, so any real restriction has to come from a facet.
What catches people out
- Collapsing happens before facet checks, so an enumeration of 'gold' accepts ' gold '. That is the point, and it surprises people who expected a rejection.
- It does not forbid spaces — 'two words' is a valid token. For a single word use xs:NMTOKEN or a pattern.
- The stored value in most toolchains is the collapsed one, so round-tripping loses the original spacing.
The schema used
This is the exact schema the literals above are validated against. Paste it into the validator with one of them to see the result yourself.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="v" type="xs:token"/>
</xs:schema>Related
- xs:stringAny sequence of legal XML characters, with whitespace preserved exactly. The only string type that does not touch your value.
- xs:normalizedStringA string with tabs, newlines and carriage returns replaced by spaces — but not trimmed and not collapsed.
- xs:NCNameAn XML name with no colon — the type of every element and attribute name, and of the local half of a QName.
- enumerationA closed list of permitted values, compared in the value space — so 01 and 1 are the same number.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.