简体   繁体   中英

Convert tabular data to XML using SQL Server

I have a flattened table that contains columns that represent groups that need to be displayed in XML. Example data:

Market, Label, Style, Type
XXX,    YYY,   JJJ,   111
XXX,    YYY,   JJJ,   222
XXX,    YYY,   JJJ,   333
XXX,    YYY,   JJJ,   444    
XXX,    YYY,   LLL,   111    
XXX,    YYY,   LLL,   222    
XXX,    YYY,   LLL,   333    
XXX,    YYY,   LLL,   444

Using T-SQL what would be the best way to output the following:

<Market value=XXX>
    <label value=YYY>
       <Style value=JJJ>
          <Type value=111>
          </Type>
          ...
       </Style>
       <Style value=LLL>
          ...
       </Style>
    </label>
</Market>

Can I do this by using the XML Explicit clause in SQL Server?

This might help.. take a look

SELECT distinct MyTable.Market "Market/@Value",
        MyTable.Label "Market/Label/@Value",
        MyTable.Style "Market/Label/Style/@Value",

             (SELECT Type AS  "Value"
               FROM   MyTable myTab
               WHERE myTab.Market=MyTable.Market
                        and myTab.Label=MyTable.Label
                        and myTab.Style = MyTable.Style
               FOR XML PATH ('')
               ) AS "Market/Label/Style/Type"
        FROM MyTable  
FOR XML PATH('')

Resultant XML was :

<Market Value="XXX">
  <Label Value="YYY">
    <Style Value="JJJ">
      <Type>&lt;Value&gt;111&lt;/Value&gt;&lt;Value&gt;222&lt;/Value&gt;&lt;Value&gt;333&lt;/Value&gt;&lt;Value&gt;444&lt;/Value&gt;</Type>
    </Style>
  </Label>
</Market>
<Market Value="XXX">
  <Label Value="YYY">
    <Style Value="LLL">
      <Type>&lt;Value&gt;111&lt;/Value&gt;&lt;Value&gt;123&lt;/Value&gt;</Type>
    </Style>
  </Label>
</Market>

Hope it helps

Formatting complex XML documents in T-SQL is a fool's errand. It can be done - maybe - but then you come back to it a month later and what you've got is incomprehensible.

It's much, much easier to either write a method in C# or whatever that processes a DataReader to produce the XML, or write an XSLT transform that converts the XML emitted from the query into whatever specialized format you're trying to create.

Have a look at the SQLXML library. You can create an annotated XSD file that will produce an XML document.

http://msdn.microsoft.com/en-us/library/ms172699

Just be forewarned that the performance of this method is quite bad. I have made multiple requests to Microsoft to look at this issue. I have compared hand-writing a "FOR XML" query that takes milliseconds to finish, and the annotated XSD version takes 10 seconds.

I personally think that annotated XSDs are a very elegant solution if you can work around the poor performance.

there are 3 shaping method to generate table to xml in SQL Server 2000:

  1. for xml auto -> generate the query to xml automatically based on using table
  2. for xml raw -> generate the query table to row tag in xml
  3. for xml explicit -> the most complex but most comfort way :) you can defined your own xml structure

for the cheat sheet you can see this link

hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM