English 中文(简体)
将XML列入服务器2008年,列作一栏
原标题:Parsing XML in SQL Server 2008 to columns

I m trying to parse XML that is stored as a column in a table into it s basic components. The XML describes a rule, below is an example.

以下例子为:“Date = 12/23/2011 and Change = No”。

I d like to get the and operator between the rules (BOOLEAN AND) in a column, the left hand side and right hand side for each rule into columns (DATE, 12/23/2011), and the operation between LHS and RHS in another column (EQUAL TO).

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

这些规则的范围也越来越复杂。

更为复杂的规则:(WeekDay = 星期一和(从1个或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>

事先得到任何援助。

问题回答

如何做到这一点?

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

你们是否回想?





相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签