"base64Binary" Datatype Values and Representations

This section describes the built-in primitive datatype, 'base64Binary' that represents binary data represented in Base64 encoding. Whitespaces are allowed and removed.

"base64Binary" is a built-in datatype that represents binary data encoded with Base64 algorithm with these rules:

To verify these rules, I wrote this simple XSD document that uses "base64Binary" datatype to declare XML elements:

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- base64Binary_datatype_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:element name="Base64Binary_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="Base64Binary" type="xs:base64Binary" 
         maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

Here is a sample XML document that can be used to test these declarations:

<?xml version="1.1"?>
<!-- base64Binary_datatype_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<Base64Binary_Datatype_Test>

<!-- 4 valid "Base64Binary" elements -->
  <Base64Binary>    QQ==   </Base64Binary>
  <Base64Binary>    aGVyb25neWFuZy5jb20=   </Base64Binary>
  <Base64Binary> a G  V   y   b  2 5neWFuZy5jb20=   </Base64Binary>
  <Base64Binary>    1234   </Base64Binary>

<!-- 4 invalid "Base64Binary" elements -->
  <Base64Binary>    123   </Base64Binary>
  <Base64Binary>    herongyang.com==   </Base64Binary>
  <Base64Binary>    QQ==QQ==   </Base64Binary>
  <Base64Binary>    Q===   </Base64Binary>
</Base64Binary_Datatype_Test>

When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get 4 groups of errors for 4 invalid XML elements:

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator 
   base64Binary_datatype_test.xsd base64Binary_datatype_test.xml

Error:
   Line number: 14
   Column number: 42
   Message: cvc-datatype-valid.1.2.1: '    123   ' is not a valid 
   value for 'base64Binary'. (must be multiples of 4 characters)

Error:
   Line number: 14
   Column number: 42
   Message: cvc-type.3.1.3: The value '    123   ' of element 
   'Base64Binary' is not valid.

Error:
   Line number: 15
   Column number: 55
   Message: cvc-datatype-valid.1.2.1: '    herongyang.com==   ' is not
   a valid value for 'base64Binary'. ("." not allowed)

Error:
   Line number: 15
   Column number: 55
   Message: cvc-type.3.1.3: The value '    herongyang.com==   ' of 
   element 'Base64Binary' is not valid.

Error:
   Line number: 16
   Column number: 47
   Message: cvc-datatype-valid.1.2.1: '    QQ==QQ==   ' is not a valid
   value for 'base64Binary'. ("=" only allowed at the end)

Error:
   Line number: 16
   Column number: 47
   Message: cvc-type.3.1.3: The value '    QQ==QQ==   ' of element 
   'Base64Binary' is not valid.

Error:
   Line number: 17
   Column number: 43
   Message: cvc-datatype-valid.1.2.1: '    Q===   ' is not a valid 
   value for 'base64Binary'. (maximum 2 "=" at the end)

Error:
   Line number: 17
   Column number: 43
   Message: cvc-type.3.1.3: The value '    Q===   ' of element 
   'Base64Binary' is not valid.

Failed with errors: 8

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 XML Editor and Schema Processor - XMLPad

 Java API for XML Processing - JAXP

 JAXP - XML Schema (XSD) Validation

 Xerces2 Java Parser - Java API of XML Parsers

 Using Xerces2 Java API

 XML Schema Language - Basics

 Introduction of XSD Built-in Datatypes

 "string" and Its Derived Datatypes

 "decimal" and Its Derived Datatypes

 "dateTime" and Its Related Datatypes

Miscellaneous Built-in Datatypes

 "anyURI" Datatype Values and Representations

 "QName" Datatype Values and Representations

 "NOTATION" Datatype Values and Representations

"base64Binary" Datatype Values and Representations

 "hexBinary" Datatype Values and Representations

 "float" and "double" Datatype Values and Representations

 "boolean" Datatype Values and Representations

 Facets, Constraining Facets and Restriction Datatypes

 "simpleType" - Defining Your Own Simple Datatypes

 Complex Element Declaration

 Identity-Constraints: unique, key and keyref

 Assertion as Custom Validation Rules

 XML Schema Location and Namespace in XML Documents

 Overriding Element Types in XML Documents

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version