有命名空间时更新 XML
UPDATEXML when there is namespaces
我必须更新 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 在 table 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: 内部错误码,参数:[%s], [%s], [%s], [%s] , [%s], [%s], [%s], [%s] (here)
上面的代码不会改变任何东西。我认为这是因为在 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 格式文件
的一部分
DocumentProperties
元素及其子元素位于具有快捷方式 o
的命名空间 urn:schemas-microsoft-com:office:office
中;您需要在 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 |
| :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <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>2020-01-08</Created><Version>14.00</Version></DocumentProperties><a>5</a></Workbook> |
db<>fiddle here
但是,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<>fiddle here
我必须更新 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 在 table 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: 内部错误码,参数:[%s], [%s], [%s], [%s] , [%s], [%s], [%s], [%s] (here)
上面的代码不会改变任何东西。我认为这是因为在 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 格式文件
的一部分DocumentProperties
元素及其子元素位于具有快捷方式 o
的命名空间 urn:schemas-microsoft-com:office:office
中;您需要在 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 | | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | <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>2020-01-08</Created><Version>14.00</Version></DocumentProperties><a>5</a></Workbook> |
db<>fiddle here
但是,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<>fiddle here