SQL Server使用通用字段元素生成XML

时间:2014-08-20 18:58:52

标签: sql sql-server xml

我基本上试图扭转这个问题的要求...... SQL Server query xml attribute for an element value

我需要生成一个“row”元素的结果集,其中包含一组带有定义键的属性的“field”元素。

<resultset statement="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
    <field name="id">1</field>
    <field name="version”>0</field>
    <field name="property">My Movie</field>
    <field name="release_date">2012-01-01</field>
    <field name="territory_code”>FR</field>
    <field name="territory_description">FRANCE</field>
    <field name="currency_code”>EUR</field>
</row>
<row>
    <field name="id">2</field>
    <field name="version”>0</field>
    <field name="property">My Sequel</field>
    <field name="release_date">2014-03-01</field>
    <field name="territory_code”>UK</field>
    <field name="territory_description">United Kingdom</field>
    <field name="currency_code”>GBP</field>
</row>
</resultset>

我有一个返回此信息的查询...

<resultset statement="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
    <id>1</id>
    <version>0</version>
    <property>My Movie</property>
    <release_date>2012-01-01</release_date>
    <territory_code>FR</territory_code>
    <territory_description>FRANCE</territory_description>
    <currency_code>EUR</currency_code>
</row>
<row>
    <id>2</id>
    <version>0</version>
    <property>My Sequel</property>
    <release_date>2014-03-01</release_date>
    <territory_code>UK</territory_code>
    <territory_description>UNITED KINGDOM</territory_description>
    <currency_code>GBP</currency_code>
</row>
</resultset>

在我的SQL语句中使用FOR XML PATH ('row'), ROOT ('resultset')

我错过了什么?感谢。

3 个答案:

答案 0 :(得分:8)

它有点涉及SQL Server - 正常的行为就是你所看到的 - 列名将被用作XML元素名称。

如果确实希望所有XML元素的名称相同,那么您必须使用以下代码:

SELECT
    'id' AS 'field/@name',
    id AS 'field',
    '',
    'version' AS 'field/@name',
    version AS 'field',
    '',
    'property' AS 'field/@name',
    property AS 'field',
    '',
    ... and so on ....
FROM Person.Person
FOR XML PATH('row'),ROOT('resultset')

这是确保列名用作name元素上的<field>属性所必需的,并且空字符串是必需的,以便SQL XML解析器不会混淆关于哪个name属性属于哪个元素......

答案 1 :(得分:1)

您可以执行此操作,而无需将列指定为常量,这样您也可以使用select *。它比marc_s提供的答案稍微复杂一点,执行起来会慢得多。

select (
       select T.X.value('local-name(.)', 'nvarchar(128)') as '@name',
              T.X.value('text()[1]', 'nvarchar(max)') as '*'
       from C.X.nodes('/X/*') as T(X)
       for xml path('field'), type
       )
from (
     select (
            select T.*
            for xml path('X'), type
            ) as X
     from dbo.YourTable as T
     ) as C
for xml path('row'), root('resultset')

SQL Fiddle

查询创建一个派生表,其中每一行都有一个如下所示的XML:

<X>
  <ID>1</ID>
  <Col1>1</Col1>
  <Col2>2014-08-21</Col2>
</X>

然后使用nodes()local-name(.)对XML进行粉碎,以创建所需的形状。

答案 2 :(得分:-1)

您的SELECT语句需要看起来像这样

SELECT
    'id' AS [field/@name],
    id AS field,
    'version' AS [field/@name],
    version AS field,
    'property' AS [field/@name],
    property AS field,
    'release_date' AS [field/@name],
    release_date AS field,
    'territory_code' AS [field/@name],
    territory_code AS field,
    'territory_description' AS [field/@name],
    territory_description AS field,
    'currency_code' AS [field/@name],
    currency_code AS field