English 中文(简体)
SQL Server 2005 FOR XML
原标题:

I want to export data from a table to a specifically formatted XML file. I am fairly new to XML files, so what I am after may be quite obvious but I just cant find what I am looking for on the net.

The format of the XML results I need are:

    <data>
        <event 
            start="May 28 2006 09:00:00 GMT"
            end="Jun 15 2006 09:00:00 GMT"
            isDuration="true"
            title="Writing Timeline documentation"
            image="http://simile.mit.edu/images/csail-logo.gif">
            A few days to write some documentation
        </event>
     </data>

My table structure is:

name VARCHAR(50),
description VARCHAR(255),
startDate DATETIME,
endDate DATETIME

(I am not too interested in the XML fields image or isDuration at this point in time).

I have tried:

SELECT [name]
           ,[description]
           ,[startDate]
           ,[endTime]

  FROM [testing].[dbo].[time_timeline]
  FOR XML RAW( event ), ROOT( data ), type

Which gives me:

<data>
    <event name="Test1" 
           description="Test 1 Description...." 
           startDate="1900-01-01T00:00:00" 
           endTime="1900-01-01T00:00:00" 
    />
    <event name="Test2" 
           description="Test 2 Description...." 
           startDate="1900-01-01T00:00:00" 
           endTime="1900-01-01T00:00:00" 
    />
</data>

What I am missing, is the description needs to be outside of the event attributes, and there needs to be a tag.

Is anyone able to point me in the correct direction, or point me to a tutorial or similar on how to accomplish this?

Thanks,

Matt

最佳回答

This should do the job:

SELECT
    name "event/@name"
    , startDate "event/@start"
    , description "event"
FROM
    [testing].[dbo].[time_timeline]
FOR XML PATH(  ), ROOT( data )

Things to note:

  • In order to get description as the text content of event, we have to step up a level and use PATH( ), and specify the name event in the alias for all columns
  • All attribute-centric columns must come before all non-attribute-centric columns

For learning this stuff (or at least getting an idea of how to do what you want), see the docs for FOR XML and just play around with your own tables and desired XML structures.

问题回答

Consider switching to FOR XML PATH (see http://msdn.microsoft.com/en-us/library/ms189885.aspx for documentation)

A very partial example:

SELECT
    [name] 
   ,[description] 
   ,[startDate]   "@start"
   ,[endTime]     "@end"
  FROM [testing].[dbo].[time_timeline] 
  FOR XML PATH( event ), ROOT( data )




相关问题
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

热门标签