为什么CDATA指令不适用于xml显式查询?

Why is the CDATA directive not working this for xml explicit query?

提问人:medic 提问时间:3/9/2022 最后编辑:medic 更新时间:3/10/2022 访问量:88

问:

我希望 cdata 标签包装 col2 元素的文本值,但它没有这样做。我使用的是 xml raw,但由于我在数据中遇到了一些无效的 xml 字符,我现在必须用 cdata 标签包装所有内容,所以我想做到这一点的唯一方法是使用显式模式。如果没有插入到表中,就会显示 cdata 标签,但插入到表中会删除它们,我怀疑它与自动 xml 数据类型转换有关。此外,排序依据不是按数值数据类型排序,而是在进行字符串排序


-- drop table Table1
-- truncate table Table1
create table dbo.Table1
(
  RowId int not null identity,
  BatchId int not null,    
  RequirementId int not null,   
  DataSheetName nvarchar(max) null,
  TestCaseName  nvarchar(max) null,
  GridStart nvarchar(max) null,
  GridId int not null,
  Col1  nvarchar(max) null,
  Col2  nvarchar(max) null
) 

create table results (
    GridName nvarchar(max),
    GridXML xml,
)


insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','Grid', 1,'','Deal Number')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','1', 1,'r1','r1c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','2', 1,'r2','r2c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','3', 1,'r3','r3c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','4', 1,'r4','r4c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','5', 1,'r5','r5c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','6', 1,'r6','r6c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','7', 1,'r7','r7c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','8', 1,'r8','r8c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','9', 1,'r9','r9c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','10', 1,'r10','r10c1')
insert into Table1(BatchId, RequirementId, DataSheetName,TestCaseName,GridName,GridStart,GridId, Col1,Col2)
values(1,1,'DataSheet','TestCase1','Grid1','END', 1,'','')


   insert into results
   select Z.GridName,  
      (select * from 
         ( select 1 [Tag], null [Parent], null [DocumentElement!1!], 
            null [tr!2!], null [tr!2!RowId!Element], null [tr!2!Col2!CDATA]  

            union  all  

            select 2 as Tag, 1 as Parent, 'DocumentElement',GridStart,GridStart, Col2  
            from Table1 S  
            where GridStart != 'GRID' and GridStart != 'END'  
            and Z.DataSheetName = S.DataSheetName  
            and Z.TestCaseName = S.TestCaseName  
            and Z.GridName = S.GridName  
            and Z.GridId = S.GridId  
            and Z.BatchId = S.BatchId 
         ) 
         as GridData 
         order by [tr!2!RowId!Element] 
         for xml explicit 
      ) as G 
    from dbo.Table1 Z 

select  *  from results

Generated output:

<DocumentElement>
  <tr>1<RowId>1</RowId><Col2>r1c1</Col2></tr>
  <tr>10<RowId>10</RowId><Col2>r10c1</Col2></tr>
  <tr>2<RowId>2</RowId><Col2>r2c1</Col2></tr>
  <tr>3<RowId>3</RowId><Col2>r3c1</Col2></tr>
  <tr>4<RowId>4</RowId><Col2>r4c1</Col2></tr>
  <tr>5<RowId>5</RowId><Col2>r5c1</Col2></tr>
  <tr>6<RowId>6</RowId><Col2>r6c1</Col2></tr>
  <tr>7<RowId>7</RowId><Col2>r7c1</Col2></tr>
  <tr>8<RowId>8</RowId><Col2>r8c1</Col2></tr>
  <tr>9<RowId>9</RowId><Col2>r9c1</Col2></tr>
</DocumentElement>


<DocumentElement>
  <tr>1<RowId>1</RowId><Col2><![CDATA[r1c1]]</Col2></tr>
  <tr>2<RowId>2</RowId>Col2><![CDATA[r2c1]]</Col2></tr>
  <tr>3<RowId>3</RowId>Col2><![CDATA[r3c1]]</Col2></tr>
  <tr>4<RowId>4</RowId>Col2><![CDATA[r4c1]]</Col2></tr>
  <tr>5<RowId>5</RowId>Col2><![CDATA[r5c1]]</Col2></tr>
  <tr>6<RowId>6</RowId>Col2><![CDATA[r6c1]]</Col2></tr>
  <tr>7<RowId>7</RowId>Col2><![CDATA[r7c1]]</Col2></tr>
  <tr>8<RowId>8</RowId>Col2><![CDATA[r8c1]]</Col2></tr>
  <tr>9<RowId>9</RowId>Col2><![CDATA[r9c1]]</Col2></tr>
  <tr>10<RowId>10</RowId>Col2><![CDATA[r10c1]]</Col2></tr>
</DocumentElement>
xml 显式 的 sql-server

评论

0赞 Yitzhak Khabinsky 3/9/2022
在提出问题时,您需要提供一个最小的可重现示例:(1) DDL 和示例数据填充,即 CREATE 表和 INSERT T-SQL 语句。(2) 需要执行的操作,即逻辑和代码尝试在 T-SQL 中实现它。(3) 期望的输出,基于上面 #1 中的示例数据。(4) 您的 SQL Server 版本 (SELECT @@version;)。
0赞 medic 3/9/2022
@YitzhakKhabinsky,OK就可以了。 我应该删除问题并重新发布示例数据吗?
0赞 Thom A 3/9/2022
不,使用编辑功能,@medic .
0赞 Mike Miller 3/9/2022
我想你需要!!不!?
0赞 Charlieface 3/9/2022
我很想知道为什么无论如何都需要这样做,似乎不涉及嵌套EXPLICIT

答: 暂无答案