【问题标题】:Converting XML Clob using XMLtable/Xpath and storing it in Database table使用 XMLtable/Xpath 转换 XML Clob 并将其存储在数据库表中
【发布时间】:2016-09-27 09:09:59
【问题描述】:

我正在尝试使用 Oracle Pl/SQL 将 XMl 下面的内容提取到行和列中,但没有得到所需的格式

XML:-

<QualificationCriterion name="iPass Roaming Zone 1" description="" id="##PC4.0##183647208" status="CheckedIn" kind="Implementation" eventName="##PC4.0##67625472">
    <Condition xsi:type="LogicalExpression" expressionType="AND" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Operands>
            <Operand xsi:type="LogicalExpression" expressionType="AND">
                <Operands>
                    <Operand xsi:type="Comparison" expressionType="NE" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6249415" entity="Event" entityName="##PC4.0##67625472" attributeName="Service filter"/>
                        <RightOperand xsi:type="ValueRef" type="##PC4.0##6249415" name="Pseudo Allowance "/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##76915825" entity="Customer" entityName="##PC4.0##701" attributeName="Last BLIMPACT Cycle Month"/>
                        <RightOperand xsi:type="ValueRef" type="##PC4.0##76915825" name="Wholesale"/>
                    </Operand>
                </Operands>
            </Operand>
            <Operand xsi:type="LogicalExpression" expressionType="OR">
                <Operands>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="23102"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="21630"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="26201"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="23001"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="23203"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="20420"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##8537479" value="23207"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##6250817" value="21901"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##6250817" value="20416"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6250817" entity="Event" entityName="##PC4.0##67625472" attributeName="Provider ID"/>
                        <RightOperand xsi:type="Constant" type="##PC4.0##6250817" value="22004"/>
                    </Operand>
                </Operands>
            </Operand>
            <Operand xsi:type="LogicalExpression" expressionType="OR">
                <Operands>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6249415" entity="Event" entityName="##PC4.0##67625472" attributeName="Service filter"/>
                        <RightOperand xsi:type="ValueRef" type="##PC4.0##6249415" name="ROAMBL"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6249415" entity="Event" entityName="##PC4.0##67625472" attributeName="Service filter"/>
                        <RightOperand xsi:type="ValueRef" type="##PC4.0##6249415" name="ROAMGP"/>
                    </Operand>
                    <Operand xsi:type="Comparison" expressionType="EQ" type="Boolean">
                        <LeftOperand xsi:type="AttributeRef" type="##PC4.0##6249415" entity="Event" entityName="##PC4.0##67625472" attributeName="Service filter"/>
                        <RightOperand xsi:type="ValueRef" type="##PC4.0##6249415" name="GPRSRM"/>
                    </Operand>
                </Operands>
            </Operand>
        </Operands>
    </Condition>
</QualificationCriterion>

预期输出:-

NAME value1 value2 value4 iPass Roaming Zone 1 服务过滤器 Pseudo Allowance AND iPass Roaming Zone 1 Last BLIMPACT Cycle Month Wholesale AND iPass Roaming Zone 1 iPass Roaming Zone 1 Provider ID 23102 或 iPass Roaming Zone 1 Provider ID 21630 或 iPass 漫游区 1 提供商 ID 26201 或 iPass 漫游区 1 提供商 ID 23001 或 iPass 漫游区 1
提供商 ID 23203 或 iPass 漫游区 1 提供商 ID 20420 或 iPass Roaming Zone 1 提供商 ID 23207 或 iPass Roaming Zone 1
提供商 ID 21901 或 iPass 漫游区 1 提供商 ID 20416 或 iPass Roaming Zone 1 提供商 ID 22004 或 iPass Roaming Zone 1
iPass Roaming Zone 1 服务过滤器 ROAMBL OR iPass Roaming Zone 1 服务过滤器 ROAMGP OR iPass Roaming Zone 1 服务过滤器 GPRSRM 或

而我到现在为止,

名称 value1 value2 value3 value4 iPass 漫游区 1
AND iPass Roaming Zone 1 服务过滤器 iPass Roaming Zone 1 Pseudo Allowance iPass Roaming Zone 1 Last BLIMPACT 周期月 iPass 漫游 1 区批发 iPass 漫游区 1 或 iPass 漫游区 1
提供商 ID iPass Roaming Zone 1 23102 iPass 漫游区 1 提供商 ID iPass 漫游区 1
21630 iPass Roaming Zone 1 Provider ID iPass Roaming Zone 1 26201 iPass Roaming Zone 1 Provider ID
iPass Roaming Zone 1 23001 iPass Roaming Zone 1
提供商 ID iPass Roaming Zone 1 23203 iPass 漫游区 1 提供商 ID iPass 漫游区 1
20420 iPass Roaming Zone 1 Provider ID iPass Roaming Zone 1 23207 iPass Roaming Zone 1 Provider ID
iPass Roaming Zone 1 21901 iPass Roaming Zone 1
提供商 ID iPass Roaming Zone 1 20416 iPass 漫游区 1 提供商 ID iPass 漫游区 1
22004 iPass Roaming Zone 1 或 iPass Roaming Zone 1
服务过滤器 iPass Roaming Zone 1 ROAMBL
iPass Roaming Zone 1 服务过滤器 iPass Roaming Zone 1 ROAMGP iPass Roaming Zone 1 服务过滤器 iPass 漫游区 1 GPRSRM

我的代码:-

SELECT xt.*
FROM   (select * from pc_element where element_type='QualificationCriterion' and name='ETDR Group' and first_effective_version is not null and last_effective_version is null) x,
       XMLTABLE('/QualificationCriterion/Condition | //Operand[not(@type="Boolean")] | //Operands/Operand/LeftOperand | //Operands/Operand/RightOperand'
         PASSING x.xmltype(element_value)
         COLUMNS
        -- "value0"    VARCHAR2(50) PATH '@expressionType',
           "value1"    VARCHAR2(50) PATH '@attributeName',
           "value2" varchar2(10) PATH '@name',
            "value3" varchar2(10) PATH '@value',
            "value4"    VARCHAR2(50) PATH '@expressionType'
         ) xt ; 

对于每个值提取,我都会得到 3 个空格,并且该值连续请帮助,我是 XML 解析的新手,非常困惑,请帮助

【问题讨论】:

  • @PrashantMishra:更新了
  • @PrashantMishra 我的代码也被添加了

标签: xml plsql oracle10g extract xmltable


【解决方案1】:

这是您需要的查询:

SELECT xt.NAME NAME, xt.attributeName value1, nvl(RName,RValue) value2, xt.expressionType value4
FROM   (select * from pc_element where element_type='QualificationCriterion' and name='ETDR Group' and first_effective_version is not null and last_effective_version is null) x,
   --XMLTABLE('/QualificationCriterion/Condition | //Operands/Operand[not(@type="Boolean")] | //Operands/Operand/LeftOperand | //Operands/Operand/RightOperand'
   XMLTABLE('//Operand[@type="Boolean"]' 
     PASSING x.xmltype(element_value)
     COLUMNS
       NAME   VARCHAR2(50) PATH './../../../../../@name',
       attributeName    VARCHAR2(50) PATH './LeftOperand/@attributeName',
       RName varchar2(50) PATH './RightOperand/@name',
        RValue varchar2(50) PATH './RightOperand/@value',
        expressionType    VARCHAR2(50) PATH './../../@expressionType'
        --"self"    varchar2(50) path 'name()'
     ) xt ; 

【讨论】:

  • :- 非常感谢,几乎是需要的,但没有得到 NAME 和 value4 列值:-(
  • 尝试根据@name 属性的位置增加或减少../ 的数量。也对 value4 做类似的事情。
猜你喜欢
  • 1970-01-01
  • 2021-08-30
  • 2011-12-30
  • 2021-05-03
  • 1970-01-01
  • 2014-04-19
  • 2016-10-04
  • 2020-08-27
  • 1970-01-01
相关资源
最近更新 更多