Michael Rys

Musings on XML, XQuery and more...

<September 2008>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011


Navigation

Papers

SQL Server XML Whitepapers

Weblogging Links

MS Bloggers

Recommended Books

Other Blogs

Recommended Links

Presentations (Upcoming)

Presentations (Recent)

Subscriptions

News


Upcoming Presentations


TechEd 2007, Orlando, June 4 to June 8, 2007


Books I co-authored



www.flickr.com
This is a Flickr badge showing public photos from Michael Rys. Make your own badge here.
eXTReMe Tracker

Post Categories

Article Categories



How to check for the XML document constraint

During a recent newsgroup exchange, we discussed how to constrain an untyped XML data type to be a document. Kent recapped my statement and Darshan provides an attempt at writing the constraint. Unfortunately, it is not quite correct.

Here is Darshan's statement:

CREATE TABLE t (
 
xcol xml
       
CHECK (xcol.value('count(/*)', 'int')=1)
);

This is not going to catch the invalid document <a/>textnode that is a valid XML data type instance. The general way to constrain the column is the following (Bob recently posted a variety):

CREATE TABLE t (
 
xcol xml
       
CHECK (xcol.value('count(/*)', 'int') = 1 and xcol.exist('/text()')=0)
);

Another interesting alternative is the following (courtesy of John Gallardo):

CREATE TABLE t (
 
xcol xml
       
CHECK (xcol.exist('(/*[position() > 1] , /text())') = 0 and xcol.exist('/*') = 1)
);

I have not tested yet, which of the two expressions will be more efficient.

posted on Friday, March 04, 2005 9:24 AM by mrys


# DOCUMENT without typed XML @ Friday, March 04, 2005 1:05 PM

mrys

# re: DOCUMENT without typed XML @ Friday, March 04, 2005 1:24 PM

mrys

# Using the XQuery methods in CHECK constraints @ Friday, April 01, 2005 3:23 PM

Using the XQuery methods in CHECK constraints

mrys




Powered by Dot Net Junkies, by Telligent Systems