使用.remove()删除多个XmlData兄弟节点

时间:2014-01-16 20:51:20

标签: xml sql-server-2008 xml-dml

这个问题与Deleting Multiple Nodes in Single XQuery for SQL Server一致。不同之处在于我想不加选择地删除文档中的所有节点。

XML:

<root>      
    <Attachment id="Holding_1_attachment_0">
      <AttachmentData>(B64 Enconded string)</AttachmentData>
      <MimeTypeTC tc="17" />
      <AttachmentLocation tc="2">URL Reference</AttachmentLocation>
    </Attachment>
    <Attachment id="Holding_1_attachment_0">
      <AttachmentData>(B64 Enconded string)</AttachmentData>
      <MimeTypeTC tc="17" />
      <AttachmentLocation tc="2">URL Reference</AttachmentLocation>
    </Attachment>
    <Attachment id="234">
      <AttachmentBasicType tc="3">File</AttachmentBasicType>
      <AttachmentSource>C:\Windows Ding.wav</AttachmentSource>
      <AttachmentData>(B64 Enconded string)</AttachmentData>
      <MimeTypeTC tc="7">WAV</MimeTypeTC>
      <TransferEncodingTypeTC tc="4">Base64</TransferEncodingTypeTC>
      <AttachmentLocation tc="1">Inline</AttachmentLocation>
      <FileName>Windows Ding.wav</FileName>
    </Attachment>
    <Attachment id="234">
      <AttachmentBasicType tc="3">File</AttachmentBasicType>
      <AttachmentSource>C:\Windows Ding.wav</AttachmentSource>
      <AttachmentData>(B64 Enconded string)</AttachmentData>
      <MimeTypeTC tc="7">WAV</MimeTypeTC>
      <TransferEncodingTypeTC tc="4">Base64</TransferEncodingTypeTC>
      <AttachmentLocation tc="1">Inline</AttachmentLocation>
      <FileName>Windows Ding2.wav</FileName>
    </Attachment>
</root>

基本上我有一个包含上述XML的大文档,我想要删除所有Attachment节点(包括子节点),或删除AttachmentData节点(我还没有决定我想使用哪种方法。

我尝试了以下操作来删除节点:

UPDATE tblXmlDocumentData
SET DocumentXml = DocumentXml.modify('delete (//Attachment)') /* or //Attachment/AttachmentData */
Where DocumentId = 1

哪个SQL回复: Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.

我假设这是因为我没有指定要删除的Attachment节点。我可以删除所有节点而不必一次删除一个节点吗?

1 个答案:

答案 0 :(得分:1)

尝试此查询:

UPDATE tblXmlDocumentData
SET DocumentXml.modify('delete (//Attachment)') 
Where DocumentId = 1