【问题标题】:How to specify attributes on root element?如何在根元素上指定属性?
【发布时间】:2021-05-18 20:00:02
【问题描述】:

短版

使用 SQL Server FOR XML ROOT('Customers'),如何向该根节点添加属性?

<Customers> ← attributes here
   <Customer>Ian</Customer>
   <Customer>Shelby</Customer>
   <Customer>Dave</Customer>
</Customers>

加长版

在 SQL Server 中使用 FOR XML 时:

SELECT *
FROM (VALUES
    (122, 'All-Purpose Bike Stand'),
    (119, 'Bike Wash'),
    (115, 'Cable Lock')
) AS Products(ProductModelID, Name)
FOR XML PATH('Product')

它通常只返回元素:

<Product>
  <ProductModelID>122</ProductModelID>
  <Name>All-Purpose Bike Stand</Name>
</Product>
<Product>
  <ProductModelID>119</ProductModelID>
  <Name>Bike Wash</Name>
</Product>
<Product>
  <ProductModelID>115</ProductModelID>
  <Name>Cable Lock</Name>
</Product>

(3 rows affected)

这不是一个有效的 XML 文档,因为有 三个 顶级节点 - 而不仅仅是一个。

这可以通过指定ROOT('RootNodeName')来解决:

SELECT *
FROM (VALUES
    (122, 'All-Purpose Bike Stand'),
    (119, 'Bike Wash'),
    (115, 'Cable Lock')
) AS Products(ProductModelID, Name)
FOR XML PATH('Product'), ROOT('Products')

<Products>
  <Product>
    <ProductModelID>122</ProductModelID>
    <Name>All-Purpose Bike Stand</Name>
  </Product>
  <Product>
    <ProductModelID>119</ProductModelID>
    <Name>Bike Wash</Name>
  </Product>
  <Product>
    <ProductModelID>115</ProductModelID>
    <Name>Cable Lock</Name>
  </Product>
</Products>

(3 rows affected)

非常好。

属性除外

以上内容很好,但是我还没有完成需要生成的 XML 文档。我需要为根节点添加一些属性:

<Products operationalMode="Test" batchDate="2021-02-15T17:36:22" formatId="8e884ace-bee4-11e4-8dfc-aa07a5b093db">
  <Product>
    <ProductModelID>122</ProductModelID>
    <Name>All-Purpose Bike Stand</Name>
  </Product>
  <Product>
    <ProductModelID>119</ProductModelID>
    <Name>Bike Wash</Name>
  </Product>
  <Product>
    <ProductModelID>115</ProductModelID>
    <Name>Cable Lock</Name>
  </Product>
</Products>

如何向XML ROOT('rootNode') 元素添加属性?

【问题讨论】:

    标签: sql-server sql-server-2012 for-xml-path for-xml


    【解决方案1】:

    请尝试以下解决方案。

    XML 至少应格式正确。为了有效,它需要一个 XML Schema。

    不清楚属性的来源是什么,所以我只是硬编码了它们的值。

    如您所见,我们需要应用两次FOR XML PATH 子句。一次用于“内部”XML。第二次用于根元素,并通过带有 at 符号的别名指定属性。

    SQL

    SELECT 'Test'AS [@operationalMode]
        , '2021-02-15T17:36:22' AS [@batchDate]
        , '8e884ace-bee4-11e4-8dfc-aa07a5b093db' AS [@formatId]
    , (
        SELECT *
        FROM (VALUES
            (122, 'All-Purpose Bike Stand'),
            (119, 'Bike Wash'),
            (115, 'Cable Lock')) AS Products(ProductModelID, Name)
        FOR XML PATH('Product'), TYPE
    )
    FOR XML PATH('Products'), TYPE;
    

    输出

    <Products operationalMode="Test" batchDate="2021-02-15T17:36:22" formatId="8e884ace-bee4-11e4-8dfc-aa07a5b093db">
      <Product>
        <ProductModelID>122</ProductModelID>
        <Name>All-Purpose Bike Stand</Name>
      </Product>
      <Product>
        <ProductModelID>119</ProductModelID>
        <Name>Bike Wash</Name>
      </Product>
      <Product>
        <ProductModelID>115</ProductModelID>
        <Name>Cable Lock</Name>
      </Product>
    </Products>
    

    【讨论】:

      猜你喜欢
      • 2015-02-17
      • 2012-12-08
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多