[All Packages]
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.
| xsdctx | Schema processor context |
| xsd | Schema structure |
| 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 |
typedef struct xsdctx xsdctx;
typedef struct xsd xsd;
Note: The contents of xsdctx or xsd are private
(opaque) and must not be accessed by users.
xsdctx *schemaInitialize(xmlctx *ctx);
| ctx | (IN) | XML context used to allocate memory for the Schema context |
xsdctx *schemaLoad(xsdctx *scctx, oratext *uri, oratext *nsp, xsd **schema);
| 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 |
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:
uword schemaValidate(xsdctx *scctx, xmlctx *inst, oratext *schema);
| scctx | (IN) | Schema context |
| inst | (IN) | XML context of instance document to validate |
| schema | (IN) | URI or default Schema |
oratext * schemaTarget(xsd *schema);
| schema | (IN) | Schema structure |
void schemaTerminate(xsdctx *scctx);
| scctx | (IN) | Schema context to terminate |
/* 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;
}