【问题标题】:PL/SQL Using XMLQuery/XQuery to insert elements from one XML document inside another at a specific pointPL/SQL 使用 XMLQuery/XQuery 将元素从一个 XML 文档插入到另一个 XML 文档中的特定点
【发布时间】:2018-04-11 17:21:35
【问题描述】:

我想知道是否有人可以帮忙?

我正在尝试将一个 xml 文档中的所有元素(根级别以下)插入另一个。这两个文档都作为 XMLType 列驻留在 Oracle 12c 表中。我希望能够从他们的表中读取它们,执行转换,然后将其作为新文档写到表中。其中大部分内容都很简单,但我在执行插入的 XQuery 操作时遇到了问题……我天真地认为这很简单,但似乎无法做到正确。在 Oracle 12c 之前,我能够使用 appendchildxml 函数:

select
appendchildxml(my_output, '/Message', my_other_file, 'xmlns="SFA/ILR/2016-17"') into my_output
from
dual;

但该功能在 Oracle 12c 中已弃用,所以我现在需要使用 XQuery。

update xml_testing_12c
set 
my_ilrxml = 
XMLQuery('
xquery version "1.0";

copy $ilr := .
modify
    insert nodes $dp_out/Destinations/node() as last into $ilr/Message
return $ilr
'
passing my_ilr, my_dpoutcome as "dp_out" returning content
)
where
run_number = (select max(run_number) from xml_testing_12c);

my_ilr 和 my_dpoutcome 是 PL/SQL XMLType 变量,包含我的有效 XML 文档。它们具有以下结构:

my_ilr 如下:

<Message xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="SFA/ILR/2016-17" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="SFA/ILR/2016-17">
  <Header>
  <CollectionDetails>
   </CollectionDetails>
  <Source>
   </Source>
  </Header>
  <LearningProvider>
   </LearningProvider>
  <Learner>
    <LearningDelivery>
     </LearningDelivery>
  </Learner>
</Message>

my_dpoutcome 如下:

<Destinations>
  <LearnerDestinationandProgression>
    <LearnRefNumber></LearnRefNumber>
    <ULN></ULN>
    <DPOutcome>
      <OutType></OutType>
      <OutCode></OutCode>
      <OutStartDate></OutStartDate>
      <OutCollDate></OutCollDate>
    </DPOutcome>
  </LearnerDestinationandProgression>
</Destinations>

父节点中有大量​​LearnerDestination和Progression元素。

我收到以下错误:

ORA-19112:评估期间出现错误: XVM-01155:[XUDY0027] 无效的目标表达式 ORA-06512:在第 286 行

任何帮助将不胜感激。

谢谢。

【问题讨论】:

    标签: xml plsql xquery


    【解决方案1】:

    XUDY0027 - 意思是。你正试图在 null 中插入一些东西。

    你的 xmls 有不同的命名空间。 1) 消息有 xmlns="SFA/ILR/2016-17"
    2) Destinations 有 xmlns="some default url for oracle"

    当您在 xmlquery 中处理两个 xml 时,此表达式 $ilr/Message 返回 null。因为在默认命名空间中没有元素 Message。

    快速修复是:* - 任何命名空间;

    insert nodes $dp_out/Destinations as last into $ilr/*:Message

    更优雅的方式是命名空间声明。

    xquery version "1.0";
    declare namespace ns="SFA/ILR/2016-17";
    copy $ilr := .
    modify
        insert nodes $dp_out/Destinations  as last into $ilr/ns:Message
    return $ilr
    '
    

    【讨论】:

    • 非常感谢您的清晰解释。我已经按照建议修改了我的代码,现在它正在工作。再次感谢。
    猜你喜欢
    • 2013-02-22
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多