0

現在、PL/SQL を使用して一部の XML を解析する際に問題が発生しています。基本的に、既存の関数によって提供される clob 変数に格納されている XML があります。clob を xmltype に変換するために使用する xmltype の新しい変数を作成します。次に、この新しい xmltype 変数の内容をループ処理して、各タグの内容を取り出して独自の div に出力しようとします。ただし、私のコードは現在、すべてのタグの内容を 1 つの div に出力するだけで、まったくループしていないという印象を与えます。

XML 構造は次のとおりです (明らかに、より多くの内部タグが入れ子になっています)。

 <?xml version="1.0" encoding="UTF-8"?>
    <results>
       <return>
          <createDate> Date 1 Here </createDate>
          <description> Description 1 here </description>
       </return>
       <return>
          <createDate> Date 2 Here </createDate>
          <description> Description 2 here </description>
       </return>
    </results>

そして、ループスルーに使用している PLSQL は次の場所にあります。

    -- parsing : Parse the xml and wrap description content in a html div
        l_html_build := '<!DOCTYPE html><html><head></head><body>';
        l_parse_xml := (xmltype(l_xml_response));
        l_parse_xml_index := 1;
  while l_parse_xml.existsNode('//description[' || To_Char(l_parse_xml_index) || ']') > 0 
     loop
        l_html_build := l_html_build || '<div class="description">';
        l_html_build := l_html_build || (xmltype(l_xml_response).extract('//description[' || To_Char(l_parse_xml_index) 
        || ']/text()').getclobval());
        l_html_build := l_html_build || '</div>';
        l_html_build := replace(l_html_build,'&lt;','<');
        l_html_build := replace(l_html_build,'&gt;','>');
        l_html_build := replace(l_html_build,'&amp;lt;','');
        l_html_build := replace(l_html_build,'&amp;gt;','');
        l_html_build := replace(l_html_build,'&amp;nbsp;','&nbsp;');
        l_html_build := l_html_build ||'</body></html>';
        l_parse_xml_index := l_parse_xml_index + 1; 
     end loop;  

一部のパラメータと変数の説明は、次のとおりです。

    l_xml_response     clob;  -- XML from another function is stored here
    l_html_build        clob; -- Used as a variable to store the output to be sent to the page
    l_parse_xml        xmltype; -- l_xml_response content is passed into this xmltype variable which is used for parsing
    l_parse_xml_index  number; -- Used as an index to assist with looping through the tags

出力は次のとおりです。

 <div class = "description">
  Description 1 here Description 2 here
 </div> 

それがいつあるべきか:

 <div class = "description">
  Description 1 here
 </div> 
 <div class = "description">
  Description 2 here
 </div> 

私は貿易によるhtml/css/php/javascriptプログラマーであり、PL/SQLを実際に行ったことがないため、どんな助けも大歓迎です(そして、私の上司は実際には役に立たないトレーニングを提供していません) .

前もって感謝します!

4

2 に答える 2

1

これは、jme1988 のソリューションよりも約 1 倍効率的であるように思われます - 少なくとも 1000returnレコードまでのおもちゃの例では。明らかに、このアプローチが作業するデータの量にどのようにスケーリングされるかはわかりません (ただし、かなりうまくスケーリングする必要があります)。

DECLARE
   l_c CLOB;
BEGIN
   l_html_build   := '<!DOCTYPE html><html><head></head><body>';
   l_parse_xml    := xmltype(l_xml_response);
   --
   BEGIN
      WITH xdata AS
         (
            SELECT xmltype ( l_xml_response )   xml
              from dual
         )
    SELECT XMLSERIALIZE ( CONTENT extract(xml, '//description') )  x
      INTO l_c
      FROM xdata
         ;
   END;
   l_html_build := l_html_build || l_c;
   --
   l_html_build := replace(l_html_build,'<description', '<div class="description"');
   l_html_build := replace(l_html_build,'</description>', '</div>');
   l_html_build := replace(l_html_build,'&amp;lt;','&lt;');
   l_html_build := replace(l_html_build,'&amp;gt;','&gt;');
   l_html_build := replace(l_html_build,'&amp;nbsp;','&nbsp;');
   l_html_build := l_html_build ||'</body></html>';
END;

うまくいけば、そのものがあなたのニーズを満たします。よろしく、カルテン

于 2013-02-08T16:03:36.697 に答える