1

我必须更新 XML 中的值:

<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
                  xmlns:o="urn:schemas-microsoft-com:office:office" 
                  xmlns:x="urn:schemas-microsoft-com:office:excel" 
                  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
                  xmlns:html="http://www.w3.org/TR/REC-html40">
            <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
                <Author>XXXXXX</Author>
                <LastAuthor>UCB User</LastAuthor>
                <Created>2019-10-31T13:04:09Z</Created>
                <Version>14.00</Version>
            </DocumentProperties>
            <a>5</a>
        </Workbook>

在我的例子中,这个 XML 在表tt字段xml_val中。

目标 XPath 是/Workbook/DocumentProperties/Created,其值为2019-10-31T13:04:09Z并且必须替换为2020-01-08

我绑定了这段代码:

select UPDATEXML(xml_val,
   '/Workbook/DocumentProperties/Created/text()','2020-01-08',
    'xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
              xmlns:o="urn:schemas-microsoft-com:office:office" 
              xmlns:x="urn:schemas-microsoft-com:office:excel" 
              xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
              xmlns:html="http://www.w3.org/TR/REC-html40"').getClobVal() as last
from tt;

//.getClobVal()到底是因为ORA-21500: internal error code, arguments: [%s], [%s], [%s], [%s], [%s], [%s] , [%s], [%s] (这里)

上面的代码不会改变任何东西。我认为这是因为DocumentProperties标记中声明了另一个命名空间。但我不知道如何在UPDATEXML子句中声明命名空间。

当我尝试使用此代码更新/Workbook/a中的值时,它可以正常工作:

select UPDATEXML(xml_val,
   '/Workbook/a/text()',2020-01-08,
    'xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
              xmlns:o="urn:schemas-microsoft-com:office:office" 
              xmlns:x="urn:schemas-microsoft-com:office:excel" 
              xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
              xmlns:html="http://www.w3.org/TR/REC-html40"').getClobVal() as last
from tt;

我尝试过但不起作用的不同命名空间组合:

--1

xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"

--2

xmlns="urn:schemas-microsoft-com:office:office"
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"

--3

xmlns="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"

--4

xmlns="urn:schemas-microsoft-com:office:office"

注意:我无法删除 DocumentProperties 标记中的命名空间声明,因为此 XML 是Excel-XML格式文件的一部分

4

1 回答 1

1

DocumentProperties元素及其子元素位于具有快捷方式的命名空间urn:schemas-microsoft-com:office:officeo;您需要在 Xpath 中使用它们的命名空间为这些元素添加前缀:

SELECT UPDATEXML(
         xml_val,
         '/Workbook/o:DocumentProperties/o:Created/text()',
         '2020-01-08',
         'xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
          xmlns:o="urn:schemas-microsoft-com:office:office" 
          xmlns:x="urn:schemas-microsoft-com:office:excel" 
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
          xmlns:html="http://www.w3.org/TR/REC-html40"'
       ) AS updated_xml
FROM   tt;
| UPDATED_XML |
| :------------------------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ------------------------------ |
| <工作簿 xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel " xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"><DocumentProperties xmlns="urn:schemas-microsoft -com:office:office"><Author>XXXXXX</Author><LastAuthor>UCB 用户</LastAuthor><Created>2020-01-08</Created><Version>14.00</Version></DocumentProperties>< a>5</a></工作簿> |

db<>在这里摆弄

但是,UPDATEXML已弃用,您应该使用XMLQUERY

SELECT XMLQuery(
         'declare default element namespace "urn:schemas-microsoft-com:office:spreadsheet"; (: :)
          declare namespace o = "urn:schemas-microsoft-com:office:office"; (: :)
          copy $i := $x modify 
          ( for $j in $i/Workbook/o:DocumentProperties/o:Created
            return replace value of node $j with $v )
          return $i'
         PASSING xml_val AS "x",
                 '2020-01-08' AS "v"
         RETURNING CONTENT
       ) AS updated_xml
FROM   tt

db<>在这里摆弄

于 2020-01-08T13:27:45.060 回答