简体   繁体   中英

Selecting Single XML from Multiple XMLs rows in a Table

I have an XML column in a table where each row of the table is a complete XML node. I am trying to simply trying to select a subset of these rows and generate an XML document out of it with a root node. I thought I could do the following, but it keeps added an extra wrapper around each node with the name of the XML column. Is there anything different I can do to not get this wrapper?

Sample Data Structure:

CREATE TABLE ActivityTable 
(
    XMLDATA AS XML
)

INSERT INTO [ActivityTable] VALUES ( '<Activity>This is activity one</Activity>' )
INSERT INTO [ActivityTable] VALUES ( '<Activity>This is activity two</Activity>' )
INSERT INTO [ActivityTable] VALUES ( '<Activity>This is activity three</Activity>' ) 

Query to get Data

SELECT 
    XMLdata FROM ActivityTable 
FOR XML PATH(''), ROOT('RootNode')

What I'm getting:

<root>
  <XMLdata>
    <Activity>This is activity one</Activity>
  </XMLdata>
  <XMLdata>
    <Activity>This is activity two</Activity>
  </XMLdata>
  <XMLdata>
    <Activity>This is activity three</Activity>
  </XMLdata>
</root>

What I want:

<root>
  <Activity>This is activity one</Activity>
  <Activity>This is activity two</Activity>
  <Activity>This is activity three</Activity>
</root>
SELECT XMLdata AS '*' 
FROM ActivityTable 
FOR XML PATH(''), ROOT('RootNode')

Columns with a Name Specified as a Wildcard Character

If the column name specified is a wildcard character (*), the content of that column is inserted as if there is no column name specified.

using .query('/Node') is a way of querying for a certain node, and you don't get the XMLData tags back. Hope it helps!

SELECT XMLDATA.query('/Activity') FROM ActivityTable 
FOR XML PATH(''), ROOT('root')

SQL Fiddle example

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