Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

SQL Server Valid XML

My following schema works and throws an error if an invalid schema is passed in

So if I use this
<root0MwljpDk4TE</root>

Open in new window


This error is thrown
Msg 6909, Level 16, State 1, Line 2
XML Validation: Text node is not allowed at this location, the type was defined with element only content or with simple content. Location: /*:root[1]

But I can't seem to get error handling to work.
If I use womething like this I don;t get the @@Error part returned. Just the initial XML Validation error
DECLARE @x XML(videoSchema)
        
SELECT  @x = '<root>0MwljpDk4TE=</root>'

IF @@error <> 0
   PRINT '@err is ' + ltrim(str(@@error)) + '.'

Open in new window



Heres my schema

CREATE XML SCHEMA COLLECTION videoSchema
    AS'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root" >
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="encString" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>'

DECLARE @x XML(videoSchema)
SELECT @x = '<root><encString>0MwljpDk4TE=</encString></root>'

SELECT @x

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Larry Brister

ASKER

Got me started.

Thanks