Validates an XML document.
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
For information on validating Unicode, see Chapter 6, “XML Support for I18N.”
A basic_string_expression is a sql_query_expression whose datatype is character, varchar, unichar, univarchar, or java.lang.String.
A general_string_expression is a sql_query_expression whose datatype is character, varchar, unichar, univarchar, text, unitext, or java.lang.String.
If any parameter of xmlvalidate() is null, the result of the call is null.
The result datatype of an xmlvalidate_call is the datatype specified by the returns_type.
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
The defaults for validation_options are:
dtdvalidate = See below
schemavalidate = no
schemalocation = " "
nonamespaceschemalocation = " "
xmlerror = exception
xmlvalid = document
Keywords in a validation_option are not case-sensitive, but the schema_uri_list and namespace_schema_uri_list are case-sensitive.
Refer to the document you parse or store as the subject XML document.
The default for dtdvalidate depends on the implicit or explicit value of the schemavalidate option. If the schemavalidate option value is no, the default value of dtdvalidate is no. If the schemavalidate option value is yes, the default dtdvalidate option value is strict.
If you specify schemavalidate = yes, you must either specify dtdvalidate = strict or omit dtdvalidate.
If you specify dtdvalidate = no with schemavalidate = no, the document is checked for well-formedness only.
If you specify schemavalidate = no, the clauses nonamespaceschemalocation and schemalocation are ignored.
The values specified in the clauses nonamespaceschemalocation and schemalocation are character literals. If the Transact-SQL quoted_identifier option is off, you can choose either apostrophes (') or quotation marks (") to surround the option_string, and use the other to surround the values specified by nonamespacescemalocation and schemalocation. If the Transact-SQL quoted_identifier option is on, you must surround the option_string with apostrophes ('), and you must surround the values specified by nonamespacescemalocation and schemalocation by quotation marks (").
nonamespaceschemalocation specifies a list of schema URIs, which overrides the list of schema uris specified in the xsi:noNameSpaceschemalocation clause in the subject XML document.
schemalocation specifies a list of pairs, each pair consisting of a namespace name and a schema URI.
a namespace name is the name an xmlns attribute specifies for a namespace. http://acme.com/schemas.contract is declared as the default namespace in this example:
<contract xmlns="http://acme.com/schemas.contract">
In this example, however, it is declared as the namespace for the prefix "co":
<co:contract xmlns:co="http://acme.com/schemas.contract">
The namespace name is the URI specified in a namespace declaration itself, not the prefix.
A schema_uri is a character string literal that contains a schema URI. The maximum length of a URI_string is 1927 characters, and it must specify http. The schema referenced by a schema_uri must be encoded as either UTF8 or UTF16.
The dtdvalidate option values are:
dtdvalidate=no: No DTD or schema validation is performed; the document is checked to ensure that it is well-formed.
dtdvalidate=yes: The document is validated against any DTD the document specifies.
dtdvalidate=strict: This option depends on the schemavalidate option.
schemavalidate=no:
You must specify a DTD in the subject XML document, and the document is validated against that DTD.
schemavalidate=yes:
You must declare every element in the subject XML document in a DTD or a schema, and each element is validated against those declarations.
The schemavalidate option values are:
If you specify schemavalidate=no, no schema validation is performed for the subject XML document.
If you specify schemavalidate=yes, schema validation is performed.
The following results apply when a general_string_expression, for instance XC, is an XML document that passes the validation options specified in the option_string clause:
If xmlvalid specifies doc, the result of xmlvalidate is:
convert(text, XC)
If xmlvalid specifies message, the result of xmlvalidate is this XML document:
<xmlvalid/>
The following results apply when a general_string_expression is not an XML document that passes the validation options specified in the option_string clause:
If the option_string specifies xmlerror=exception, an exception is raised carrying the exception message.
If option_string specifies xmlerror=message, an XML document of the following form is returned. E1, E2, and so forth are messages that describe the validation errors.
<xml_validation_errors> <xml_validation_error>E1</xml_validation_error> <xml_validation_error>E2</xml_validation_error> ... <xml_validation_warning>W1</xml_validation_warning> <xml_vvalidation_fatal_error>E3<xml_validation_fatal_error> </xml_validation_errors>
If option_string specifies xmlerror=null, a null value is returned.
If the value of the xml_data_expression is not valid XML:
If the explicit or default options specifies xmlerror=exception, an exception is raised.
If the explicit or default options specifies xmlerror=null, a null value will be returned.
If the explicit or default options specifies xmlerror=message, a character string containing an XML element with all the exception messages is returned. This value is valid parsed XML.
If a web resource required for validation is unavailable, an exception occurs.
If the source XML document is either invalid or not well-formed, an exception occurs. Its message describes the validation failure.
The XML DTDs and schemas shown in Table 2-2 illustrate validation clauses.
dtd_emp and schema_emp define a single text element, "<emp_name>"
dtd_cust and schema_cust define a single text element , "<cust_name>"
ns_schema_emp and ns_schema_cust are variants that specify a target namespace.
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 16 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)
Copyright © 2005. Sybase Inc. All rights reserved. |
![]() |