Restricting the contents of a typed XML column using triggers


A typed XML column/variable is constrained by a schema collection. That means only instances that can be validated against the schemas in the collection will be accepted. You can tighten the rules a little bit with the use of the DOCUMENT facet, which adds the extra requirement that only well formed XML documents be accepted (no fragments).


 


For some users, this isn’t enough. They would also like to be able to constrain their XML columns or variables to a single namespace or even to a single element.


 


Unfortunately the DDL syntax doesn’t allow for such requirements. There are workarounds though, and we’ll try to look at a simple example.


 


First, let’s create an Xml Schema Collection and a table.


 


CREATE XML SCHEMA COLLECTION myCollection AS '


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"


targetNamespace="http://a">


 


 <xsd:element name="a">


  <xsd:complexType>


   <xsd:sequence>


    <xsd:element name="strElement" type="xsd:string"/>


    <xsd:element name="bElement" type="xsd:boolean"/>


   </xsd:sequence>


  </xsd:complexType>


 </xsd:element>


 


</xsd:schema>


 


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"


targetNamespace="http://b">


 


 <xsd:element name="b">


  <xsd:complexType>


   <xsd:sequence>


    <xsd:element name="strElement" type="xsd:string"/>


    <xsd:element name="floatElement" type="xsd:float" minOccurs="0"/>


   </xsd:sequence>


  </xsd:complexType>


 </xsd:element>


 


</xsd:schema>


 


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"


targetNamespace="http://main"


xmlns:ns="http://main"


xmlns:b="http://b"


xmlns:a="http://a">


 


 <xsd:import namespace="http://a"/>


 <xsd:import namespace="http://b"/>


 


 <xsd:element name="root" type="ns:rootType"/>


 


 <xsd:complexType name="rootType">


  <xsd:choice minOccurs="0" maxOccurs="unbounded">


   <xsd:element ref="a:a"/>


   <xsd:element ref="b:b"/>


  </xsd:choice>


 </xsd:complexType>


 


</xsd:schema>


'


go


 


CREATE TABLE myTable (


date Datetime,


xmlCol XML(myCollection)


)


go


 


At this point any valid instance of elements “root” (namespace “http://main”), “a” (namespace “http://a”) and “b” (namespace “http://b”) can be inserted into the table.


Let’s decide that we should only accept instances of elements from namespace “http://main”.


 


Let’s create the following trigger


 


CREATE TRIGGER CheckNS_OnInsert_Trigger on myTable INSTEAD OF INSERT


AS


BEGIN


 


      INSERT INTO myTable


            SELECT date, xmlCol.query('for $i in /* return (if (namespace-uri($i) = "http://main") then $i else ())')


            FROM inserted


END


 


Now, let’s try the following insertion


 


INSERT INTO myTable VALUES ('1/1/2001 15:00:00', '


<x:a xmlns:x="http://a"><strElement>Data a</strElement><bElement>false</bElement></x:a>


<x:root xmlns:x="http://main"/>


<x:b xmlns:x="http://b"><strElement> Data b</strElement></x:b>


<x:root xmlns:x="http://main">


      <x:a xmlns:x="http://a"><strElement>Data a</strElement><bElement>true</bElement></x:a>


      <x:b xmlns:x="http://b"><strElement> Data b</strElement></x:b>


</x:root>


<x:b xmlns:x="http://b"><strElement/></x:b>'


)


 


Apparently it succeeds (no error message back) but let’s pull the data back out of the table.


 


select xmlCol FROM myTable WHERE date = '1/1/2001 15:00:00'


 


The data looks like this (I pretty-printed it for better legibility)


 


<x:root xmlns:x="http://main" />


<x:root xmlns:x="http://main">


<x:a xmlns:x="http://a">


<strElement>Data a</strElement>


<bElement>true</bElement>


</x:a>


<x:b xmlns:x="http://b">


<strElement> Data b</strElement>


</x:b>


</x:root>


 


All instances of top level elements that were not qualified with namespace “http://main” were eliminated by the trigger.


 


The problem is, we can still perform a DML operation that will insert a top level element from any namespace at the root, like this


 


UPDATE myTable


SET xmlCol.modify('insert <x:b xmlns:x="http://b"><strElement/></x:b> as last into / ')


WHERE date = '1/1/2001 15:00:00'


go


 


When we pull the data out of the table again we get


 


<x:root xmlns:x="http://main" />


<x:root xmlns:x="http://main">


<x:a xmlns:x="http://a">


<strElement>Data a</strElement>


<bElement>true</bElement>


</x:a>


<x:b xmlns:x="http://b">


<strElement> Data b</strElement>


</x:b>


</x:root>


<x:b xmlns:x="http://b"><strElement></strElement></x:b>


 


To remedy this, we need to create another trigger that will catch any “illegal” element(s) inserted during updates.


 


CREATE TRIGGER CheckNS_OnUpdate_Trigger on myTable AFTER UPDATE


AS


BEGIN


      IF UPDATE (xmlCol)


            UPDATE myTable


            SET xmlCol.modify('delete /*[namespace-uri(.)!="http://main"] ')


END


 


 


Now let’s try that update again


UPDATE myTable


SET xmlCol.modify('insert <x:b xmlns:x="http://b"><strElement/></x:b> as last into / ')


WHERE date = '1/1/2001 15:00:00'


go


 


The query seems to succeed but when we query the data out we get this:


 


<x:root xmlns:x="http://main" />


<x:root xmlns:x="http://main">


<x:a xmlns:x="http://a">


<strElement>Data a</strElement>


<bElement>true</bElement>


</x:a>


<x:b xmlns:x="http://b">


<strElement> Data b</strElement>


</x:b>


</x:root>


 


All top level elements from namespaces other than “http://main”, including the one present before we created the trigger are gone.


 


This trigger deletes all top level element whose namespace uri isn’t “http://main”, in all rows. Performance-wise this might become a problem once your table contains more than a few rows. It can probably be made more effective with a primary key and a join on the inserted or delete table. I’ll leave the optimization to you.


 


One last thing is left to check. We must ensure that an element from “http://a” or “http://b” can still be inserted inside an element from “http://main”.


 


UPDATE myTable


SET xmlCol.modify('declare namespace main="http://main";


insert <x:b xmlns:x="http://b"><strElement/></x:b> as first into /main:root[1] ')


WHERE date = '1/1/2001 15:00:00'


go


 


When we query the data out we get this:


 


<x:root xmlns:x="http://main">


<x:b xmlns:x="http://b">


<strElement></strElement>


</x:b>


</x:root>


<x:root xmlns:x="http://main">


<x:a xmlns:x="http://a">


<strElement>Data a</strElement>


<bElement>true</bElement>


</x:a>


<x:b xmlns:x="http://b">


<strElement> Data b</strElement>


</x:b>


</x:root>


 


The first “root” element has now a child “b” qualified with namespace “http://b”.


 


-
Disclaimer:
This posting is provided “AS IS” with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.

Skip to main content