Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

How do I use one column for node names and the others for elements in that node? Expand / Collapse
Author
Message
Posted Wednesday, September 19, 2012 11:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, September 15, 2014 7:19 PM
Points: 7,127, Visits: 12,655
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
Post #1361538
Posted Wednesday, September 19, 2012 12:04 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:55 AM
Points: 2,852, Visits: 5,107

...
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1361551
Posted Wednesday, September 19, 2012 1:32 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 7,156, Visits: 15,255
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?
Post #1361595
Posted Wednesday, September 19, 2012 3:24 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 7,156, Visits: 15,255
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?
Post #1361655
Posted Wednesday, September 19, 2012 6:38 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
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!
Post #1361701
Posted Thursday, September 20, 2012 5:49 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 10:34 AM
Points: 386, Visits: 624

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

&lt;personGenderCodeCurrent&gt;M&lt;/personGenderCodeCurrent&gt;

Post #1361883
Posted Thursday, September 20, 2012 6:00 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
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

&lt;personGenderCodeCurrent&gt;M&lt;/personGenderCodeCurrent&gt;



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!
Post #1361894
Posted Thursday, September 20, 2012 7:27 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:55 AM
Points: 2,852, Visits: 5,107
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

&lt;personGenderCodeCurrent&gt;M&lt;/personGenderCodeCurrent&gt;



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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1361953
Posted Thursday, September 20, 2012 8:40 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:49 PM
Points: 3,648, Visits: 5,326
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

&lt;personGenderCodeCurrent&gt;M&lt;/personGenderCodeCurrent&gt;



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!
Post #1362034
Posted Thursday, September 20, 2012 10:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Monday, September 15, 2014 7:19 PM
Points: 7,127, Visits: 12,655
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

&lt;personGenderCodeCurrent&gt;M&lt;/personGenderCodeCurrent&gt;



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
Post #1362142
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse