在 XMLType 上选择不同的值

发布于 2024-09-30 23:38:01 字数 1631 浏览 3 评论 0原文

我有一个包含 XMLType 字段的表。该表是使用以下 DDL/DML 创建和加载的:

CREATE TABLE T_ECO_test_LOG
(
  SECID             NUMBER                      NOT NULL,
  LOG_ATTRIBUTES    SYS.XMLTYPE
)

INSERT INTO t_eco_test_log VALUES 
   (       1, XMLType(
              '<attributes>
  <attribute>
    <name>remoteAddress</name>
    <value>180.201.106.130</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>'));

INSERT INTO t_eco_test_log VALUES 
   (       2, XMLType(
              '<attributes>
  <attribute>
    <name>user</name>
    <value>xxxx</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>'));        

我想获取 /attributes/attribute/name 中行中的不同值;因此,对于数据 O 希望获得:

remoteAddress
domain
user

到目前为止,我已经尝试了以下查询:

select extractValue(value(x),'/attributes/attribute/name') 
  from t_eco_log,
        table(xmlsequence(extract(log_attributes,'/attributes')) )x

但我收到以下消息:

ORA-19025: EXTRACTVALUE 仅返回一个节点的值

如果我使用

select extract(value(x),'/attributes/attribute/name') 
  from t_eco_log,
        table(xmlsequence(extract(log_attributes,'/attributes')) )x

,我得到一个 XML 结果,其中包含:

<name>remoteAddress</name><name>domain</name>

但我想要将它们作为行,我该怎么做?

TIA

I've got a table that has a XMLType field. The table is created and loaded using the following DDL/DML:

CREATE TABLE T_ECO_test_LOG
(
  SECID             NUMBER                      NOT NULL,
  LOG_ATTRIBUTES    SYS.XMLTYPE
)

INSERT INTO t_eco_test_log VALUES 
   (       1, XMLType(
              '<attributes>
  <attribute>
    <name>remoteAddress</name>
    <value>180.201.106.130</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>'));

INSERT INTO t_eco_test_log VALUES 
   (       2, XMLType(
              '<attributes>
  <attribute>
    <name>user</name>
    <value>xxxx</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>'));        

I want to get the different values in /attributes/attribute/name, in rows; So with data O would like to get:

remoteAddress
domain
user

So far I've tried the following query:

select extractValue(value(x),'/attributes/attribute/name') 
  from t_eco_log,
        table(xmlsequence(extract(log_attributes,'/attributes')) )x

But I get the following message :

ORA-19025: EXTRACTVALUE returns value of only one node

If I use

select extract(value(x),'/attributes/attribute/name') 
  from t_eco_log,
        table(xmlsequence(extract(log_attributes,'/attributes')) )x

I got a XML result which contains :

<name>remoteAddress</name><name>domain</name>

But I would like to get them as rows, how can I do that?

TIA

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

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

发布评论

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

评论(2

辞旧 2024-10-07 23:38:02

类似于:

with x1 as (select xmltype('<attributes>
  <attribute>
    <name>remoteAddress</name>
    <value>180.201.106.130</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>') x2 from dual)
select extract(value(x3),'/attribute/name') 
  from x1,
        table(xmlsequence(extract(x2,'/attributes/*')) ) x3

如果你提供 CREATE TABLE 和 INSERT,那么更容易给出精确的 SQL

Something like :

with x1 as (select xmltype('<attributes>
  <attribute>
    <name>remoteAddress</name>
    <value>180.201.106.130</value>
  </attribute>
  <attribute>
    <name>domain</name>
    <value>BSI_US</value>
  </attribute>
</attributes>') x2 from dual)
select extract(value(x3),'/attribute/name') 
  from x1,
        table(xmlsequence(extract(x2,'/attributes/*')) ) x3

If you supply CREATE TABLE and INSERT, then it is easier to give a precise SQL

你又不是我 2024-10-07 23:38:02

我得到了它。根据加里所说:

with x1 as (select log_attributes x2 from t_eco_test_log)
select distinct(extractValue(value(x3),'/attribute/name')) 
  from x1,
        table(xmlsequence(extract(x2,'/attributes/*')) ) x3

谢谢!

I got it. Base on what Gary put:

with x1 as (select log_attributes x2 from t_eco_test_log)
select distinct(extractValue(value(x3),'/attribute/name')) 
  from x1,
        table(xmlsequence(extract(x2,'/attributes/*')) ) x3

Thank you!

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