Pregunta

Quiero extraer solo los nodos de hoja de un objeto XMLTYPE en 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

¿Qué debo usar como cláusula WHERE para que esto devuelva solo estos:

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

He intentado lo siguiente pero no funcionan:

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

¿Qué me estoy perdiendo?

¿Fue útil?

Solución

No importa, lo encontré:

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

Otros consejos

El XPath // * [not (*)] obtendrá cualquier elemento en el nivel 1 o más profundo que no tenga hijos:

Fiddle de SQL

Consulta 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

Resultados :

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

Si desea poder incluir el elemento raíz, use el XPath (//*|/*)[not(*)?

Puedes probar el siguiente 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;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top