Skip to main content
XMLDir

Search 261 pages — tools, formats, elements, namespaces, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

XSD facet

minLength, maxLength and length

Bounds on size — measured in characters for strings, list items for lists, and bytes for binary.

length fixes an exact size; minLength and maxLength bound it. What is being counted depends on the type: characters for string types, octets for base64Binary and hexBinary, and items for list types.

That variation is the trap. maxLength 10 on an xs:base64Binary means ten decoded bytes, not ten characters of Base64.

Lexical space
A non-negative integer.
Derives from
Primitive type
Category
Constraining facets

What validates

Every literal below is put through the schema validator on each test run, against the schema at the foot of this page.

  • abc

What does not

  • ab

    Shorter than the minLength in the example schema.

What catches people out

  • The unit changes with the type: characters, bytes, or list items.
  • length cannot be combined with minLength or maxLength on the same type.
  • Whitespace processing happens first, so a collapsed token may be shorter than the literal you sent.

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.

schema.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="t"><xs:restriction base="xs:string"><xs:minLength value="3"/><xs:maxLength value="8"/></xs:restriction></xs:simpleType>
  <xs:element name="v" type="t"/>
</xs:schema>

Open the XSD validator

Related

All 29 datatypes and facets

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.