简体   繁体   English

没有命名空间SQL2008的XQuery插入

[英]XQuery Insert without namespace SQL2008

<game xmlns="http://my.name.space" ></game>  

This is my root element. 这是我的根本要素。 I've written a stored procedure to insert elements into it. 我编写了一个存储过程来插入元素。 To sum up the stored procedure, here's the SQL 总结存储过程,这是SQL

UPDATE ChessGame SET GameHistory.modify('insert <move><player>black</player><piece>pawn</piece><start>E7</start><end>E6</end></move> as last into (/game)[0]') WHERE Id = @GameId;

Now when MSSQL does the insert an empty namespace is also inserted so the result is this 现在,当MSSQL插入时,还会插入一个空名称空间,因此结果就是这样

<move xmlns="">
  <player>black</player>
  <piece>king</piece>
  <start>E7</start>
  <end>E6</end>
</move>

Now I've tried using both 现在我尝试过使用它们

WITH XMLNAMESPACES(DEFAULT 'http://my.name.space')

and

GameHistory.modify('declare default element namespace "http://my.name.space"; insert ...')

But I end up with prefixes everywhere and a namespace declaration for each element. 但我到处都是前缀,每个元素都有一个命名空间声明。

Problems arise in code libraries that lack logic to handle the prefixes MSSQL puts in. In the end I just want to insert a new element into my xml root and to leave the namespace blank (use the root default?). 代码库中出现问题,这些代码库缺少处理MSSQL输入的前缀的逻辑。最后我只想在我的xml根中插入一个新元素并将命名空白留空(使用root默认值?)。 I'm all very new to this but as I understand it, if I have a namespace in my root element, shouldn't all childnodes have a default namespace of my root? 我对这一切都很陌生,但据我所知,如果我的根元素中有一个命名空间,那么所有的子节点都不应该有我的root的默认命名空间吗?

OK, this works for me: 好的,这对我有用:

DECLARE @x XML;
SET @x = '<game xmlns="http://my.name.space" ></game>';

select @x

SET @x.modify(
    ' declare default element namespace "http://my.name.space";
    insert <move><player>black</player><piece>pawn</piece><start>E7</start><end>E6</end></move> as last into (/*:game)[1]'
    )

select @x
declare @x xml;
select @x='<game xmlns="http://my.name.space" ></game>';
set @x.modify('declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece>
     <start>E7</start><end>E6</end></move> as last into (/game)[1]');
select @x;

This produces: 这会产生:

<game xmlns="http://my.name.space">
  <move>
    <player>black</player>
    <piece>pawn</piece>
    <start>E7</start>
    <end>E6</end>
  </move>
</game>

on both SQL 2005 SP2 and SQL 2008 SP1. 在SQL 2005 SP2和SQL 2008 SP1上。

Also this table update works fine: 此表更新也正常:

declare @t table (x xml);
insert into @t (x) values ('<game xmlns="http://my.name.space" ></game>');
update @t
set x.modify('declare default element namespace "http://my.name.space"; 
    insert <move><player>black</player><piece>pawn</piece>
       <start>E7</start><end>E6</end></move> as last into (/game)[1]');
select * from @t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM