SQL Server 2005 XML 操作總結(五)元素(節點)操作——修改、刪除、移

========修改操作======

--將category="WEB"的第一個book節點的year值改爲2000
set @data.modify('replace value of
(/bookstore/book[@category="WEB"]/year/text())[1] with "2000"')
/*output:
<book category="WEB">
<title lang="cn">Learning XML</title>
<author>Erik T. Ray</author>
<year>2000</year>
<price>39.95</price>
</book>
*/
--替換第一個book節點的author的內容爲“替換內容”
set @data.modify('replace value of(/bookstore/book[1]/author[1]/text())[1] with ("替換內容")')
/*output:
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>替換內容</author>
<year>2005</year>
<price>30.00</price>
</book>*/ ========刪除操作========

--刪除title的@lang="en"的所有book節點
set @data.modify('delete /bookstore/book[./title[@lang="en"]]')
/*output:
<bookstore>
  <book>
    <title lang="jp">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="cn">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>
*/
--刪除第一個book節點的author的內容
set @data.modify('delete /bookstore[1]/book[1]/author[1]/text()')
/*output:
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author />
    <year>2005</year>
    <price>30.00</price>
  </book>
*/
========移動操作=========

--title="Harry Potter"的book節點在同級中上移一層
set @data.modify('insert (/bookstore/book[title="Harry Potter"])
before (/bookstore/book[. <&lt; (/bookstore/book[title="Harry Potter"])[1]])
[last()]')
SET @data.modify ('delete /bookstore/book[title="Harry Potter"]
[. is (/bookstore/book[title="Harry Potter"])[last()]]')
/*output:
&lt;book category="CHILDREN">
......
</book>
<book category="COOKING">
......
</book>
<book category="WEB">
......
</book>
<book category="WEB">
......
</book>*/
--title="Harry Potter"的book節點在同級中下移一層
set @data.modify('insert (/bookstore/book[title="Harry Potter"])
before (/bookstore/book[. &gt;&gt; (/bookstore/book[title="Harry Potter"])[1]])
[last()]')
SET @data.modify ('delete /bookstore/book[title="Harry Potter"][1] ')
/*output:
<book category="COOKING">
......
</book>
<book category="WEB">
......
</book>
<book category="CHILDREN">
......
</book>
<book category="WEB">
......
</book>*/
--title="Harry Potter"的book節點移到category爲COOKING的book節點前
set @data.modify('insert (/bookstore/book[title="Harry Potter"])
before (/bookstore/book[@category="COOKING"])[1]')
SET @data.modify ('delete /bookstore/book[title="Harry Potter"] [2]')
/*output:
<book category="CHILDREN">
......
</book>
<book category="COOKING">
......
</book>
<book category="WEB">
......
</book>
<book category="WEB">
......
</book>*/
--title="Harry Potter"的book節點移到categroy爲WEB的第一個book節點後
set @data.modify('insert (/bookstore/book[title="Harry Potter"])
after (/bookstore/book[@category="WEB"])[1] ')
SET @data.modify ('delete /bookstore/book[title="Harry Potter"][1] ')
/*output:
<book category="COOKING">
......
</book>
<book category="WEB">
......
</book>
<book category="CHILDREN">
......
</book>
<book category="WEB">
......
</book>*/

========循環遍歷所有元素=========

--循環所有book節點
DECLARE
    @cnt INT,
    @totCnt INT,
    @child XML
-- counter variables
SELECT
    @cnt = 1,
    @totCnt = @data.value('count(/bookstore/book)','INT')
-- loop
WHILE @cnt <= @totCnt BEGIN
    SELECT
        @child = @data.query('/bookstore/book[position()=sql:variable("@cnt")]')
    PRINT 'Processing Child Element: ' + CAST(@cnt AS VARCHAR)
    PRINT 'Child element:  ' + CAST(@child AS VARCHAR(max))
    PRINT ''
    -- incremet the counter variable
    SELECT @cnt = @cnt + 1
END
/*output
Processing Child Element: 1
Child element:  &lt;book category="COOKING">......</book>

Processing Child Element: 2
Child element:  <book><title lang="jp">......</book>

Processing Child Element: 3
Child element:  <book category="WEB">......</book>

Processing Child Element: 4
Child element:  <book category="WEB">......</book>

複製代碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章