Невозможно вставить новый узел с помощью вставки (XML DML)

Я пытаюсь вставить узел в XML, используя вставку XML (XML DML).

XML выглядит следующим образом:

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <Worksheet xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ss:Name="1">
        <Table xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
            <Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">Audit ID</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">Audit Subcategory ID</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">1</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">ObjectID</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">ObjectTypeID</Data>
                </Cell>
            </Row>
            <Row xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">55406</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">3</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">1</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">6078</Data>
                </Cell>
                <Cell xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
                    <Data ss:Type="String">1</Data>
                </Cell>
            </Row>
        </Table>
    </Worksheet>
</Workbook>

Я пытаюсь вставить узел, используя приведенный ниже код:

SET @xml.modify('insert <Maintenance>111111111111111</Maintenance> into (/Workbook)[1]');

а затем я отображаю данные, используя

  Select @xml;

Проблема в том, что новый узел не отображается. Я пытаюсь изменить XML, используя

SET @xml.modify('insert <Maintenance>111111111111111</Maintenance> into (/Workbook/Worksheet)[1]');

Но это также не вставляет никакой узел.

Может ли кто-нибудь предложить, что я могу делать неправильно?


person Nimble Fungus    schedule 08.12.2015    source источник


Ответы (2)


Похоже, что при вставке здесь необходимо использовать пространство имен по умолчанию. Попробуй это.

set @xml.modify('
declare namespace ns="urn:schemas-microsoft-com:office:spreadsheet";
insert <ns:Maintenance>111111111111111</ns:Maintenance>
into (/ns:Workbook)[1]');


select @xml
person Kik    schedule 08.12.2015
comment
@NimbleFungus Это странно, поскольку у меня это работает, оно появляется под элементом Worksheet. Я попытаюсь получить его в скрипте sql. - person Kik; 08.12.2015
comment
Пожалуйста, смотрите мой ответ. Я использую SQL Server 2008 R2. Может ли версия повлиять на поведение? Но большое спасибо за помощь. Вы отвечаете мне на игру, чтобы попробовать мой ответ. - person Nimble Fungus; 08.12.2015

Это сработало, ребята..

 SET @xml.modify('
        declare default element namespace  "urn:schemas-microsoft-com:office:spreadsheet";
        declare namespace ss="urn:schemas-microsoft-com:office:spreadsheet" ;
        declare namespace x="urn:schemas-microsoft-com:office:excel";
        insert sql:variable("@xmlStyle") as first into (/Workbook)[1]')

Мне пришлось объявить все пространства имен, которые использовались в XML. @xmlStyle имеет тип XML и содержит фрагмент XML, который я хочу включить в качестве узла.

@xmlStyle AS XML
person Nimble Fungus    schedule 08.12.2015