Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate XML variable against XSD in PostGreSQL

I am trying to port an MS SQL script to execute in PostGreSQL. It uses an XML schema collection script that is defined as follows:

CREATE XML SCHEMA COLLECTION [MySchema] AS N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> 
<xsd:element name="MyRootElement"> 
<xsd:complexType> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:sequence> <xsd:element name="Child1" minOccurs="0"> <xsd:complexType> <xsd:complexContent> <xsd:restriction base="xsd:anyType"> <xsd:sequence>
    .
    .

Can I convert this XML schema type for use in PostGreSQL? So I can use the definition for stored procedure xml-type input parameter validation. If not, how do I ensure that the input parameter has the required schema?

I read that The xml type does not validate input values against a document type declaration (DTD), even when the input value specifies a DTD. There is also currently no built-in support for validating against other XML schema languages such as XML Schema

like image 836
Trey Jonn Avatar asked Nov 02 '22 14:11

Trey Jonn


1 Answers

I would write a custom function (in PL/Perl or PL/Python perhaps) that does the required validation, and hook that to the column using a check constraint.

like image 177
Peter Eisentraut Avatar answered Nov 09 '22 12:11

Peter Eisentraut