Home Forums SQL Server 2008 T-SQL (SS2K8) How do I use one column for node names and the others for elements in that node? RE: How do I use one column for node names and the others for elements in that node?

  • ...

    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[/url]