2011-11-20 75 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 .... –