简体   繁体   中英

How to create XML nodes with attributes from Table

I have following example table

在此处输入图片说明

Code here

CREATE TABLE XMLData
(
    NodeName NVARCHAR(255),
    AttributA NVARCHAR(255),
    AttributB NVARCHAR(255),
    AttributC NVARCHAR(255),
) 

INSERT INTO XMLData VALUES 
('RowA','','abcd','efgh'),
('RowB','wxyz',NULL,NULL),
('RowC',NULL,'qwer','tyui'),
('RowD','stuv','erty','fghj')

SELECT * FROM dbo.XMLData

How can I get following XML ?

<NodeA>
  <NodeB />
  <NodeC AttributeX="">
    <RowA AttributeA="" AttributeB="abcd" AttributeC="efgh" />
    <RowB AttributeA="wxyz" />
    <RowC AttributeB="qwer" AttributeC="tyui" />
    <RowD AttributeA="stuv" AttributeB="erty" AttributeC="fghj" />
  </NodeC>
</NodeA>

I am beginner with XML, but I tried to play with something like this

SELECT
    (
    SELECT
        (
        SELECT '' AS '@AttributeX' FOR XML PATH('NodeC'),TYPE
        -- How to get table rows here ?
        )
    FOR XML PATH('NodeB'),TYPE -- Here it creates additional end NodeB tag
    )
FOR XML PATH('NodeA'),TYPE

This is - almost - what you wanted. As told in my comment you will not be able to create your Element's names ( <RowA> etc.) dynamically. For this you'd have to use dynamic SQL and I assume this is not what you want actually...

Here's my suggestion:

SELECT '' AS [NodeB]
      ,'' AS [NodeC/@AttributeX]
      ,(
          SELECT x.NodeName AS [@NodeName]
                ,x.AttributA AS [@AttributeA]
                ,x.AttributB AS [@AttributeB]
                ,x.AttributC AS [@AttributeC]
          FROM XMLData AS x
          FOR XML PATH('Row'),TYPE
       ) AS NodeC
FOR XML PATH(''),ROOT('NodeA')

The result

<NodeA>
  <NodeB></NodeB>
  <NodeC AttributeX="">
    <Row NodeName="RowA" AttributeA="" AttributeB="abcd" AttributeC="efgh" />
    <Row NodeName="RowB" AttributeA="wxyz" />
    <Row NodeName="RowC" AttributeB="qwer" AttributeC="tyui" />
    <Row NodeName="RowD" AttributeA="stuv" AttributeB="erty" AttributeC="fghj" />
  </NodeC>
</NodeA>

This was an XSLT to get "dynamic" element names out of the XML above

Credits to Tim C

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="*[@NodeName]">
        <xsl:element name="{@NodeName}">
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="@NodeName" />
</xsl:stylesheet>

And this was the result Test it here

<NodeA>
  <NodeB/>
  <NodeC AttributeX="">
    <RowA AttributeA="" AttributeB="abcd" AttributeC="efgh"/>
    <RowB AttributeA="wxyz"/>
    <RowC AttributeB="qwer" AttributeC="tyui"/>
    <RowD AttributeA="stuv" AttributeB="erty" AttributeC="fghj"/>
  </NodeC>
</NodeA>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM