get the list of nodes and node-text that contains specific text using XPATH

狂风中的少年 提交于 2019-12-23 02:19:31

问题


I am playing around with parsing XML files on remote server using oracle apex and pl/sql. I use APEX_WEB_SERVICE.MAKE_REST_REQUEST to get the xml file into a clob variable and then use XPATH and XMLTable to get specific text.

Here's a sample XML file that I have (note that, the original file is quite complex, this is only the representation of the content in the file):

<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>

Here's what I am trying to do with this file:

  1. Get a list of nodes that contain 'term1' in the text.
  2. Get the line of text that contain 'term1'

In this example, I should get the following:

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

and so on.

I am able to do a general text search, but it returns everything under the article, and not only the element that is required. For this example, I would get ul as well as li in the results, where as I only want li and the text within it.

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 is the clob that contains xml file.

Can someone suggest the correct xpath to be used for such?

来源:https://stackoverflow.com/questions/48085611/get-the-list-of-nodes-and-node-text-that-contains-specific-text-using-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!