[All Packages]

XML Schema APIs

XML Schema is an alternative to XML DTDs. It defines the document organization (like DTD), but also provides data typing of element contents and additional features. XML Schema provides many built-in datatypes such as scalar, real, date & time, URIs, and encoded binary data. User-defined datatypes can then be build up from those primitive datatypes through extension or restriction. See the Schema specification for full details:
XML Schema is still beta. This version implements the May 2001 W3C Recommendation.


Calling Sequence

The sequence of calls to the processor is initialize, validate, validate, ..., validate, terminate. The initialize call is invoked once at the beginning of a session; it returns a Schema context which is used throughout the session.

 The instance document to be validated is first parsed with the XML parser. The XML context for the instance is then passed to the Schema validate function, along with an optional Schema URL. If no explicit Schema is defined in the instance document, the default Schema will be used. More documents may then be processed using the same Schema context. When the session is over, the Schema terminate function is called, which releases all memory allocated by the loaded Schemas.


Data Types Index

xsdctx Schema processor context 
xsd Schema structure

Function Index

schemaInitialize Initialize the XML Schema processor 
schemaValidate Validate an XML document against a Schema 
schemaTerminate Shut down the Schema processor 
schemaLoad Load an XML schema file offline
schemaTarget Return target namespace URI for schema 

Data Structures and Types


xsdctx

xsd

typedef struct xsd  xsd;


Note: The contents of xsdctx or xsd are private (opaque) and must not be accessed by users.


Functions


schemaInitialize

Purpose

 
Initializes the XML Schema processor; must be called before any Schema validation can take place. The same context may be used repeatedly for validating multiple documents. A pointer to the allocated context is returned, or (xmlscctx *) 0 on error.

 
Syntax
xsdctx *schemaInitialize(xmlctx *ctx);
Parameters

schemaLoad

Purpose

 
Load schema.

 
Syntax
xsdctx *schemaLoad(xsdctx *scctx, oratext *uri, oratext *nsp, xsd **schema);
Parameters
 
scctx (IN) XML context used to allocate memory for the Schema context
uri (IN) URL of schema (in compiler character set)
nsp (IN) Namespace of schema (optional; in compiler character set).
schema (OUT) Returned schema structure
Comments

 

 

To indicate that "nsp" is not provided, specify NULL; to indicate that it's not in any target namespace, specify an empty string (i.e., ""); otherwise, provide a namespace URI such as "http://www.example.com/Report".

If "nsp" is provided, then the following must be all true:
 


schemaValidate

Purpose

 
Validates an instance document against a Schema. Returns zero on success (the document is structurally valid and all datatypes check out), or a non-zero error code on failure.

 
Syntax
uword schemaValidate(xsdctx *scctx, xmlctx *inst, oratext *schema);
Parameters
Comments

 
The default Schema is only used if the instance document does not explicitly refer to any Schemas.


schemaTarget

Purpose

 
Returns target namespace URI for schema

 
Syntax
oratext * schemaTarget(xsd *schema);
Parameters

schemaTerminate

Purpose

 
Tears down the Schema processor, releasing all allocated memory. The context may not be used again.

 
Syntax
void schemaTerminate(xsdctx *scctx);
Parameters

Sample Program

Two of the header files (i.e., lpx0.h and lsx.h) included in this sample program are not needed by the application.  lpx0.h is included for the defintion of LPXP_DATA_ENCODING which can be replaced by (const oratext *) "UTF-8" on ASCII-based platforms or  (const oratext *) "UTFE" for EBCDIC-based platforms.  lsx.h is included to access the version information of the implementation.
/* Copyright (c) 2001, Oracle Corporation.  All rights reserved.  */
 
/*
   NAME
     tlsx.c - Sample Schema validation
 
   DESCRIPTION
     Sample usage of C XML Schema processor
*/
 
#include <stdio.h>

#ifndef ORATYPES
# include <oratypes.h>
#endif
 
#ifndef ORAXML_ORACLE
# include <oraxml.h>
#endif
 
#ifndef ORAXSD_ORACLE
# include <oraxsd.h>
#endif
 

/* Test engine OSD main */

static char *usage =
    "Usage: %s [flags] <instance> [schema] [working dir]\n\
\n\
Where:\n\
    <instance>    is the XML instance document to validate (required)\n\
    [schema]      is the default schema (optional)\n\
    [working dir] is the working directory for processing (optional)\n\
\n\
Flags:\n\
    -0    Always exit with code 0 (success)\n\
    -i    ignore provided schema file\n\
    -u    Unicode input\n";

sb4 main(/* ARGUSED */ sword argc, char *argv[])
{
    xmlctx     *ctx, *tmp;
    xmlnode    *root;
    xsdctx     *scctx;
    char       *doc, *schema, *arg, c;
    uword       ecode;
    sword       i, j, n_left;
    boolean     debug, print, skip, ignoreSchema, unicodeInput;
    boolean     bAlwaysSucceed = FALSE;
    sb4         err;
 
    debug = print = ignoreSchema = FALSE;

    if (debug)
        puts("XML C Schema processor");

    for (i = 1; i < argc; i++)
    {
        arg = argv[i];                  /* arg #i */
        if (arg[0] != '-')              /* not a switch? */
            break;                      /* must be document. */
    for (j = 1; c = arg[j]; j++)        /* allow -xyz */
    {
        skip = FALSE;           /* skip to next arg? */
        switch (c)
        {
        case '0':               /* always return success */
            bAlwaysSucceed = TRUE;
            break;
        case 'u':
            unicodeInput = TRUE;
            break;
        case 'i':
            ignoreSchema = TRUE;
            break;
        case 'p':               /* print document */
            print = TRUE;
            break;
        default:
             printf("Unknown switch -%c\n", c);
             printf(usage, argv[0]);
            return 1;
        }
        if (skip)                       /* skip to next arg? */
        break;
        }
    }

    if ((n_left = argc - i) < 1)
    {
         printf(usage, argv[0]);
        if (bAlwaysSucceed)
            return 0;
        else
            return 1;
    }

    doc = argv[i];
    schema = (n_left > 1) ? argv[i + 1] : 0;
    if (n_left > 2)                     /* change working directories */
         chdir((const char *) argv[i + 2]);

    if (debug)
             printf("Initializing XML package...\n");

    if (unicodeInput)
    {
        if (!(ctx = xmlinit(&ecode, (const oratext *) "UTF-8",
                        (void (*)(void *, const oratext *, uword)) 0, 
                        (void *) 0, (const xmlsaxcb *) 0, (void *) 0,
                        (const xmlmemcb *) 0, (void *) 0, 
                        (const oratext *) 0)))
        {
             printf("Failed to initialze XML parser, error %u\n", 
                        (unsigned) ecode);
            if (bAlwaysSucceed)
                return 0;
            else
                return 1;
        }
    }
    else
    {
        if (!(ctx = xmlinitenc(&ecode, (const oratext *) 0, 
                        "UTF-8",
                        (void (*)(void *, const oratext *, uword)) 0,
                        (void *) 0, (const xmlsaxcb *) 0, (void *) 0,
                        (const xmlmemcb *) 0, (void *) 0,
                        (const oratext *) 0)))
        {
             printf("Failed to initialze XML parser, error %u\n",
                        (unsigned) ecode);
            if (bAlwaysSucceed)
                return 0;
            else
                return 1;
        }
    }

    if (debug)
             printf("Parsing '%s' ...\n", doc);

    if (ecode = xmlparse(ctx, (oratext *) doc, (oratext *) 0,
                         XML_FLAG_DISCARD_WHITESPACE))
    {
         printf("Parse of %s failed, error %u\n", doc, 
                        (unsigned) ecode);

        if (bAlwaysSucceed)
            return 0;
        else
            return 2;
    }

    if (debug)
         printf("Parsing '%s' ...\n", schema);

    if (unicodeInput)
    {
        if (!(tmp = xmlinit(&ecode, (const oratext *) "UTF-8",
                        (void (*)(void *, const oratext *, uword)) 0, 
                        (void *) 0, (const xmlsaxcb *) 0, (void *) 0,
                        (const xmlmemcb *) 0, (void *) 0, 
                        (const oratext *) 0)))
        {
             printf("Failed to initialze XML parser, error %u\n", 
                       (unsigned) ecode);

            if (bAlwaysSucceed)
                return 0;
            else
                return 3;
        }
    }
    else
    {
        if (!(tmp = xmlinitenc(&ecode, (const oratext *) 0,
                        "UTF-8",
                        (void (*)(void *, const oratext *, uword)) 0,
                        (void *) 0, (const xmlsaxcb *) 0, (void *) 0,
                        (const xmlmemcb *) 0, (void *) 0,
                        (const oratext *) 0)))
        {
             printf("Failed to initialze XML parser, error %u\n",
                       (unsigned) ecode);
            if (bAlwaysSucceed)
                return 0;
            else
                return 3;
        }
    }
    if (ecode = xmlparse(tmp, (oratext *) schema, (oratext *) 0,
                         XML_FLAG_DISCARD_WHITESPACE))
    {
         printf("Parse of %s failed, error %u\n", schema, 
                       (unsigned) ecode);
        if (bAlwaysSucceed)
            return 0;
        else
            return 4;
    }

    xmlterm(tmp);

    root = getDocumentElement(ctx);

    if (debug)
        puts("Initializing Schema package...");

    if (!(scctx = schemaInitialize(ctx, &ecode)))
    {
             printf("Failed, code %u!\n", ecode);
        if (bAlwaysSucceed)
            return 0;
        else
            return 5;
    }

    if (debug)
        puts("Validating document...");

    if (ecode = schemaValidate(scctx, root, ignoreSchema ? (oratext *) 0 :
                                            (oratext *) schema))
    {
        if (debug)
             printf("Validation failed, error %u\n", 
                           (unsigned) ecode);
        return 6;
    }
    else
    {
        puts("Document is valid.");

    }
 
    return 0;
}