Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating empty xml by xsd in SQL Server

Tags:

sql

sql-server

I have following simple xsd schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="MyUrl" type="xsd:anyURI"/>
</xsd:schema>'

I have SQL Server 2008 R2 and I want to validate my variable against this schema. It works, but variable x got validated even if it's empty or whitespace, however empty xml isn't valid against this schema.

Why I got these results?

TSQL code:

CREATE XML SCHEMA COLLECTION dbo.xsdTest AS
N'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="MyUrl" type="xsd:anyURI"/>
</xsd:schema>'
GO

DECLARE @x XML(dbo.xsdTest)
SET @x = ' ' --no error
like image 643
msergey Avatar asked Feb 28 '26 20:02

msergey


1 Answers

By default the XML data type accepts XML fragments as valid XML.

The XML data can contain multiple zero or more elements at the top level.

You can specify that the XML must be a valid XML document like this.

declare @x xml(document dbo.xsdTest)
set @x = '' -- error here
like image 115
Mikael Eriksson Avatar answered Mar 03 '26 12:03

Mikael Eriksson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!