如何从 Oracle XMLTYPE 中提取叶节点

发布于 2024-07-15 07:57:03 字数 943 浏览 8 评论 0原文

我只想从 Oracle 10g 中的 XMLTYPE 对象中提取叶节点

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

我应该使用什么作为 WHERE 子句,以便只返回这些:

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

我已经尝试了以下方法,但它们不起作用:

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

我缺少什么?

I want to extract only the 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 I use as the WHERE clause so this returns only these:

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

I've tried the following but they don't work:

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

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-07-22 07:57:03

没关系,我找到了:

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

Nevermind, I found it:

WHERE
    t.existsNode( '/*//*' ) = 0
聆听风音 2024-07-22 07:57:03

XPath //*[not(*)] 将获取级别 1 或更深级别的任何没有任何子元素的元素:

SQL Fiddle

查询 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> |

如果您希望能够包含根元素,则使用 XPath (//*|/*)[not( *)]

The XPath //*[not(*)] will get any element at level 1 or deeper that does not have any children:

SQL Fiddle

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

Results:

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

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

不美如何 2024-07-22 07:57:03

您可以尝试以下 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;

You can try the following 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文