dbTalk Databases Forums  

Attributes and elements in FOR XML subqueries

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss Attributes and elements in FOR XML subqueries in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Chloe C
 
Posts: n/a

Default Attributes and elements in FOR XML subqueries - 12-29-2009 , 05:50 AM






Hi

I'm trying to build an XML document from a relational schema. A lot of
it works well, but I've having trouble building sub-elements which
have attributes as well as data. A simplified example is:

Tables

Name NameId Type
1 person

NamePart NameId Type Value
1 given Chloe
1 family Crowder

What I want is
<name type="person"><namePart type="given">Chloe</namePart><namePart
type="family">Crowder</namePart></name>

I've tried
SELECT n.type as "@type", (SELECT np.type AS "@type", np.value as
"namePart" FROM NamePart np WHERE np.NameId = n.NameId FOR XML
PATH(N'namePart'), TYPE)
FROM Name n
FOR XML PATH(N'name'),
TYPE


But this gives an unwanted extra layer of <namePart></namePart> (with
the attribute on the outer one). Omitting namePart from the SELECT
clause gives an unwanted <value></value> round np.value, while
omitting namePart from the FOR XML Path clause gives a compilation
error

Msg 6864, Level 16, State 1, Line 1
Row tag omission (empty row tag name) cannot be used with
attribute-centric FOR XML serialization.

Any bright ideas on this?

Chloe

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Attributes and elements in FOR XML subqueries - 12-29-2009 , 07:52 AM






Chloe C wrote:
Quote:
Hi

I'm trying to build an XML document from a relational schema. A lot of
it works well, but I've having trouble building sub-elements which
have attributes as well as data. A simplified example is:

Tables

Name NameId Type
1 person

NamePart NameId Type Value
1 given Chloe
1 family Crowder

What I want is
name type="person"><namePart type="given">Chloe</namePart><namePart
type="family">Crowder</namePart></name
SELECT
n.type as "@type",
(SELECT
np.type AS "@type",
np.value as "text()"
FROM NamePart np WHERE np.NameId = n.NameId
FOR XML PATH(N'namePart'), TYPE)
FROM Name n
FOR XML PATH(N'name'), TYPE

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #3  
Old   
Chloe C
 
Posts: n/a

Default Re: Attributes and elements in FOR XML subqueries - 12-29-2009 , 08:08 AM



Martin - perfect! It's easy when you know how (and find the right
place in the documentation).

Many thanks.

On Tue, 29 Dec 2009 14:52:50 +0100, Martin Honnen <mahotrash (AT) yahoo (DOT) de>
wrote:

Quote:
Chloe C wrote:
Hi

I'm trying to build an XML document from a relational schema. A lot of
it works well, but I've having trouble building sub-elements which
have attributes as well as data. A simplified example is:

Tables

Name NameId Type
1 person

NamePart NameId Type Value
1 given Chloe
1 family Crowder

What I want is
name type="person"><namePart type="given">Chloe</namePart><namePart
type="family">Crowder</namePart></name

SELECT
n.type as "@type",
(SELECT
np.type AS "@type",
np.value as "text()"
FROM NamePart np WHERE np.NameId = n.NameId
FOR XML PATH(N'namePart'), TYPE)
FROM Name n
FOR XML PATH(N'name'), TYPE

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.