xmlvalidate

Validates an XML document.

Syntax

 xmlvalidate_call ::=
      xmlvalidate ( general_string_expression, [optional_parameters])
 optional_parameters ::= options_parameter 
      | returns_type
      |options_parameter returns type
 options_parameter ::= [,] option option_string
 options_string ::= basic_string_expression 
 returns_type ::= [,] returns string_type
 string_type ::=char (integer) | varchar (integer) 
     | unichar (integer) |univarchar (integer) 
     | text | unitext |image | java.lang.String

Description

NoteFor information on validating Unicode, see Chapter 6, “XML Support for I18N.”

Options

The general format of the option_string is described in “option_strings: general format”. The options supported for xmlvalidate are:

validation_options::=       [dtdvalidate = {no | yes | strict}]       [schemavalidate = {no | yes}]       [nonamespaceschemalocation = 'schema_uri_list']       [schemalocation = 'namespace_schema_uri_list']       [xmlerror = {exception | null | message }]       [xmlvalid = {document | message}]  schema_uri_list::=       schema_uri [schema_uri]...  namespace_schema_uri_list ::=       namespacename schema_uri       [ namespacename schema_uri]...  schema_uri ::= character_string  namespacename ::= character_string

Options description

Exceptions

If the value of the xml_data_expression is not valid XML:

Examples

The XML DTDs and schemas shown in Table 2-2 illustrate validation clauses.

Table 2-2: Example DTDs and schemas, and their URIs

URI

Document

http://test/dtd_emp.dtd
<!ELEMENT emp_name (#PCDATA)>
http://test/dtd_cust.dtd
<!ELEMENT cust_name (#PCDATA)>
http://test/schema_emp.xsd
<xsd:schema xmlns:xsd   ="http://www.w3.org/2001/XMLSchema" targetNamespace   ="http://test/ns_schema_emp">
<xsd:element name="emp_name"        type="xsd:string"/> 

</xsd:schema>
http://test/ns_schema_emp.xsd
<xsd:schema xmlns:xsd   ="http://www.w3.org/2001/XMLSchema" targetNamespace   ="http://test/ns_schema_emp">
<xsd:element name="emp_name"        type="xsd:string"/>

</xsd:schema>
http://test/schema_cust.xsd
<xsd:schema xmlns:xsd
  ="http://www.w3.org/2001/XMLSchema">
<xsd:element name="cust_name"
  type="xsd:string"/>

</xsd:schema>
http://test/ns_schema_cust.xsd
<xsd:schema xmlns:xsd
   ="http://www.w3.org/2001/XMLSchema"   targetNamespace
   ="http://test/ns_schema_cust">
<xsd:element name="cust_name"
   type="xsd:string"/>

</xsd:schema>

Example 1 This example creates a table in which to store XML documents in a text column. Use this table to show example calls of xmlvalidate. In other words, xmlvalidate explicitly validates documents stored in the text column.

create table text_docs(xml_doc text null)

Example 2 This example shows xmlvalidate specifying a document with no DTD declaration, and the validation option dtdvalidate=yes. The command succeeds because the inserted document is well-formed, and dtdvalidate is not specified as strict.

insert into text_docs
values (xmlvalidate(
  '<employee_name>John Doe</employee_name>',
  option 'dtdvalidate=yes'))
---------
(1 row inserted)

Example 3 This example shows xmlvalidate specifying a document with no DTD declaration and the validation option dtdvalidate=strict. xmlvalidate raises an exception, because strict DTD validation requires every element in the document to be specified by a DTD.

insert into text_docs
values(xmlvalidate(
  '<emp_name>John Doe</emp_name>',
option 'dtdvalidate=strict'))
--------
EXCEPTION

Example 4 The last example raised an exception when validation failed. Instead, you can use the option xmlerror to specify that xmlvalidate should return null when validation fails.

insert into text_docs
values(xmlvalidate(
  '<emp_name>John Doe</emp_name>'
option 'dtdvalidate=strict xmlerror=null'))
-------
null

Example 5 You can also use xmlerror to specify that xmlvalidate should return the XML error message as an XML document when validation fails:

insert into text_docs
values(xmlvalidate(
  '<emp_name>John Doe</emp_name>'
option 'dtdvalidate=strict xmlerror=message'))
--------
<xml_validation_errors>
<xml_validation_error>(1:15)Document is invalid:
  no grammar found.<xml_validation_error>
<xml_validation_error>(1:15)Document root element    "employee name",must match DOCTYPE root    "null."</xml_validation_error>
</xml_validation_errors>

Example 6 This example shows xmlvalidate specifying a document that references both a DTD and the validation option dtdvalidate=yes. This command succeeds.

insert into text_docs
values(xmlvalidate(
  '<DOCTYPE emp_name PUBLIC "http://test/dtd_emp.dtd">
  <emp_name>John Doe</emp_name>',
option 'dtdvalidate=yes'))
-------
(1 row inserted)

Example 7 This example shows xmlvalidate specifying a document that references a DTD and the validation option dtdvalidate=yes. xmlvalidate raises an exception, because the inserted document does not match the DTD referenced in the document.

insert into text_docs
values(xmlvalidate(
  '<DOCTYPE emp_name PUBLIC "http://test/dtd_cust.dtd">
<emp_name>John Doe</emp_name>',
option 'dtdvalidate=yes'))
--------
EXCEPTION

Example 8 This example shows xmlvalidate specifying a document with no schema declaration and the validation option schemavalidate=yes. This command fails because the '<emp_name>' element has no declaration.

insert into text_docs
values(xmlvalidate('<emp_name>John Doe</emp_name>',
  option 'schemavalidate=yes'))
-------
EXCEPTION

Example 9 This example shows xmlvalidate specifying a document with a schema declaration and the validation option schemavalidate=yes. This document does not use namespaces. The command succeeds, because the document matches the schema referenced in the document.

insert into text_docs
values(xmlvalidate(
  '<emp_name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi: noNamespaceSchemaLocation="http://test/schema_emp.xsd">
  John Doe</emp_name>'
 option 'schemavalidate=yes'))
--------
(1 row inserted)

Example 10 This example shows xmlvalidate specifying a document that specifies a namespace and the validation option schemavalidate=yes. The command succeeds, because the document matches the schema referenced in the document.

insert into text_docs
values(xmlvalidate(
  '<emp:emp_name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:emp="http://test/ns_schema_emp"
   xsi: SchemaLocation="http://test/ns_schema_emp       http://test/ns_schema_emp.xsd">
  John Doe</emp:emp_name>'
 option 'schemavalidate=yes'))
--------
(1 row inserted)

Example 11 This example shows xmlvalidate specifying a document with a schema declaration and the validation option schemavalidate=yes. This command fails, because the document doesn’t match the schema referenced in the document.

insert into text_docs
values (xmlvalidate(
  '<emp_name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://test/schema_cust.xsd">
   John Doe</emp_name>'
 option 'schemavalidate=yes'))
-------
EXCEPTION

Example 12 This example shows xmlvalidate specifying a document with a schema declaration and the validation option schemavalidate=yes. This document specifies a namespace. The command fails, because the document doesn’t match the schema referenced in the document.

insert into text_docs
values(xmlvalidate(
  '<emp:emp_name
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:emp="http://test/ns_schema_cust"
    xsi:schemaLocation=
       "http://test/ns_schema_cust http://test/ns_schema_cust.xsd">
    John Doe</emp:emp_name>',
   option 'schemavalidate=yes'))
------------
EXCEPTION

The validation options of xmlvalidate specify a nonamespaceschemalocation of http://test/ns_schema_emp.xsd.

Example 13 This example shows xmlvalidate specifying a document with a schema declaration and the validation option schemavalidate=yes, as well as the clauses schemalocation and nonamespaceschemalocation.

The document specifies a schemaLocation of http://test/schema_cust.xsd, and the validation option in xmlvalidate specifies a schemalocation of http://test/ns_schema_emp.xsd.

This command succeeds, because the document matches the schema referenced in xmlvalidate, which overrides the schema referenced in the document.

insert into text_docs
values (xmlvalidate(
  '<emp:emp_name
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:emp="http://test/schema_emp"
    xsi:schemaLocation="http://test/ns_schema_emp          http://test/schema_cust.xsd">
    John Doe</emp:emp_name>',
   option 'schemavalidate=yes,
    schemalocation= "http://test/ns_schema_emp          http://test/ns_schema_emp.xsd"
    nonamespaceschemalocation="http://test/schema_emp.xsd" '))
-------------
(1 row inserted)

Example 14 This example shows xmlvalidate specifying a document with a schema declaration and the validation option schemavalidate=yes, as well as the clauses schemalocation and nonamespaceschemalocation.

The document specifies a noNamespaceSchemaLocation of http://test/schema_cust.xsd, and the validation option in xmlvalidate specifies a nonamespaceschemalocation of http://test/ns_schema_emp.xsd.

This command fails, because the document doesn’t match the schema referenced in xmlvalidate. The document does, however, match the schema referenced in the document.

insert into text_docs
values(xmlvalidate(
  '<customer_name
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://test/schema_cust.xsd">
  John Doe</customer_name>'
  option 'schemavalidate=yes,
schemalocation="http://test/ns_schema_emp http://test/ns_schema_emp.xsd"
    nonamespaceschemalocation="http://test/schema_emp.xsd" '))
-----------
EXCEPTION

Example 15 This example shows xmlvalidate specifying a document with a schema declaration and the validation option schemavalidate=yes, as well as the clauses schemalocation and nonamespaceschemalocation

The document specifies a schemaLocation of http://test/schema_cust.xsd, and the validation option of xmlvalidate specifies a schemalocation of http://test/ns_schema_emp.xsd.

This command fails, because the document doesn’t match the schema referenced in xmlvalidate. The document does, however, match the schema referenced in the document.

insert into text_docs
values(xmlvalidate(
  '<cust:cust_name
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:cust="http://test/schema_cust"
    xsi:schemaLocation="http://test/schema_cust           http://test/schema_cust.xsd">
   John Doe</cust:cust_name>',
  option 'schemavalidate=yes,
   schemalocation="http://test/ns_schema_emp http://test/ns_schema_emp.xsd"
   nonamespaceschemalocation="http://test/schema_emp.xsd" '))
-------
(1 row inserted)