Correctly validate an XML document

UV 10.2

Using the XDOMValidate() function to confirm that a specific XML document is valid according to a specific XML schema requires the XML document to include a reference to the schema item to be used for the validation.

Without namespace

An XML document that is not using namespaces needs to have the attributes xmlns:xsi and xsi:noNamespaceSchemaLocation included in the opening root tag:
 
<Info xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:noNamespaceSchemaLocation=“w3c_example.xsd”/>   

With Namespace

An XML document that is using namespaces needs to have the attributes xmlns:xsi and xsi:schemaLocation added to the tag that defines the namespaces:
 
                     xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
                     xsi:schemaLocation=http://databonnet.sellstuff.com/schema/ prodinfo.xsd”>
 
The xsi:schemaLocation attribute needs to reference the appropriate namespace and the appropriate item id of the schema to be used (separated by whitespace).

Schema Reference

The contents of either the xsi:noNamespaceSchemaLocation and xsi:schemaLocation attributes needs to reference the item ID of a schema item in the same location (file) as the XML being validated.
If it does not, then the validation stops with no errors (as it cannot locate the schema to use to perform the validation), and the XDOMValidate() function returns XML.SUCCESS.
BASIC example
$include UNIVERSE.INCLUDE XML.H
id.XML = “test.xml”
id.XSD = “test.xsd”
stat = XDOMValidate( id.XML, XML.FROM.FILE, id.XSD, XML.FROM.FILE)
if stat = XML.SUCCESS then
   crt squote(id.XML):” is valid according to “:squote(id.XSD)
end else
   hstat = XMLGetError(code.ERR, desc.ERR)
   crt “(stat=”:stat:”) [“:code.ERR:”] “:desc.ERR
end

NOTES:

The item named in id.XML must contain either xsi:noNamespaceSchemaLocation or xsi:schemaLocation as an attribute. As a consequence, the xmlns:xsi attribute is needed to define the xsi: prefix.
The schema item defined in these [NoNamespace]schemalocation attributes MUST exist in the same file as the item named in id.XML.
The item named in id.XSD must exist, but it is not used in the validation so it can be any item at all.
This is solely based on the XML.FROM.FILE source being used. Other source options may behave differently.

Explanation

When using the XDOMValidate() function using the XML.FROM.FILE source, the schema item defined in the [NoNamespace]schemalocation attribute is used as the sole source for the schema when validating. The function seems to ignore the contents of the schema item supplied. If the schema item from the xml document  is not found in the same file as the xml document, the schema validation cannot proceed. No errors are produced about the missing schema and because no schema is loaded the xml document is reported as valid (by virtue of a non-failure result from the XDOMValidate() function).

Leave a comment