2011-11-20 81 views
2

我想解析存儲爲表中的一列的XML到它的基本組件中。 XML描述了一條規則,下面是一個例子。解析SQL Server 2008中的XML到列

下面的示例將顯示爲:「Date = 12/23/2011和Change = No」。

我想在規則(BOOLEAN AND)列中,每條規則的左側和右側插入列(DATE,12/23/2011)和操作符在另一列中的LHS和RHS之間(等於)。

<Conditions> 
    <FactsetConditionBase xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" d2p1:type="FactsetExpression" Operation="Boolean And"> 
    <Conditions> 
     <FactsetStatement Operation="Equal To"> 
     <Identifier Value="Date" /> 
     <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" d2p1:type="q1:string">12/23/2011</Value> 
     </FactsetStatement> 
     <FactsetStatement Operation="Equal To"> 
     <Identifier Value="Change" /> 
     <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" d2p1:type="q2:string">No</Value> 
     </FactsetStatement> 
    </Conditions> 
    </FactsetConditionBase> 
</Conditions> 

這些規則的規模也變得或多或少變得複雜。

一個更復雜的規則:(平日=星期一和(編號1開始或數量與2開始或數量與3)開始)

<Conditions> 
    <FactsetConditionBase xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" d2p1:type="FactsetExpression" Operation="Boolean And"> 
    <Conditions> 
     <FactsetExpression Operation="Boolean And"> 
     <Conditions> 
      <FactsetExpression Operation="Boolean And"> 
      <Conditions> 
       <FactsetStatement Operation="Equal To"> 
       <Identifier Value="WeekDay" /> 
       <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" d2p1:type="q1:string">Monday</Value> 
       </FactsetStatement> 
      </Conditions> 
      </FactsetExpression> 
      <FactsetExpression Operation="Boolean Or"> 
      <Conditions> 
       <FactsetStatement Operation="Begins With"> 
       <Identifier Value="Number" /> 
       <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" d2p1:type="q2:string">1</Value> 
       </FactsetStatement> 
       <FactsetStatement Operation="Begins With"> 
       <Identifier Value="Number" /> 
       <Value xmlns:q3="http://www.w3.org/2001/XMLSchema" d2p1:type="q3:string">2</Value> 
       </FactsetStatement> 
       <FactsetStatement Operation="Begins With"> 
       <Identifier Value="Number" /> 
       <Value xmlns:q4="http://www.w3.org/2001/XMLSchema" d2p1:type="q4:string">3</Value> 
       </FactsetStatement> 
      </Conditions> 
      </FactsetExpression> 
     </Conditions> 
     </FactsetExpression> 
    </Conditions> 
    </FactsetConditionBase> 
</Conditions> 

一個不太複雜的規則:顏色= RED

<Conditions> 
    <FactsetConditionBase xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" d2p1:type="FactsetStatement" Operation="Equal To"> 
    <Identifier Value="Color" /> 
    <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" d2p1:type="q1:string">RED</Value> 
    </FactsetConditionBase> 
</Conditions> 

在此先感謝您的幫助。

回答

2

這個怎麼樣?

DECLARE @rules TABLE (ID INT, XmlRule XML) 

INSERT INTO @rules VALUES(1, '<Conditions> 
    <FactsetConditionBase xmlns:d2p1="http://www.w3.org/2001/XMLSchema-instance" d2p1:type="FactsetExpression" Operation="Boolean And"> 
    <Conditions> 
     <FactsetStatement Operation="Equal To"> 
     <Identifier Value="Date" /> 
     <Value xmlns:q1="http://www.w3.org/2001/XMLSchema" d2p1:type="q1:string">12/23/2011</Value> 
     </FactsetStatement> 
     <FactsetStatement Operation="Equal To"> 
     <Identifier Value="Change" /> 
     <Value xmlns:q2="http://www.w3.org/2001/XMLSchema" d2p1:type="q2:string">No</Value> 
     </FactsetStatement> 
    </Conditions> 
    </FactsetConditionBase> 
</Conditions>') 

SELECT 
    r.ID, 
    T.Col.value('(@Operation)[1]', 'varchar(25)') AS 'Operation', 
    T2.Col2.value('(@Operation)[1]', 'varchar(25)') AS 'Operation2', 
    T2.Col2.value('(Identifier/@Value)[1]', 'varchar(25)') AS 'Identifier', 
    T2.Col2.value('(Value/text())[1]', 'varchar(25)') AS 'Value' 
FROM @rules r 
CROSS APPLY XmlRule.nodes('/Conditions/FactsetConditionBase') AS T(Col) 
CROSS APPLY T.Col.nodes('./Conditions/FactsetStatement') AS T2(Col2) 

它給我的輸出:

ID Operation Operation2 Identifier Value 
1 Boolean And Equal To  Date  12/23/2011 
1 Boolean And Equal To  Change  No 

是你在找什麼?

+0

感謝您的回覆Marc。您的解決方案當然適用於我的原始示例。我沒有意識到這些規則取決於它們代表的複雜性而採用不同的形式。我已經添加了幾個例子 - 一個更復雜,一個更簡單。 – macwiz

+0

@macwiz:恐怕您可能無法使用SQL中的一組XQuery解析所有這些不同形狀的XML .... –