SQL Server XML DML Undeclared prefix

10.1k views Asked by At

I can't understand why I get the error "XML parsing: line 2, character 45, undeclared prefix" in this simple line of code:

DECLARE @ECAS XML;
SET @ECAS = 'declare namespace xs="http://www.w3.org/2001/XMLSchema";
             <xs:element name="ecasData">
               <xs:complexType>
                 <xs:all minOccurs="1" maxOccurs="1"/>
               </xs:complexType>
             </xs:element>';

SELECT @ECAS;

Isn't declared the namespace xs in the begin of the sentence? Any help will be appreciated. Thanks.

2

There are 2 answers

2
podiluska On BEST ANSWER
DECLARE @ECAS XML;

SET @ECAS = '<xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" name="ecasData" >
               <xs:complexType>
                 <xs:all minOccurs="1" maxOccurs="1"/>
               </xs:complexType>
             </xs:element>';
SELECT @ECAS;
1
Peter PitLock On

In the interest of sanity, even though its not directly related to your code, and because this is a top googled post for "undeclared prefix", also look for xsi:nil="true" in your xml