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 datatype

xs:base64Binary and xs:hexBinary

The two ways to carry bytes in XML. base64Binary tolerates whitespace; hexBinary does not.

xs:base64Binary holds standard Base64, and whitespace within it is ignored — which is what makes it usable for the long values that get wrapped across lines. xs:hexBinary holds an even number of hex digits and allows no whitespace at all.

hexBinary is about twice the size on the wire for the same payload, so base64Binary is the usual choice for anything large.

Lexical space
Base64 characters with optional whitespace, or an even count of hex digits.
Derives from
Primitive type
Category
Other 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.

  • SGVsbG8=
  • SGVs bG8=
  • (the empty string)

What does not

  • SGVsbG8

    Base64 must be correctly padded to a multiple of four.

  • Hello!

    '!' is not a Base64 character.

  • 0F1E2D

    Valid xs:hexBinary, but not valid Base64 — the two are not interchangeable.

What catches people out

  • Length facets count decoded bytes, not the characters of the literal. maxLength 10 on base64Binary means ten bytes.
  • hexBinary needs an even number of digits — an odd count is a validation error.
  • The empty string is valid for both, meaning zero bytes.

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:element name="v" type="xs:base64Binary"/>
</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.