Como extrair nós folha da Oracle XmlType
Pergunta
Eu quero extrair apenas os nós de folha de um objeto XmlType no 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
O que devo usar como a cláusula WHERE para que este retorna apenas estes:
<one>text</one>
<two>text</two>
<three>text</three>
Eu tentei o seguinte mas não fazer o trabalho:
WHERE t.existsNode( '//*' ) = 0
WHERE t.existsNode( '/.//*' ) = 0
WHERE t.existsNode( './/*' ) = 0
O que eu estou ausente?
Solução
Não importa, eu achei:
WHERE
t.existsNode( '/*//*' ) = 0
Outras dicas
O //*[not(*)]
XPath terá qualquer elemento no nível 1 ou mais profundo que não tem filhos:
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
| T.GETSTRINGVAL() |
|---------------------|
| <one>text</one> |
| <two>text</two> |
| <three>text</three> |
Se você quer ser capaz de incluir o elemento raiz, em seguida, usar o XPath (//*|/*)[not(*)]
Você pode tentar o seguinte 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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow