XML解析:第21行,字符67,非法的合格名称字符转换错误?

时间:2018-11-08 14:27:06

标签: sql sql-server xml

我正在使用SQL Server 2014,当我在查询下面运行时,出现以下错误消息:

  

XML解析:第21行,字符67,非法的合格名称字符

这可能是转换错误吗?我的问题是我不知道问题出在哪里,我已经阅读了关于类似问题的几个问题,但对于为什么会发生这种情况仍然感到困惑,任何人都可以请我帮助您理解错误消息。

上下文:我有一个名为tasks的表,该表存储p_num(产品编号),而descript该表存储日期注释和更新产品订单的人员。我想检索日期和人名。但是,问题在于每个p_num具有多个注释,并且所有注释都存储在每个p_num的单个单元格中,并且它们之间仅用空格分隔。这种方法似乎可以帮助我获得结果,但是却收到了此错误消息。

  Sample of text field(Descript):
   11/6/2018 2:04pm Standard pacific time - m.doe I need help with this issue & I might need help with this stupid issue 11/5/2018 1:05pm standard pacific time - m.doe I looked at the issue and I found it difficult to understand 11/4/2018 8:00am standard pacific time - j.moe issue has been registered
   end of all the note.

输出:

P_num    Date        Name
100      11/6/2018   m.doe
100      11/5/2018   m.doe
100      11/4/2018   j.moe

代码:

 DECLARE @tmp13 TABLE
                (
                   [p_Num] INTEGER NOT NULL,
                   [DESCRIPT] NVARCHAR(MAX)
                   PRIMARY KEY([p_Num])
                );

DECLARE @tmp13Parse TABLE
                    (
                        [Id] INT,
                        [Position] BIGINT,
                        [Value] NVARCHAR(500)
                        unique clustered ([Id], [Position])
                    );


 --insert test data
 INSERT INTO @tmp13 
  SELECT p_NUM,DESCRIPT FROM TASKS WHERE p_NUM >250000;



INSERT INTO @tmp13Parse (
                        [Id]
                      , [Position]
                      , [Value]
                    )
        SELECT      [a].[p_Num]
                  , [ss].[Position]
                  , [ss].[Value]
        FROM        @tmp13 [a]
        CROSS APPLY (
                        SELECT      ROW_NUMBER() OVER ( ORDER BY (SELECT 1)) 
 AS [Position]
                                  , [y].[i].[value]('(./text())[1]', 'nvarchar(max)') AS [Value]
                        FROM        (
                                        SELECT [x] = CONVERT(XML, '<i>'+ REPLACE([a].[DESCRIPT], ' ', '</i><i>')+ '</i>').[query]('.')
                                    ) AS [a]
                        CROSS APPLY [x].[nodes]('i') AS [y]([i])
                    ) AS [ss];



 SELECT      [a].[Id]
      , [a].[Value] AS [Date]
      , [ccc].[Value] AS [Name]
 FROM        @tmp13Parse [a]

 CROSS APPLY (
            SELECT MIN([aa].[Position]) + 1 AS [nameAnchorPosition]
            FROM   @tmp13Parse [aa]
            WHERE  [aa].[Id] = [a].[Id]
                   AND [aa].[Value] = '-'
                   AND [aa].[Position] > [a].[Position]
        ) AS [bb]

  CROSS APPLY (
            SELECT [cc].[Value]
            FROM   @tmp13Parse [cc]
            WHERE  [cc].[Id] = [a].[Id]
                   AND [cc].[Position] = [bb].[nameAnchorPosition]
                 )  AS [ccc]
   WHERE       TRY_CONVERT(DATE, [a].[Value]) > '1900-01-01'; 

1 个答案:

答案 0 :(得分:0)

更改此内容可能就足够

SELECT [x] = CONVERT(XML, '<i>'+ REPLACE([a].[DESCRIPT], ' ', '</i><i>')+ '</i>')

对此

SELECT [x] = CONVERT(XML, '<i>'+ REPLACE((SELECT [a].[DESCRIPT] AS [*] FOR XML PATH('')), ' ', '</i><i>')+ '</i>')

一些解释:

如果将带有标记的"this & that"之类的字符串连接起来,则会得到类似的信息

<SomeTag>this & that</SomeTag>

但是这将失败,因为必须使用&amp;来对&符号进行转义。幸运的是,隐式机制将隐式地进行所有转义。只需尝试:

SELECT (SELECT 'See how &, < and > are escaped?' AS [*] FOR XML PATH(''))

使用此技巧,您可以将带有禁止字符的字符串放入XML:

SELECT CAST('<x>' + (SELECT 'See how &, < and > are escaped?' AS [*] FOR XML PATH('')) + '</x>' AS XML);

结果

<x>See how &amp;, &lt; and &gt; are escaped?</x>