【问题标题】:Select an XML attribtute of parent node with condition for child node选择具有子节点条件的父节点的 XML 属性
【发布时间】:2013-06-06 06:55:59
【问题描述】:

我在数据库中有一个 XMLType 列,我有一个条件来检查子节点文本并获取父节点的属性。在 XML 中,当我有 ConfigId(=5621507) 时,我需要获取数量 (qty="3")

我的 SQL:

select
cfg.session_xml.extract('/ns1:configurationSession/ns1:products/ns1:product/@qty',
'xmlns:ns1="http://abc.com/schema/service"')
from vz_cfg2_sess_xml cfg
where existsNode(cfg.session_xml,'/ns1:configurationSession/ns1:products/ns1:product/[ns1:configId=5621507]')=1;

我的 XML:

<ns1:configurationSession sessionId="3543737" xmlns:ns1="http://abc.com/schema/service" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <ns1:products>
        <ns1:product approvalCode="N" productId="ABC" qty="1">
            <ns1:configId>5229719</ns1:configId>
        </ns1:product>
        <ns1:product approvalCode="N" productId="DEF" qty="1">
            <ns1:configId>5621506</ns1:configId>
        </ns1:product>
        <ns1:product approvalCode="N" productId="DEF" qty="3">
            <ns1:configId>5621507</ns1:configId>
        </ns1:product>
    </ns1:products>
</ns1:configurationSession>

【问题讨论】:

    标签: sql xml oracle xpath xmltype


    【解决方案1】:

    为什么不这样:

    select
    cfg.session_xml.extract('/ns1:configurationSession/ns1:products/ns1:product[ns1:configId=5621507]/@qty',
    'xmlns:ns1="http://abc.com/schema/service"').getstringval()
    from vz_cfg2_sess_xml cfg
    

    Here is a sqlfiddle demo

    【讨论】:

      猜你喜欢
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多