Postgres导入xml文件

时间:2015-07-24 13:39:10

标签: xml postgresql xpath triggers

我在触发函数中编写了以下代码序列

    CREATE OR REPLACE FUNCTION _00_bu_factura_kaem_xml() RETURNS trigger 
    AS  $BODY$
    declare lclXml xml = xml_import(new.file_name);
    lclDetails xml; 
    lclArticleCode text; lclSubInventoryCode text; lclMessageId text;
    lclIndex bigint; lclReceivedQty bigint; lclOrderedQty bigint;
    begin
      if new.file_name is not null then 
        lclMessageId = (select (xpath('./MessageID/text()',lclXml))                            [1]::text);
        lclDetails   = (select xpath('./Inbounds/Details/Detail',                             lclXml));
        for iCount in 1 .. (select (xpath('count(/Header/Inbounds/Details/Detail)', lclXml))[1]::text::bigint) loop
          select (xpath('./LineNo/text()', lclDetails))[iCount][1]::text::bigint into lclIndex;
        end loop;
      end if; 
      return new;  
    end;
    $BODY$

xml文件具有以下结构

    <?xml version="1.0" encoding="UTF-8"?>
    <Header>
       <SendDate>20150713</SendDate>
       <SendTime>183308</SendTime>
       <SenderID>KLG</SenderID>
       <ReceiverID>16</ReceiverID>
       <MessageID>00000000041666</MessageID>
       <Inbounds>
         <Inbound>
           <PONo>2015323930</PONo>
           <RecNoSupplier>00000000</RecNoSupplier>
           <DeliveryDate>20150708</DeliveryDate>
         </Inbound>
         <Details>
            <Detail>
               <LineNo>1</LineNo>
               <ArticleCode>2402-691505</ArticleCode>
               <SubInventoryCode>KLG-MAIN</SubInventoryCode>
               <OrderedQty>20</OrderedQty>
               <ReceivedQty>20</ReceivedQty>
            </Detail>
            <Detail>
               <LineNo>2</LineNo>
               <ArticleCode>0181-104018</ArticleCode>
               <SubInventoryCode>KLG-MAIN</SubInventoryCode>
               <OrderedQty>22</OrderedQty>
              <ReceivedQty>22</ReceivedQty>
            </Detail> ... contains 487 <Detail> nodes.

我想要的是优化&#34; for loop ... end&#39;因为我觉得我在每次迭代时创建xml结构节点以获取每个节点的值。

我尝试创建xml lclDetail 类型的变量,以存储顶点详细信息,但是&#34;选择...&#34;给我错误信息

ERROR:  could not parse XML document
DETAIL:  line 1: Start tag expected, '<' not found 
{"<Detail>
^
CONTEXT:  SQL function "xpath" statement 1
SQL statement "select (xpath('./LineNo/text()', lclDetails))[iCount]
[1]::text::bigint"
PL/pgSQL function _00_bu_factura_kaem_xml() line 18 at comandă SQL
********** Error **********

我需要使用提示&#34; lclDetail&#34;。

1 个答案:

答案 0 :(得分:0)

问题解决了。 更正是

declare lclDetails text[]; 
....
lclDetails = (select (xpath('./Inbounds/Details/Detail', lclXml)::text[]));
 for iCount in 1 .. (select (xpath('count(/Header/Inbounds/Details/Detail)', lclXml))[1]::text::bigint) loop 
   select (xpath('./LineNo/text()',           lclDetails[iCount]::xml))[1]::text::bigint, 
          (xpath('./ArticleCode/text()',     lclDetails[iCount]::xml))[1]::text,
          (xpath('./SubInventoryCode/text()', lclDetails[iCount]::xml))[1]::text,
          (xpath('./OrderedQty/text()',       lclDetails[iCount]::xml))[1]::text::bigint,
          (xpath('./ReceivedQty/text()',      lclDetails[iCount]::xml))[1]::text::bigint
   into lclIndex, lclArticleCode, lclSubInventoryCode, lclOrderedQty, lclReceivedQty;
   ....