使用取自序列的值将属性添加到 xmltype

Add attribute to xmltype with value taken from sequence(使用取自序列的值将属性添加到 xmltype)
本文介绍了使用取自序列的值将属性添加到 xmltype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用的是 11.2 版的 Oracle XE.

I am using Oracle XE in version 11.2.

我有这样的 XML:

<root>
    <x a="a"/>
    <x a="b"/>
    <x a="c"/>
</root>

不,我想将属性 b 添加到每个元素 x ,其值取自序列,但它应该为每个元素采用新值.预期结果是:

No I would like to add attribute b to each element x with value taken from sequence, but it should take new value for each element. Expected result is:

<root>
    <x a="a" b="1"/>
    <x a="b" b="2"/>
    <x a="c" b="3"/>
</root>

我发现要向 XML 添加属性,我可以使用 insertchildxml 但在将相同的(第一个)值从序列添加到所有属性 b.我找不到如何为每个单独的元素 x 调用此函数.

I've found that to add attribute to XML I can use insertchildxml but in is adding the same (frist) value from sequence to all attributes b. I can't find how to call this function for each individual element x.

我将不胜感激.

推荐答案

我终于找到了一些解决方案,关键是使用 XMLTable() 函数.这是我的代码:

I've finaly found some solution and key to it was to use XMLTable() function. Here is my code:

declare
v_inXML xmltype;
  v_tmpXML xmltype;
  v_withIdXML xmltype;
  v_outXML xmltype;
BEGIN   
  v_inXML := XMLType('<root><x a="a"/><x a="b"/><x a="c"/></root>');
  v_withIdXML := XMLType('<root/>'); 
  v_outXML := XMLType('<root/>');

  for c_rec in (
    select *
    from   XMLTable('for $i in /root/x
          return $i'
          passing  v_inXML
          columns x xmltype path '/x'
    )
  )
  loop
    select insertchildxml(c_rec.x,'//x', '@b', pckg_ent_pk_seq.nextval) into v_tmpXML from dual;      
    select insertchildxml(v_withIdXML, '/root', 'x', v_tmpXML) into v_withIdXML from dual;    
  end loop;

  select updatexml(v_outXML, '/root', v_withIdXML) into v_outXML from dual;

  dbms_output.put_line(v_outXML.getClobVal());
END;

结果是:

<root><x a="a" b="92"/><x a="b" b="93"/><x a="c" b="94"/></root>

这篇关于使用取自序列的值将属性添加到 xmltype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Union in SQL while creating XML file(创建 XML 文件时在 SQL 中联合)
strange behavior of SQL Server when sum nodes#39;s values in XML(对 XML 中的节点值求和时 SQL Server 的奇怪行为)
How to update a SQL table column with XML data(如何使用 XML 数据更新 SQL 表列)
How To Save XML Query Results to a File(如何将 XML 查询结果保存到文件)
Extracting XML sub-tags from a clob in Oracle via SQL(通过 SQL 从 Oracle 中的 clob 中提取 XML 子标签)
ROW_NUMBER query(ROW_NUMBER 查询)