用PL / SQL替换多个节点值

时间:2016-03-16 17:14:49

标签: xml plsql

我有一个包含5列的表格:ID | APP_ID | ID_NEU | SOURCE | TARGET

然后我有这样的事情:

DECLARE
xml_in_tabelle XMLTYPE := XMLTYPE('<?xml version="1.0" encoding="UTF-8"?>
                              <!-- 
                                ****************** 
                                ** Source     :  431
                                ** Source Lang:  de
                                ** Target     :  1011
                                ** Target Lang:  en-gb
                                ** Filename:     f431_1011_de_en-gb.xlf
                                ** Generated By: KRAEMM
                                ** Date:         10-FEB-2016 10:35:49
                                ****************** 
                               -->
                              <xliff version="1.0">
                              <file original="f431_1011_de_en-gb.xlf" source-language="de" target-language="en-gb" datatype="html">
                              <header></header>
                              <body>
                              <trans-unit id="S-2.1-121026922769234943-431">
                              <source>Login</source>
                              <target>Login</target>
                              </trans-unit>
                              <trans-unit id="S-2.1-121027126423248468-431">
                              <source>Logout</source>
                              <target>Logout</target>
                              </trans-unit>
                              </body>
                              </file>
                              </xliff>');
BEGIN
INSERT INTO XML_TEST(source,target, ID,APP_ID)
SELECT extractvalue(column_value, '/trans-unit/source') "source", 
       extractvalue(column_value, '/trans-unit/target') "target", 
       extractvalue(column_value, '/trans-unit/@id') "Trans-ID",
       SUBSTR(extractvalue(column_value, '/trans-unit/@id'), INSTR(extractvalue(column_value, '/trans-unit/@id'),'-', 2, 3)+1) "App_ID"
FROM TABLE(XMLSequence(xml_in_tabelle.extract('/xliff/file/body/trans-unit'))) t
WHERE extractvalue(column_value, '/trans-unit/@id') not in (SELECT ID FROM XML_TEST);
commit;Than
END;   

将一些数据插入表中(实际上它应该来自xml文件,但我不知道如何将xml插入到表中。但很快我就会发现; D)

这很好用。

除了我有一个程序,即在两个不同的XML中创建两个跨单元ID的中间部分之间的偏移量,将它们添加到ID中并将它们保存为ID_NEU。

   -- middle part: <trans-unit id="S-2.1-**xxxxxxxx**-431"> 



DECLARE 
x1_org NUMBER := 0;
x2 NUMBER := 0; 
app_ID_v NUMBER :=0;
offset NUMBER :=0;
xml_1 XMLTYPE := XMLTYPE('xml1');

xml_2 XMLTYPE := XMLTYPE('xml2');

BEGIN
  SELECT SUBSTR(extractvalue(column_value, '/trans-unit/@id'), INSTR(extractvalue(column_value, '/trans-unit/@id'),'-',1,2)+1
               ,INSTR(extractvalue(column_value, '/trans-unit/@id'),'-',1,3)-(INSTR(extractvalue(column_value, '/trans-unit/@id'),'-',1,2)+1))
  INTO x1_org
  FROM TABLE(XMLSequence(xml_1.extract('/xliff/file/body/trans-unit'))) t
  WHERE ROWNUM = 1;

  SELECT SUBSTR(extractvalue(column_value, '/trans-unit/@id'), INSTR(extractvalue(column_value, '/trans-unit/@id'),'-',1,2)+1
               ,INSTR(extractvalue(column_value, '/trans-unit/@id'),'-',1,3)-(INSTR(extractvalue(column_value, '/trans-unit/@id'),'-',1,2)+1))
  INTO x2
  FROM TABLE(XMLSequence(xml_2.extract('/xliff/file/body/trans-unit'))) t
  WHERE ROWNUM = 1;

  SELECT  SUBSTR(extractvalue(column_value, '/trans-unit/@id'), INSTR(extractvalue(column_value, '/trans-unit/@id'),'-', 2, 3)+1) "App_ID"
  INTO app_ID_v
  FROM TABLE(XMLSequence(xml_1.extract('/xliff/file/body/trans-unit'))) t
  WHERE ROWNUM = 1;

  offset := (x1_org-x2);


  UPDATE XML_TEST
  SET ID_NEU = REPLACE(ID,
                SUBSTR(ID, INSTR(ID,'-',1,2)+1
               ,INSTR(ID,'-',1,3)-(INSTR(ID,'-',1,2)+1))
               ,(SUBSTR(ID, INSTR(ID,'-',1,2)+1
               ,INSTR(ID,'-',1,3)-(INSTR(ID,'-',1,2)+1))+10)
               );

END;   

边节点:2个XML文件之间的偏移量始终相同

最后,我尝试将表中的值(目标值可能已更改)添加到第二个XML中。

我试过这样的事情:

     select updatexml(XMLTYPE('xml2')
    , '/xliff/file/body/trans-unit[@id="'||ID_NEU||'"]/target/text()',target).getClobVal() 
from XML_TEST;

但它不能像我想的那样工作。 (它仅更改实际列的值)

有人能帮助我吗? (我希望你明白我的意思,我的英语非常糟糕)

1 个答案:

答案 0 :(得分:0)

好的,我找到了一种方法来做我想做的事。 我是这样做的:

(我将XML保存为表中的CLOB)

DECLARE 
xml_neu clob;

BEGIN     FOR cur_xml_neu in ( SELECT ID, source, target FROM XML_TEST ) LOOP


    SELECT updatexml(XMLTYPE(a.XML_DATA_NEU) 
            , '/xliff/file/body/trans-unit[@id="'||cur_xml_neu.ID||'"]/target/text()', cur_xml_neu.target).getClobVal() INTO xml_neu
            from XML_ABLAGE a JOIN XML_TEST t
            ON a.APP_ID = t.APP_ID
            WHERE rownum = 1;

    UPDATE XML_ABLAGE 
    SET XML_DATA_NEU = xml_neu;

END LOOP;
END;  

(我在表格的每一行上循环,我保存了更改,然后一点一点地写回XML)