SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How do I use one column for node names and the others for elements in that node?


How do I use one column for node names and the others for elements in that node?

Author
Message
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39138 Visits: 14411
Timothy Graffham (9/19/2012)
I was very excited to go to my post this morning and see 8 responses. I thought I was going to get some real help. Instead, the first response was an attempt but didn't help really, and then everyone just used my post for a chat about general complaints with XML as a technology.

Very disappointing, folks. Can you please have those sort of conversations in their own threads?

My format that I illustrated in the original question was not flexible. I MUST display this information in this format even if I have to loop through the data procedurally and dynamically build it as a string. It's an installed base issue that I have to support.

My question is, is it possible? Can anyone tell me how to do this?

All fair points. These forums are in a 'discussion format' as opposed to a 'Q&A format' like http://ask.sqlservercentral.com and sometimes the ensuing discussions tend to dwarf the fact that sometimes people are just looking for a straight-forward answer to their question :-)

The answer to your question is not simple. You have big challenges because the app is expecting data values to be used as element names which is outside what FOR XML provides. You might want to think about building the XML 'by hand', i.e. by using a variable to build a string in the form the app wants, but that will be a hellish coding assignment.

....

I found this link that asks a similar question: http://stackoverflow.com/questions/3240297/how-do-i-use-column-values-as-xml-element-names-using-for-xml-in-sql-server-2005

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12282 Visits: 5478

...
I want the xml to look like this:

<PartsList>
<123>
...



It is not valid XML. You cannot have element named as number.
One of XML Naming Rule is: Names cannot start with a number or punctuation character
http://www.w3schools.com/xml/xml_elements.asp

But if you still want it as non-XML text, you can do the following with Dwain solution:


DECLARE @t TABLE (Part INT, Color VARCHAR(10), Size VARCHAR(10))

INSERT INTO @t
SELECT 123,'blue','small'
UNION ALL SELECT 124,'black','medium'
UNION ALL SELECT 125,'red','large'


select * from @t

DECLARE @xml VARCHAR(max)

SELECT @xml = (
SELECT Part, Color, Size, '/' + cast(Part as varchar) AS PartC
FROM @t
FOR XML PATH(''), ROOT('PartsList') )

SELECT REPLACE(REPLACE(REPLACE(REPLACE( @xml,'</Part>','>'),'<Part>','<'),'</PartC>','>'),'<PartC>','<')



Please Note, you will not be able to load above text to DOM object for use as XML...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29195 Visits: 19002
Assuming you fix your naming convention issue (yes - SQL Server will pick up on the invalid name issue), you can use a query like the one below (i fixed the names to make this work)

declare @x xml
set @x='<PartsList>
<a123>
<Color>blue</Color>
<Size>small</Size>
</a123>
<a124>
<Color>black</Color>
<Size>medium</Size>
</a124>
<a125>
<Color>red</Color>
<Size>large</Size>
</a125>
</PartsList>'

select c.value('local-name(.)','varchar(100)') colname,
c.value('(./Color)[1]','varchar(100)'),
c.value('(./Size)[1]','varchar(100)') size
from @x.nodes('/PartsList/*') vm(c)




EDIT: sry - I am looking at the wrong end of the issue - you're attempting to generate this - not parse it.

I am pretty sure this is possible using reasonably advanced FLOQWR statements, but it may take some time to get this right.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
Matt Miller (4)
Matt Miller (4)
One Orange Chip
One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)One Orange Chip (29K reputation)

Group: General Forum Members
Points: 29195 Visits: 19002
Here's a version of FLWOR which approximates what you want (again - with the name isssue handled)


declare @x xml

;with a as (
select 'a123' partID, 'blue' color,'large' size
union all
select 'a124' partID, 'green' color,'medium' size
union all
select 'a125' partID, 'red' color,'small' size)
select @x=(select partid as "@partid",color as "color", size from a for XML path ('part'), root('PartList'), type)

select cast(replace(replace(cast(
@x.query
('
for $a in PartList,
$e in $a/part
return <partlist>
{concat("<",string($e/@partid),">")}
{$e/*}
{concat("</",string($e/@partid),">")}
</partlist>
') as varchar(max)),'<','<'),'>','>') as XML)




----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17605 Visits: 6431
Timothy Graffham (9/19/2012)
I was very excited to go to my post this morning and see 8 responses. I thought I was going to get some real help. Instead, the first response was an attempt but didn't help really, and then everyone just used my post for a chat about general complaints with XML as a technology.

Very disappointing, folks. Can you please have those sort of conversations in their own threads?

My format that I illustrated in the original question was not flexible. I MUST display this information in this format even if I have to loop through the data procedurally and dynamically build it as a string. It's an installed base issue that I have to support.

My question is, is it possible? Can anyone tell me how to do this?


OK, so now Matt and Eugene have shown that it can be done.

The question now becomes will you be remiss in your responsibilities and not at least question why someone is asking for a format that isn't standard XML?

"All that is necessary for the triumph of evil is that good men do nothing. -- Edmund Burke

The SQLverse version:

"All that is necessary for the triumph of bad database design is that good DBAs don't question. -- Dwain.C

You can quote me.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
aaron.reese
aaron.reese
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2399 Visits: 907

I do have to agree with the byte-bloat issue.


Can anyone beat this? its from the NHS clinical data set upload for patient details

<personGenderCodeCurrent>M</personGenderCodeCurrent>


dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17605 Visits: 6431
aaron.reese (9/20/2012)

I do have to agree with the byte-bloat issue.


Can anyone beat this? its from the NHS clinical data set upload for patient details

<personGenderCodeCurrent>M</personGenderCodeCurrent>




They must be doing those special sex-change operations!;-)


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12282 Visits: 5478
aaron.reese (9/20/2012)

I do have to agree with the byte-bloat issue.


Can anyone beat this? its from the NHS clinical data set upload for patient details

<personGenderCodeCurrent>M</personGenderCodeCurrent>




There is nothing wrong with that!
XML is designed specifically for this.

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
dwain.c
dwain.c
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17605 Visits: 6431
Eugene Elutin (9/20/2012)
aaron.reese (9/20/2012)

I do have to agree with the byte-bloat issue.


Can anyone beat this? its from the NHS clinical data set upload for patient details

<personGenderCodeCurrent>M</personGenderCodeCurrent>




There is nothing wrong with that!
XML is designed specifically for this.



For gender transformations?


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Orlando Colamatteo
Orlando Colamatteo
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39138 Visits: 14411
dwain.c (9/20/2012)
Eugene Elutin (9/20/2012)
aaron.reese (9/20/2012)

I do have to agree with the byte-bloat issue.


Can anyone beat this? its from the NHS clinical data set upload for patient details

<personGenderCodeCurrent>M</personGenderCodeCurrent>




There is nothing wrong with that!
XML is designed specifically for this.



For gender transformations?

No, that is what XSLT is for.

__________________________________________________________________________________________________
There are no special teachers of virtue, because virtue is taught by the whole community. --Plato
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search