使用XPATH获取包含特定文本的节点和节点文本列表

时间:2018-01-03 21:48:54

标签: xml xpath plsql oracle-apex

我正在使用oracle apex和pl / sql解析远程服务器上的XML文件。我使用APEX_WEB_SERVICE.MAKE_REST_REQUEST将xml文件转换为clob变量,然后使用XPATH和XMLTable获取特定文本。

这是我的一个示例XML文件(请注意,原始文件非常复杂,这只是文件中内容的表示):

<article>
  <index>
    <term>term1</term>
    <term>term2</term>
  </index>
  <ul>
    <li>this is text with term1</li>
  </ul>
  <p>this is another text with term1</p>
  <p>this is text with term2</p>
</article>
<article>
  <index>
    <term>term2</term>
    <term>term3</term>
  </index>
  <ul>
    <li>this is text with term2</li>
  </ul>
  <p>this is another text with term1</p>
  <p>this is text with term2</p>
</article>

以下是我要对此文件进行的操作:

  1. 获取包含&#39; term1&#39;的节点列表在文中。
  2. 获取包含&#39; term1&#39;
  3. 的文字行

    在这个例子中,我应该得到以下内容:

    li: this is text with term1
    p: this is another text with term1
    

    等等。

    我能够进行一般文本搜索,但它会返回文章下的所有内容,而不仅仅是所需的元素。对于此示例,我会在结果中获得ul以及li,因为我只需要li及其中的文本。

    FOR r IN (  SELECT rownum rn, termtext 
                   FROM xmltable('/article//*[contains(.,"term1")]' passing XMLTYPE(sourceXML)
                                             columns termtext VARCHAR2(500) PATH '.')
                )
       LOOP
          DBMS_OUTPUT.PUT_LINE('Row: '||r.rn);      
          DBMS_OUTPUT.PUT_LINE('Text: '||r.termtext);      
    
       END LOOP;
    

    sourceXML是包含xml文件的clob。

    有人可以建议使用正确的xpath吗?

0 个答案:

没有答案
相关问题