Удалить несколько одноуровневых узлов XmlData с помощью .remove()

Этот вопрос аналогичен Удаление нескольких узлов в одном XQuery для SQL-сервер. Разница в том, что я хочу без разбора удалить все узлы в документе.

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 узел я хочу удалить. Могу ли я удалить все узлы без необходимости делать это по одному?


person Matt R    schedule 16.01.2014    source источник
comment
DocumentXml — это столбец xml, отличный от null: DocumentXml (XML(.), not null) — это то, что показывает SSMS. Но ошибка все равно присутствует.   -  person Matt R    schedule 17.01.2014


Ответы (1)


Попробуйте этот запрос:

UPDATE tblXmlDocumentData
SET DocumentXml.modify('delete (//Attachment)') 
Where DocumentId = 1
person Bogdan Sahlean    schedule 17.01.2014
comment
Я понял свою ошибку некоторое время назад. SET DocumentXml = Document... было причиной ошибки. - person Matt R; 21.01.2014