2

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.

Oscar
  • 13,594
  • 8
  • 47
  • 75

2 Answers2

3
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;
podiluska
  • 50,950
  • 7
  • 98
  • 104
  • 1
    Thanks! But now when I insert this fragment in another document where this namespace is declared the declaration is repeated. Is there some nice way to get rid of this without treating the xml as string? – Oscar Aug 27 '13 at 09:56
  • How are you inserting it? – podiluska Aug 27 '13 at 10:12
3

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

Peter PitLock
  • 1,823
  • 7
  • 34
  • 71
  • Was trying to insert into an xml type variable and it was giving me the same error. I used a REPLACE to remove the xsi:nil="true" from the command and now it's ok. :) – Clayton A. Santos Dec 26 '22 at 01:14