Come estrarre i nodi foglia da Oracle XMLTYPE
Domanda
Voglio estrarre solo i nodi foglia da un oggetto XMLTYPE 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
Cosa dovrei usare come clausola WHERE in modo che restituisca solo questi:
<one>text</one>
<two>text</two>
<three>text</three>
Ho provato quanto segue ma non funzionano:
WHERE t.existsNode( '//*' ) = 0
WHERE t.existsNode( '/.//*' ) = 0
WHERE t.existsNode( './/*' ) = 0
Cosa mi sto perdendo?
Soluzione
Non importa, l'ho trovato:
WHERE
t.existsNode( '/*//*' ) = 0
Altri suggerimenti
L'Xpath // * [not (*)]
otterrà qualsiasi elemento di livello 1 o più profondo che non abbia figli:
Query 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
| T.GETSTRINGVAL() |
|---------------------|
| <one>text</one> |
| <two>text</two> |
| <three>text</three> |
Se vuoi essere in grado di includere l'elemento root, usa XPath (//*|/*)[not(*)[
Puoi provare il seguente 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;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow