2016-09-20 73 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