How to extract sheet nodes from Oracle XMLTYPE

I want to extract only leaf nodes from an XMLTYPE object in Oracle 10g

SELECT
    t.getStringVal() AS text
FROM
    TABLE( XMLSequence( 
        XMLTYPE(
            '<xml>
                <node>
                    <one>text</one>
                </node>
                <node>
                    <two>text</two>
                </node>
                <node>
                    <three>text</three>
                </node>
            </xml>'
        ).extract( '//*' ) 
    ) ) t

What should be used as a WHERE clause, so that it returns only the following values:

                    <one>text</one>
                    <two>text</two>
                    <three>text</three>

I tried the following, but they do not work:

WHERE t.existsNode( '//*' ) = 0
WHERE t.existsNode( '/.//*' ) = 0
WHERE t.existsNode( './/*' ) = 0

What am I missing?

+3
source share
3 answers

Nevermind, I found it:

WHERE
    t.existsNode( '/*//*' ) = 0
0
source

XPath //*[not(*)]will receive any element at level 1 or deeper that has no children:

SQL Fiddle

Request 1 :

SELECT t.getStringVal()
FROM   TABLE(
         XMLSEQUENCE(
           XMLTYPE(
             '<xml>
             <node><one>text</one></node>
             <node><two>text</two></node>
             <node><three>text</three></node>
             </xml>'
           ).extract( '//*[not(*)]' )
         )
       ) t

Results :

|    T.GETSTRINGVAL() |
|---------------------|
|     <one>text</one> |
|     <two>text</two> |
| <three>text</three> |

If you want to include the root element, use XPath (//*|/*)[not(*)]

0
source

PL/SQL:

DECLARE
  x XMLType := XMLType(
            '<?xml version="1.0" ?>
            <xml>
                <node>
                    <one>text</one>
                </node>
                <node>
                    <two>text</two>
                </node
                <node>
                    <three>text</three>
                </node>
            </xml>');
BEGIN
  FOR r IN (
    SELECT ExtractValue(Value(p),'/XML/node/one/text()') as one_x
          ,ExtractValue(Value(p),'/XML/node/two/text()') as TWO_x
          ,ExtractValue(Value(p),'/XML/node/three/text()') as three
    FROM   TABLE(XMLSequence(Extract(x,'/XML/node/'))) p
    ) LOOP
    /* do whatever you want with r.one_x, r.TWO_x, r.three_x */
  END LOOP;
END;
-1

Source: https://habr.com/ru/post/1704221/


All Articles