DB2 Scalar functions - XMLVALIDATE
The XMLVALIDATE function returns a copy of the input XML value augmented with information obtained from XML schema validation, including default values.
The schema is SYSIBM. The function name cannot be specified as a qualified name.
The data type of the result is XML. If the value of XML-expression can be null, the result can be null; if the value of XML-expression is null, the result is the null value.
The XML validation process is performed on a serialized XML value. Because XMLVALIDATE is invoked with an argument of type XML, this value is automatically serialized before validation processing with the follow two exceptions.
To validate a document whose root element does not have a namespace, an xsi:noNamespaceSchemaLocation attribute must be present on the root element.
If an XML schema for validation is explicitly specified with the ACCORDING TO XMLSCHEMA clause, the schema location information specified in the input XML value is ignored.
If the XML schema information is not specified with the ACCORDING TO XMLSCHEMA clause, the input XML value must contain XML schema location information (SQLSTATE 2200M). The schema location information in the input XML value, a namespace name, and a schema location specifies the XML schema document in the XML schema repository used for validation.
If you use an XML schema that defines an element that has a maxOccurs attribute value that is greater than 5000 and you want to reject XML documents that have a maxOccurs attribute value greater than 5000, you can define a trigger or procedure to check for that condition. In the trigger or procedure, use an XPath expression to count the number of occurrences of the element and return an error if the number of elements exceeds the maxOccurs attribute value.
For example, the following trigger ensures that a document never has more than 6500 phone elements:
CREATE TRIGGER CUST_INSERT AFTER INSERT ON CUSTOMER REFERENCING NEW AS NEWROW FOR EACH ROW MODE DB2SQL BEGIN ATOMIC SELECT CASE WHEN X <= 6500 THEN 'OK - Do Nothing' ELSE RAISE_ERROR('75000', 'TooManyPhones') END FROM ( SELECT XMLCAST(XMLQUERY('$INFO/customerinfo/count(phone)') AS INTEGER) AS X FROM CUSTOMER WHERE CUSTOMER.CID = NEWROW.CID ); END
INSERT INTO T1(XMLCOL) VALUES (XMLVALIDATE(?))
Assume that the input parameter marker is bound to an XML value that contains the XML schema information.
<po:order xmlns:po="http://my.world.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://my.world.com http://my.world.com/world.xsd" > ... </po:order>
Based on these assumptions, the input XML value will be validated according to that XML schema.
INSERT INTO T1(XMLCOL) VALUES ( XMLVALIDATE( ? ACCORDING TO XMLSCHEMA ID PODOCS.WORLDPO ) )
Assuming that the XML schema that is associated with SQL name FOO.WORLDPO is found in the XML repository, the input XML value will be validated according to that XML schema.
INSERT INTO T1(XMLCOL) VALUES ( XMLVALIDATE( ? ACCORDING TO XMLSCHEMA ID FOO.WORLDPO NAMESPACE 'http://my.world.com/Mary' ELEMENT "po" ) )
Assuming that the XML schema that is associated with SQL name FOO.WORLDPO is found in the XML repository, the XML schema will be validated against the element "po", whose namespace is 'http://my.world.com/Mary'.
INSERT INTO T1(XMLCOL) VALUES ( XMLVALIDATE( ? ACCORDING TO XMLSCHEMA URI 'http://my.world.com' LOCATION 'http://my.world.com/world.xsd' ) )
Assuming that an XML schema associated with the target namespace "http://my.world.com" and by schemaLocation hint "http://my.world.com/world.xsd" is found in the XML schema repository, the input XML value will be validated according to that schema.