问题
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:
- Get a list of nodes that contain 'term1' in the text.
- 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