View as source file or XML.

Schema Lifecycle


This page is a quick presentation on how to use Zorba with XMLSchema type information.


Zorba supports W3C XML Schema (XSD) 1.1 including dateTimeStamp, +/-0, +/-INF. But due to the missing support from Xerces-C validation is still using only XSD 1.0, which is compatible with XSD 1.1 for the vast majority of the features with a few exceptions. Check out the full list.

How to import a schema

In order to use XMLSchema type information in a query a schema file needs to be imported. Note the URI must match the target namespace of schema file.
import schema namespace r = "http://example" at "example.xsd";
For a full example see the Example 1 below.Note: for simplicity, the examples on this page make use of the "at <filename>" clause of import schema to explicitly load a specific schema file. However, when developing applications, it is usually better to omit this clause, and make use of Zorba's extensive URI-resolution mechanisms to load schema documents by URI. For details on this, see URI Resolvers.

Validate expression

Use validate expression to validate an untyped document against an XMLSchema.
validate {$untypeDoc}
For a full example see the Example 1 below.

Example 1

The script example.xq:
import schema namespace r = "http://example" at "example.xsd";

import module namespace file = "";

let $untypeDoc := fn:parse-xml(file:read-text("example.xml"))
let $validatedDoc := validate {$untypeDoc}
Assuming there is an example.xsd file with this content:
<xs:schema xmlns:xs="" 
  elementFormDefault="qualified" attributeFormDefault="unqualified">

  <xs:element name="a" type="TypeA"/>

  <xs:complexType name="TypeA">
      <xs:element name="b" maxOccurs="unbounded" type="xs:string"/>

And this example.xml instance XML document:
<a xmlns="http://example">
  <b>Hello world!</b>
Zorba can execute the query to get the result:
$ zorba --trailing-nl -i -f -q example.xq 
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns="http://example">
  <b>Hello world!</b>

Schema type name

Using the schema module it is possible to find out the QName of a certain schema type.Example 1 can be adapted to the following:
import schema namespace r = "http://example" at "example.xsd";

import module namespace file = "";
import module namespace schema = "";

let $untypeDoc := fn:parse-xml(file:read-text("example.xml"))
let $validatedDoc := validate {$untypeDoc}
  let $qname := schema:schema-type($validatedDoc/r:a)
$ zorba --trailing-nl -i -f -q example.xq 
<?xml version="1.0" encoding="UTF-8"?>
TypeA http://example

Validate using the C++ Zorba API

The C++ Zorba API can be used by an application to validate a document and later passed as a valid input into another query.In the Example 2 first loadProlog method on the static context was used to import the required schema file and than validate method was called to get the new validated item.
  sc->loadProlog("import schema namespace s='' at 'schema.xsd';", hints);
  sc->validate(lChild, vItem);

Example 2

#include <iostream>
#include <sstream>

#include <zorba/zorba.h>
#include <zorba/store_manager.h>
#include <zorba/serializer.h>
#include <zorba/singleton_item_sequence.h>
#include <zorba/zorba_exception.h>

using namespace zorba;

void printItem(Item& lChild, String pre);
void printIter(Iterator_t lIter, String pre);

void printIter(Iterator_t lIter, String pre)
  Item lChild;
  while (lIter->next(lChild))
    printItem(lChild, pre);

void printItem(Item& lChild, String pre)
  Item lNodeName;
  std::cout << pre << "node " << lNodeName.getStringValue() <<
      "   -type " << lChild.getType().getStringValue() << std::endl;
  Iterator_t lAttrIter = lChild.getAttributes();


  Item lAttr;
  while (lAttrIter->next(lAttr))
    Item lNodeName;
    std::cout << pre << "  @" << lNodeName.getStringValue() << "=" << lAttr.getStringValue() <<
        "   -type " << lAttr.getType().getStringValue() << std::endl;

  Iterator_t lChildIter = lChild.getChildren();
  printIter(lChildIter, pre + "  ");

bool example_12(Zorba* aZorba)
  // without validation
  String query = "<a xmlns=''><b attr='1'/><b attr='2'/></a>";
  XQuery_t lQuery1 = aZorba->compileQuery(query);

  Iterator_t lIterator1 = lQuery1->iterator();
  printIter(lIterator1, "  ");

  //with validation
  XQuery_t lQuery2 = aZorba->compileQuery(query);
  Iterator_t lIterator2 = lQuery2->iterator();

  Item lChild;

  StaticContext_t sc = aZorba -> createStaticContext();
  const Zorba_CompilerHints_t hints;
  sc->loadProlog("import schema namespace s='' at 'schema.xsd';", hints);

  Item vItem;
  std::cout << "Validate: " <<std::endl; std::cout.flush();
  bool vres = sc->validate(lChild, vItem);
  std::cout << "  res  : " << (vres ? "true" : "false") << std::endl; std::cout.flush();

  printItem(vItem, "  ");

  return true;

int main(int argc, char **argv)
  std::cout << "Start" << std::endl; std::cout.flush();

  void* lStore = StoreManager::getStore();
  Zorba *lZorba = Zorba::getInstance(lStore);

  bool res = false;

  std::cout << "executing example 12" << std::endl;
  res = example_12(lZorba);
  if (!res) return 1;
  std::cout << std::endl;


  std::cout << "Done!" << std::endl; std::cout.flush();
  return 0;
executing example 12
  node a   -type xs:anyType
    node b   -type xs:anyType
      @attr=1   -type xs:untypedAtomic
    node b   -type xs:anyType
      @attr=2   -type xs:untypedAtomic
  res  : true
  node a   -type __AnonC2
    node b   -type __AnonC3
      @attr=1   -type xs:int
    node b   -type __AnonC3
      @attr=2   -type xs:int