作為對xquery語言的擴展,xml dml為xml數據操作提供了更大的靈活性,而不再僅僅是對xml數據進行一些查詢操作。通過xml dml,用戶可以像操作關系表一樣對xml中的節點內容進行插入、更新和刪除操作。xml dml需要通過xml數據類型的modify方法進行調用。
1.insert
insert用于將expression1標識的一個或多個節點作為expression2標識的節點的子節點或同級節點插入。語法格式如下:
insert
   expression1 (
         {as first | as last} into | after | before
                  expression2
        )expression1和expression2
標識要插入的一個或多個節點。它可以是常量xml實例,也可以是xquery表達式。該表達式可以得出節點、文本節點或一組有序的節點,但它無法解得根節點。如果該表達式得出一個值或一組值,則這些值作為單個文本節點插入,各值之間以空格分隔開。如果將多個節點指定為常量,則這些節點用括號括住,并以逗號分隔開。但無法插入異構序列(如一組元素、屬性或值)。如果expression1解得一個空序列,則不會發生插入操作,并且不會返回任何錯誤。
into
expression1標識的節點作為expression2標識的節點的子節點插入。如果expression2中的節點已有一個或多個子節點,則必須使用as first或as last來指定所需的新節點添加位置。
after
expression1標識的節點作為expression2標識的節點的同級節點直接插入在其后面,after關鍵字不能用于插入屬性。
before
expression1標識的節點作為expression2標識的節點的同級節點直接插入在其前面,before關鍵字不能用于插入屬性。
|||(1)插入元素文檔中
在下面的示例中,首先將xml文檔分配給xml類型的變量。然后使用幾個insert xml dml語句說明如何將元素節點插入文檔中。注意在示例中為各種路徑表達式都指定了“[1]”,以要求每次只返回單個目標,這樣就確保了只有單個目標節點。每次插入后,select語句都會顯示結果。最終執行結果如圖1所示。
declare @mydoc xml   
set @mydoc = '<root>   
  <item id="1">   
  </item>   
</root>'   
select @mydoc   
-- 插入item的第1個子節點,此時不需要指定as first或as last
set @mydoc.modify('   
insert <author>張洪舉</author>
into (/root/item)[1]')
select @mydoc   
-- 插入item的第2個子節點,as first指定插入到同級子節點的前面   
set @mydoc.modify('   
insert <title>sql server 2005開發寶典</title>
as first into (/root/item)[1]')   
select @mydoc   
-- 插入第2個item節點
set @mydoc.modify('   
insert <item id="2"></item>
into (/root)[1]')   
select @mydoc 
-- 向第2個item中插入第1個子節點 
set @mydoc.modify('   
insert <title>sql server 2005的新增功能</title>
as first into (/root/item)[2]')   
select @mydoc
go
圖1 向xml中插入節點
|||商業源碼熱門下載www.html.org.cn
(2)插入多個元素到文檔中
在下面的示例中,將title和author元素插入到了item節點中。元素之間使用逗號分隔,并包含在括號中。
declare @mydoc xml
set @mydoc = '<root>   
  <item id="1">   
  </item>   
</root>' 
select @mydoc
set @mydoc.modify('   
insert (
   <title>sql server 2005開發寶典</title>,
   <author>張洪舉</author>
    )
into (/root/item)[1]');
select @mydoc 
go(3)插入屬性到文檔中
在下面的示例中,向xml文檔中插入了多個屬性。每次插入屬性后,select語句都會顯示結果,最終執行結果如圖2所示。
declare @mydoc xml   
set @mydoc = '<root>   
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>   
</root>' 
select @mydoc
set @mydoc.modify('   
insert attribute shipto {"廣州"}
into (/root/item[@id=1])[1]');
select @mydoc   
-- 通過一個sql變量指定要插入屬性shipdate的值     
declare @shipdate char(11)     
set @shipdate='2006-01-23z'     
set @mydoc.modify('     
insert attribute shipdate {sql:variable("@shipdate") cast as xs:date ?}     
into (/root/item[@id=1])[1]') ;     
select @mydoc     
-- 插入多個屬性,屬性之間使用逗號分隔,并包含在括號內     
set @mydoc.modify('     
insert (      
    attribute postcode {"253020" },     
    attribute weight {"1.5"}     
    )     
into (/root/item[@id=1])[1]');
select @mydoc
go |||
圖2插入屬性到xml中
(4)插入注釋節點
在下面的示例中,將注釋節點插入到id為2的item節點中title元素的后面。
declare @mydoc xml   
set @mydoc = '<root>   
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
  <item id="2">
    <title>asp.net實戰</title>
    <author>盧桂章</author>
  </item>
</root>' 
set @mydoc.modify('     
insert <!-- 注釋 -->     
after (/root/item[@id=2]/title)[1]');
select @mydoc
go插入注釋后xml的內容如下:
<root>
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
  <item id="2">
    <title>asp.net實戰</title>
    <!-- 注釋 -->
    <author>盧桂章</author>
  </item>
</root>(5)使用cdata部分插入數據
當插入的文本中包含有xml無效字符(如“<”或“>”)時,可以使用cdata部分插入數據。參考下面的示例:
declare @mydoc xml   
set @mydoc = '<root>   
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
  <item id="2">
    <title>asp.net實戰</title>
    <author>盧桂章</author>
  </item>
</root>' 
set @mydoc.modify('     
insert <desc><![cdata[ <送貨方式>上門<價款>未收]]></desc>
into (/root/item[@id=2])[1] ') ;
select @mydoc
go |||被插入部分中的xml無效字符,會被轉換成實體,如“<”保存為<。下面的插入cdata部分后xml文檔的內容:
<root>
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
  <item id="2">
    <title>asp.net實戰</title>
    <author>盧桂章</author>
    <desc> <送貨方式>上門<價款>未收</desc>
  </item>
</root>(6)插入文本節點
要將文件插入到xml中,需要使用text函數構造文本,參考下面的示例:
declare @mydoc xml   
set @mydoc = '<root>   
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
</root>' 
set @mydoc.modify('
insert text{"訂單列表"}
as first into (/root)[1]');
select @mydoc
go得到的xml結果如下:
<root>訂單列表<item id="1"><title>ajax實戰</title><author>張洪舉</author></item></root>(7)將節點插入類型化的xml列中
在下面的示例中,首先創建了一個架構集合,并建立了一個使用該架構集合的表。在使用transact-sql insert語句向表中插入一個符合架構約束的xml后,再使用xml dml insert向該xml中插入一個item節點。
|||-- 創建xml架構集合
create xml schema collection myschemas
as
n'<?xml version = "1.0"?>
<xsd:schema targetnamespace="http://schemas.mybook.com/customerschemas"
  xmlns:xsd="http://www.w3.org/2001/xmlschema">
  <xsd:element name="customer">
    <xsd:complextype>
      <xsd:sequence>
        <xsd:element maxoccurs="unbounded" name="item">
          <xsd:complextype>
            <xsd:sequence>
              <xsd:element name="customername" type="xsd:string"/>
              <xsd:element name="address" type="xsd:string"/>
              <xsd:element name="phone" type="xsd:string"/>
              <xsd:element name="contact" type="xsd:string"/>
            </xsd:sequence>
            <xsd:attribute name="id" type="xsd:int"/>
          </xsd:complextype>
        </xsd:element>
      </xsd:sequence>
    </xsd:complextype>
  </xsd:element>
</xsd:schema>';
go
-- 創建包含xml數據類型列的表
create table mycustomer
  (customerid int identity primary key,
   customeritem xml(myschemas));
go
-- 向表中插入xml,該xml應當符合http://schemas.mybook.com/customerschemas命名空間架構的定義
insert into mycustomer
values
(n'<c:customer xmlns:c="http://schemas.mybook.com/customerschemas">
  <item id="1">
    <customername>北方書城</customername>
    <address>北京市海淀區知春路22號</address>
    <phone>2222222</phone>
    <contact>劉先生</contact>
  </item>
</c:customer>');
-- 使用xml dml insert插入另一個item節點到xml中
update mycustomer
set customeritem.modify('
declare namespace cs="http://schemas.mybook.com/customerschemas";
insert (<item id="2">
    <customername>東圖大廈</customername>
    <address>長春市朝陽大街99號</address>
    <phone>1111111</phone>
    <contact>孫小姐</contact>
  </item>)
into (/cs:customer)[1] ')
where customerid=1;
select customeritem
from mycustomer;
go |||執行上面的select查詢后,可以看到customeritem中的xml內容,如下所示:
<c:customer xmlns:c="http://schemas.mybook.com/customerschemas">
  <item id="1">
    <customername>北方書城</customername>
    <address>北京市海淀區知春路22號</address>
    <phone>2222222</phone>
    <contact>劉先生</contact>
  </item>
  <item id="2">
    <customername>東圖大廈</customername>
    <address>長春市朝陽大街99號</address>
    <phone>1111111</phone>
    <contact>孫小姐</contact>
  </item>
</c:customer>2.delete
delete用于刪除xml實例的節點。其語法格式如下:
delete expressionexpression是要刪除的節點的xquery表達式。刪除該表達式選擇的所有節點,以及所選節點中的所有節點或值。表達式不能是根(/)節點。如果表達式返回空序列,則不進行刪除,不返回錯誤。
下面的示例演示了從非類型化的xml變量中刪除指令、注釋、屬性、元素和節點的方法。在每次刪除后都會顯示xml,結果如圖3所示。
declare @mydoc xml
set @mydoc = '<?instructions for=thewc.exe ?>
<root>
  <!-- 這里是注釋 -->
  <item id="1" shipto="廣州">這里是文本
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
  <item id="2">
    <title>asp.net實戰</title>
    <author>盧桂章</author>
  </item>
</root>'
select @mydoc
-- 刪除注釋
set @mydoc.modify('
delete /root/comment()
')
select @mydoc
-- 刪除所有指令
set @mydoc.modify('
delete //processing-instruction()
')
select @mydoc
-- 刪除id為1的item中的文本節點
set @mydoc.modify('
delete /root/item[@id=1]/text()
')
select @mydoc
-- 刪除一個屬性
set @mydoc.modify('
delete /root/item[@id=1]/@shipto
')
select @mydoc
-- 刪除一個元素
set @mydoc.modify('
delete /root/item[@id=2]/author
')
select @mydoc
-- 刪除id為2的item節點
set @mydoc.modify('
delete /root/item[@id=2]
')
select @mydoc
go |||
圖3從非類型化xml變量中刪除注釋、指令、屬性、元素和節點
下面的語句演示從類型化xml中刪除節點的方法,其中的mycustomer是前面在“將節點插入類型化的xml列中”部分中創建的。
update mycustomer
set customeritem.modify('
declare namespace cs="http://schemas.mybook.com/customerschemas";
delete /cs:customer/item[@id=2]
');
select customeritem from mycustomer;
go3.replace
replace用于更新文檔中的值。其語法格式如下:
replace value of
   expression1
with
   expression2expression1
標識其值要更新的節點。它必須僅標識一個單個節點。如果xml已類型化,則節點的類型必須是具有簡單類型內容(列表或原子類型)的單個元素、文本節點或屬性節點,不能是聯合類型、復雜類型、處理指令、文檔節點或注釋節點。否則,將返回錯誤。
expression2
標識節點的新值。在修改類型化的xml實例中,expression2與expression1必須是相同類型。
下面的示例演示了更新xml中元素的文本和屬性值的方法。每次更改時,都會顯示xml,如圖4所示。
declare @mydoc xml
set @mydoc = '<root>
  <item id="1">
    <title>ajax實戰</title>
    <author>張洪舉</author>
  </item>
  <item id="2">
    <title>asp.net實戰</title>
    <author>盧桂章</author>
  </item>
</root>'
select @mydoc
-- 更新id為1的item中的title元素的文本
set @mydoc.modify('
replace value of (/root/item[@id=1]/title/text())[1]
with "ajax實戰攻略"
')
select @mydoc
-- 更新屬性值
set @mydoc.modify('
replace value of (/root/item[@id=2]/@id)[1]
with "3"
')
select @mydoc
圖4 更改xml中元素的文本和屬性值
新聞熱點
疑難解答