[FONT=Verdana][SIZE=2]I have been working on a stored procedure that outputs an xml file for use on a web site. Here’s the stored procedure (please note some field names and values have been edited to protect client).
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROC [dbo].[PH_PROC_MYDATA_XML]
AS
DROP TABLE PH_TMP_MYDATA_XML
CREATE TABLE PH_TMP_MYDATA_XML
(ID INT,DOCUMENT XML)
INSERT INTO PH_TMP_MYDATA_XML (ID)
VALUES (1)
UPDATE PH_TMP_MYDATA_XML
SET DOCUMENT=(
SELECT
'156' AS '@id',
'en-gb' AS '@lang',
'Business info' AS 'name',
'http://www.blahblahblahsite.com' AS 'url',
'do we want a description?' AS 'description',
'<![CDATA[ some stuff will have to go in here]]>' AS 'infotxt',
(SELECT
BUS_ID AS '@id',
Name AS 'name',
'http://www.blahblahblahsite.com/index.php?bus='+BUS_ID+'&othercode='+OTHERCODE AS 'site-url',
(SELECT
'email' AS 'contact/@type',
BUSEmail AS 'contact'--,
--'telephone' AS 'contact/@type',
--'0000 000 000' AS 'contact'
FOR XML PATH('contact-details'),TYPE),
(SELECT
BUSID AS '@id',
JOB_TITLE+' '+FIRST_NAME+' '+SURNAME AS 'name',
TYPE AS 'rank',
Comment AS 'bio',
'http://www.blahblahblahsite.com/images/photos/'+BUSID+'.jpg' AS 'photo-url'
FROM VW_Team_Names
WHERE VW_Team_Names.BUS = VW_BUSInfoLink_ForXML_v2.BUS_ID
FOR XML PATH('member'), TYPE, ROOT('members')),
(SELECT
Title AS 'issue',
AddDate AS 'issue-date',
Entry AS 'action',
ActionDate AS 'action-date'
FROM VW_Priorities_for_XML
WHERE (VW_Priorities_for_XML.BUS = VW_BUSInfoLink_ForXML_v2.BUS_ID)
FOR XML PATH('priority'), TYPE, ROOT('priorities'))
FROM VW_BUSInfoLink_ForXML_v2
WHERE BUS_ID <> '0'
FOR XML PATH('team'),TYPE, ROOT('teams'))
FOR XML PATH('business'),TYPE
)
WHERE ID=1
DECLARE @COMMAND NVARCHAR(400)
SET @COMMAND='bcp "SELECT TOP 1 DOCUMENT FROM BUS_V3.dbo.PH_TMP_MYDATA_XML" queryout \\\\someservername\\d$\\intepub\\wwwroot\\busdata.xml -Sserverval -Uusernameval -Ppasswordval -c -r -t'
SELECT @COMMAND
EXEC master..xp_cmdshell @COMMAND
Everything is working absolutely fine apart from the bit that is commented out (the lines prefixed with --):
(SELECT
‘email’ AS ‘contact/@type’,
BUSEmail AS ‘contact’–,
–‘telephone’ AS ‘contact/@type’,
–‘0000 000 000’ AS ‘contact’
FOR XML PATH(‘contact-details’),TYPE),
as soon as I try to execute when doing more than 1 contact type= I get the following error message:
Msg 6852, Level 16, State 1, Procedure PH_PROC_MYDATA_XML, Line 14
Attribute-centric column ‘contact/@type’ must not come after a non-attribute-centric sibling in XML hierarchy in FOR XML PATH.
The output I want in the xml is something like:
<contact-details>
<contact type=“telephone”>1111 111 1111</contact>
<contact type=“web”>http://www.sdr34ree34.com</contact>
</contact-details>
So please forgive me if I am using incorrect terminology but how do I set different multiple attributes for sibling nodes?[/SIZE][/FONT]
Any help would be greatly appreciated.
Many thanks for your time.