XML解析错误非法限定名称字符

时间:2016-08-19 20:36:10

标签: sql sql-server xml tsql encoding

我正在使用SQL Server 2008 Management Studio,我尝试将一些XML数据插入到我的表中。我从来没有这样做过,你能不能解释一下为什么SQL会抛出错误?

这是我的数据:

select 
    IDPatient, IDObjet, Nplan, dateCreation, dateModification, actif,
    case 
       when ISNULL(unite_20,'') + ISNULL(date_20,'') <> '' 
          then 20
       when ISNULL(unite_19,'') + ISNULL(date_19,'') <> '' 
          then 19 
       when ISNULL(unite_18,'') + ISNULL(date_18,'') <> '' 
          then 18 
       when ISNULL(unite_17,'') + ISNULL(date_17,'') <> '' 
          then 17 
       when ISNULL(unite_16,'') + ISNULL(date_16,'') <> '' 
          then 16 
       when ISNULL(unite_15,'') + ISNULL(date_15,'') <> '' 
          then 15
       ... and so forth .....
       when ISNULL(unite_1,'') + ISNULL(date_1,'') <> '' 
          then 1 
    End As NbrAdministration,
    0,      
    '<IDObjet>'+ 
 case when ISNULL(unite_1,'') + ISNULL(date_1,'') <> '' then
 '<Administration1>'+
 '<Unite>'+isnull(convert(varchar,Unite_1),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_1,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_1,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_1,'')+'</Initiale>'+
 '<Site>'+isnull(Site_1,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_1),'')+'</IDRV>'+
 '</Administration1>'
 else ''
 end + 
  case when ISNULL(unite_2,'') + ISNULL(date_2,'') <> '' then
 '<Administration2>'+
 '<Unite>'+isnull(convert(varchar,Unite_2),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_2,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_2,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_2,'')+'</Initiale>'+
 '<Site>'+isnull(Site_2,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_2),'')+'</IDRV>'+
 '</Administration2>'   
 else ''
 end +
  case when ISNULL(unite_3,'') + ISNULL(date_3,'') <> '' then
 '<Administration3>'+
 '<Unite>'+isnull(convert(varchar,Unite_3),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_3,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_3,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_3,'')+'</Initiale>'+
 '<Site>'+isnull(Site_3,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_3),'')+'</IDRV>'+
 '</Administration3>'
 else ''
 end +
 case when ISNULL(unite_4,'') + ISNULL(date_4,'') <> '' then
 '<Administration4>'+
 '<Unite>'+isnull(convert(varchar,Unite_4),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_4,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_4,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_4,'')+'</Initiale>'+
 '<Site>'+isnull(Site_4,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_4),'')+'</IDRV>'+
 '</Administration4>'
 else ''
 end +
 case when ISNULL(unite_5,'') + ISNULL(date_5,'') <> '' then
 '<Administration5>'+
 '<Unite>'+isnull(convert(varchar,Unite_5),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_5,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_5,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_5,'')+'</Initiale>'+
 '<Site>'+isnull(Site_5,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_5),'')+'</IDRV>'+
 '</Administration5>'
 else ''    
 end +
 case when ISNULL(unite_6,'') + ISNULL(date_6,'') <> '' then
 '<Administration6>'+
 '<Unite>'+isnull(convert(varchar,Unite_6),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_6,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_6,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_6,'')+'</Initiale>'+
 '<Site>'+isnull(Site_6,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_6),'')+'</IDRV>'+
 '</Administration6>'
 else ''    
 end +
 case when ISNULL(unite_7,'') + ISNULL(date_7,'') <> '' then
 '<Administration7>'+
 '<Unite>'+isnull(convert(varchar,Unite_7),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_7,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_7,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_7,'')+'</Initiale>'+
 '<Site>'+isnull(Site_7,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_7),'')+'</IDRV>'+
 '</Administration7>'
 else ''    
 end +
 case when ISNULL(unite_8,'') + ISNULL(date_8,'') <> '' then
 '<Administration8>'+
 '<Unite>'+isnull(convert(varchar,Unite_8),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_8,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_8,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_8,'')+'</Initiale>'+
 '<Site>'+isnull(Site_8,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_8),'')+'</IDRV>'+
 '</Administration8>'
 else ''    
 end +
 case when ISNULL(unite_9,'') + ISNULL(date_9,'') <> '' then
 '<Administration9>'+
 '<Unite>'+isnull(convert(varchar,Unite_9),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_9,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_9,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_9,'')+'</Initiale>'+
 '<Site>'+isnull(Site_9,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_9),'')+'</IDRV>'+
 '</Administration9>'
 else ''    
 end +
 case when ISNULL(unite_10,'') + ISNULL(date_10,'') <> '' then
 '<Administration10>'+
 '<Unite>'+isnull(convert(varchar,Unite_10),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_10,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_10,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_10,'')+'</Initiale>'+
 '<Site>'+isnull(Site_10,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_10),'')+'</IDRV>'+
 '</Administration10>'
 else ''
 end +
 case when ISNULL(unite_11,'') + ISNULL(date_11,'') <> '' then
 '<Administration11>'+
 '<Unite>'+isnull(convert(varchar,Unite_11),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_11,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_11,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_11,'')+'</Initiale>'+
 '<Site>'+isnull(Site_11,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_11),'')+'</IDRV>'+
 '</Administration11>'
 else ''
 end + 
  case when ISNULL(unite_12,'') + ISNULL(date_12,'') <> '' then
 '<Administration12>'+
 '<Unite>'+isnull(convert(varchar,Unite_12),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_12,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_12,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_12,'')+'</Initiale>'+
 '<Site>'+isnull(Site_12,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_12),'')+'</IDRV>'+
 '</Administration12>'
 else ''
 end +
  case when ISNULL(unite_13,'') + ISNULL(date_13,'') <> '' then
 '<Administration13>'+
 '<Unite>'+isnull(convert(varchar,Unite_13),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_13,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_13,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_13,'')+'</Initiale>'+
 '<Site>'+isnull(Site_13,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_13),'')+'</IDRV>'+
 '</Administration13>'
 else ''
 end +
 case when ISNULL(unite_14,'') + ISNULL(date_14,'') <> '' then
 '<Administration14>'+
 '<Unite>'+isnull(convert(varchar,Unite_14),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_14,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_14,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_14,'')+'</Initiale>'+
 '<Site>'+isnull(Site_14,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_14),'')+'</IDRV>'+
 '</Administration14>'
 else ''
 end +
 case when ISNULL(unite_15,'') + ISNULL(date_15,'') <> '' then
 '<Administration15>'+
 '<Unite>'+isnull(convert(varchar,Unite_15),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_15,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_15,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_15,'')+'</Initiale>'+
 '<Site>'+isnull(Site_15,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_15),'')+'</IDRV>'+
 '</Administration15>'
 else ''    
 end +
 case when ISNULL(unite_16,'') + ISNULL(date_16,'') <> '' then
 '<Administration16>'+
 '<Unite>'+isnull(convert(varchar,Unite_16),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_16,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_16,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_16,'')+'</Initiale>'+
 '<Site>'+isnull(Site_16,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_16),'')+'</IDRV>'+
 '</Administration16>'
 else ''    
 end +
 case when ISNULL(unite_17,'') + ISNULL(date_17,'') <> '' then
 '<Administration17>'+
 '<Unite>'+isnull(convert(varchar,Unite_17),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_17,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_17,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_17,'')+'</Initiale>'+
 '<Site>'+isnull(Site_17,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_17),'')+'</IDRV>'+
 '</Administration17>'
 else ''    
 end +
 case when ISNULL(unite_18,'') + ISNULL(date_18,'') <> '' then
 '<Administration18>'+
 '<Unite>'+isnull(convert(varchar,Unite_18),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_18,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_18,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_18,'')+'</Initiale>'+
 '<Site>'+isnull(Site_18,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_18),'')+'</IDRV>'+
 '</Administration18>'
 else ''    
 end +
 case when ISNULL(unite_19,'') + ISNULL(date_19,'') <> '' then
 '<Administration19>'+
 '<Unite>'+isnull(convert(varchar,Unite_19),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_19,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_19,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_19,'')+'</Initiale>'+
 '<Site>'+isnull(Site_19,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_19),'')+'</IDRV>'+
 '</Administration19>'
 else ''    
 end +
 case when ISNULL(unite_20,'') + ISNULL(date_20,'') <> '' then
 '<Administration20>'+
 '<Unite>'+isnull(convert(varchar,Unite_20),'')+'</Unite>'+
 '<Date>'+isnull(convert(varchar,Date_20,120),'')+'</Date>'+
 '<Texte>'+isnull(Texte_20,'')+'</Texte>'+
 '<Initiale>'+isnull(Initiale_20,'')+'</Initiale>'+
 '<Site>'+isnull(Site_20,'')+'</Site>'+
 '<Cancel></Cancel>'+
 '<IdNote></IdNote>'+
 '<IDRV>'+isnull(convert(varchar,IDRV_20),'')+'</IDRV>'+
 '</Administration20>'
 else ''
 end + '</IDObjet>'
 from  tmp_CarnetVac

这是错误:

  

Msg 9455,Niveau 16,État1,Ligne 3 XML解析:第1行,角色   80,非法合格姓名字符

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您应该使用FOR XML PATH创建XML!通过将字符串绑定在一起这样做是一个非常糟糕的习惯,你真的应该避免!

向你解释发生了什么(可能):

我使用表变量来模拟带有值的表并插入两个“行”:

DECLARE @tbl TABLE(SomeText VARCHAR(100));
INSERT INTO @tbl VALUES
 ('This is some text.')
,('Even more ...');

这与你正在做的大致相同:在string-base上组合元素并将其转换为XML。这实际上有效......

SELECT CAST('<Element>' + SomeText + '</Element>' AS XML)
FROM @tbl;

但现在我添加了一行,其中包含一个非法字符

INSERT INTO @tbl VALUES('Forbidden & character!'); --The & (ampersand) is forbidden (as well as <, > and some more)

突然,同样的声明引发了“你的”错误。只是因为正常文本中的&符号是被禁止的,必须进行转义。

SELECT CAST('<Element>' + SomeText + '</Element>' AS XML)
FROM @tbl;

从你的问题我们无法知道,到底出了什么问题......可能是&amp ;. &lt;,或&gt;,可能是错误位置的“...”可能是您需要unicode编码的特殊字符。可能足以用前导N(N'string-literal')开始所有字符串文字,并在任何地方使用NVARCHAR代替VARCHAR ...... 但这一切都无法解决实际问题:不要通过字符串连接创建XML!

这是你应该这样做的方式:

SELECT SomeText
FROM @tbl
FOR XML PATH('row'),ROOT('root'),TYPE

正如您所看到的,隐式字符被隐式正确转义:

<root>
  <row>
    <SomeText>This is some text.</SomeText>
  </row>
  <row>
    <SomeText>Even more ...</SomeText>
  </row>
  <row>
    <SomeText>Forbidden &amp; character!</SomeText>
  </row>
</root>

更新

我必须承认,我很确定,这并不能真正解决你的问题...而且我必须承认,你的整个问题都有点惹人注意......但是你没有足够的表现来帮助你...

我建议您开始一个新问题,其中显示一些(减少的)样本数据和预期输出。这肯定可以做得更好......