使用XMLtable和Xpath转换XML Clob

时间:2016-09-20 13:52:38

标签: sql xml xml-parsing oracle10g

我正在尝试将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    Service filter  Pseudo Allowance    AND
iPass Roaming Zone 1    Last BLIMPACT Cycle Month   Wholesale   AND
iPass Roaming Zone 1            
iPass Roaming Zone 1    Provider ID 23102   OR
iPass Roaming Zone 1    Provider ID 21630   OR
iPass Roaming Zone 1    Provider ID 26201   OR
iPass Roaming Zone 1    Provider ID 23001   OR
iPass Roaming Zone 1    Provider ID 23203   OR
iPass Roaming Zone 1    Provider ID 20420   OR
iPass Roaming Zone 1    Provider ID 23207   OR
iPass Roaming Zone 1    Provider ID 21901   OR
iPass Roaming Zone 1    Provider ID 20416   OR
iPass Roaming Zone 1    Provider ID 22004   OR
iPass Roaming Zone 1            
iPass Roaming Zone 1    Service filter  ROAMBL  OR
iPass Roaming Zone 1    Service filter  ROAMGP  OR
iPass Roaming Zone 1    Service filter  GPRSRM  OR

以及我得到了什么:

NAME    value1  value2  value3  value4
iPass Roaming Zone 1                AND
iPass Roaming Zone 1    Service filter          
iPass Roaming Zone 1        Pseudo Allowance        
iPass Roaming Zone 1    Last BLIMPACT Cycle Month           
iPass Roaming Zone 1        Wholesale       
iPass Roaming Zone 1                OR
iPass Roaming Zone 1    Provider ID         
iPass Roaming Zone 1            23102   
iPass Roaming Zone 1    Provider ID         
iPass Roaming Zone 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    Provider ID         
iPass Roaming Zone 1            23203   
iPass Roaming Zone 1    Provider ID         
iPass Roaming Zone 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    Provider ID         
iPass Roaming Zone 1            20416   
iPass Roaming Zone 1    Provider ID         
iPass Roaming Zone 1            22004   
iPass Roaming Zone 1                OR
iPass Roaming Zone 1    Service filter          
iPass Roaming Zone 1        ROAMBL      
iPass Roaming Zone 1    Service filter          
iPass Roaming Zone 1        ROAMGP      
iPass Roaming Zone 1    Service filter          
iPass Roaming Zone 1        GPRSRM      

对于每个提取的值,我得到3个空格和连续的值 请帮助,我是XML新手,非常困惑,做错了什么

1 个答案:

答案 0 :(得分:0)

试试这样:

DECLARE @xml XML=
'Your XML here';

- 第一个CTE找到外部操作数并将它们编号

WITH OuterOperands AS
(
    SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS OperandID
          ,cond.value('@expressionType','nvarchar(100)') AS ConditionType
          ,o1.value('@expressionType','nvarchar(100)') AS OperandType
          ,o1.query('.') AS Operand
    FROM @xml.nodes('/QualificationCriterion/Condition') AS A(cond)
    OUTER APPLY cond.nodes('Operands/Operand') AS B(o1)
)

- 第二个CTE在外部操作数下面找到内部操作数并将它们编号为

,InnerOperands AS
(
    SELECT ROW_NUMBER() OVER(PARTITION BY OperandID ORDER BY (SELECT NULL)) AS InnerOperandID
          ,OuterOperands.*
          ,iop.query('.') AS InnerOperand
          ,iop.value('(LeftOperand/@attributeName)[1]','nvarchar(100)') AS LeftAttributeName
          ,COALESCE(iop.value('(RightOperand/@value)[1]','nvarchar(100)'),iop.value('(RightOperand/@name)[1]','nvarchar(100)')) AS RightValue
    FROM OuterOperands
    OUTER APPLY Operand.nodes('Operand/Operands/Operand') AS A(iop)
)

- 最终SELECT以产生您的预期输出

SELECT ConditionType
      ,OperandID
      ,InnerOperandID
      ,@xml.value('(/QualificationCriterion/@name)[1]','nvarchar(100)') AS QualificationCriterionName
      ,LeftAttributeName
      ,RightValue
      ,OperandType
      --,Operand --(find the rest here, if needed)
      --,InnerOperand --(find the rest here, if needed)
FROM InnerOperands

结果

注意:您应该添加ORDER BY OperandID,InnerOperandID以获得正确的排序顺序...

+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| ConditionType | OperandID | InnerOperandID | QualificationCriterionName | LeftAttributeName         | RightValue       | OperandType |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 1         | 1              | iPass Roaming Zone 1       | Service filter            | Pseudo Allowance | AND         |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 1         | 2              | iPass Roaming Zone 1       | Last BLIMPACT Cycle Month | Wholesale        | AND         |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 1              | iPass Roaming Zone 1       | Provider ID               | 23102            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 2              | iPass Roaming Zone 1       | Provider ID               | 21630            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 3              | iPass Roaming Zone 1       | Provider ID               | 26201            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 4              | iPass Roaming Zone 1       | Provider ID               | 23001            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 5              | iPass Roaming Zone 1       | Provider ID               | 23203            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 6              | iPass Roaming Zone 1       | Provider ID               | 20420            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 7              | iPass Roaming Zone 1       | Provider ID               | 23207            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 8              | iPass Roaming Zone 1       | Provider ID               | 21901            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 9              | iPass Roaming Zone 1       | Provider ID               | 20416            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 2         | 10             | iPass Roaming Zone 1       | Provider ID               | 22004            | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 3         | 1              | iPass Roaming Zone 1       | Service filter            | ROAMBL           | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 3         | 2              | iPass Roaming Zone 1       | Service filter            | ROAMGP           | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+
| AND           | 3         | 3              | iPass Roaming Zone 1       | Service filter            | GPRSRM           | OR          |
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+