2016-09-20 74 views
0

我试图提取如下XML形式为行和列,但没有得到它想要的格式转换使用XMLTABLE和XPath

XML XML的Clob: -

<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的新的和非常困惑,究竟我做错了

+0

我的答案有点快,我的答案是SQL Server语法......哪个DBMS是你的(供应商和版本)?标签'sql'是不够的... – Shnugo

+0

由于下面的评论添加了'oracle10g'标签... – Shnugo

回答

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) 
) 

--Second 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) 
) 

--Final 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   | 
+---------------+-----------+----------------+----------------------------+---------------------------+------------------+-------------+ 
+0

我正在使用oracle 10g.can你也帮助我的Oracle以及 –

+0

@ChandanBikash,那么,一个不应该反应如此之快:-) ......可能是你明白了,你可以将这种方法转化为你的语言...... – Shnugo