如何在sql中为xml的所有子节点添加属性

how to add attribute for all subnodes of the xml in sql(如何在sql中为xml的所有子节点添加属性)
本文介绍了如何在sql中为xml的所有子节点添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在变量@xml中有一个像下面这样的xml

I have a xml like below in a variable @xml

<ContentTemplate>
  <Tab Title="Lesson">
    <Section Title="Lesson Opening" />
    <Section Title="Lesson/Activity" />
  </Tab>
  <Tab Title="Wrap Up and Assessment">
    <Section Title="Lesson Closing" />
    <Section Title="Tracking Progress/Daily Assessment" />
  </Tab>
  <Tab Title="Differentiated Instruction">
    <Section Title="Strategies - Keyword" />
    <Section Title="Strategies – Text" />
    <Section Title="Resources" />
    <Section Title="Acceleration/Enrichment" />
  </Tab>
  <Tab Title="District Resources">
    <Section Title="Related Content Items" />
    <Section Title="Other" />
  </Tab>
</ContentTemplate>

我想为上述 xml 中的所有选项卡节点插入一个属性..输出应如下所示:

I want to insert an attribute for all tab nodes in the above xml.. the output should be like below:

<ContentTemplate>
      <Tab Title="Lesson" PortletName="CommunitiesViewer">
        <Section Title="Lesson Opening" />
        <Section Title="Lesson/Activity" />
      </Tab>
      <Tab Title="Wrap Up and Assessment" PortletName="CommunitiesViewer">
        <Section Title="Lesson Closing" />
        <Section Title="Tracking Progress/Daily Assessment" />
      </Tab>
      <Tab Title="Differentiated Instruction" PortletName="CommunitiesViewer">
        <Section Title="Strategies - Keyword" />
        <Section Title="Strategies – Text" />
        <Section Title="Resources" />
        <Section Title="Acceleration/Enrichment" />
      </Tab>
      <Tab Title="District Resources" PortletName="CommunitiesViewer">
        <Section Title="Related Content Items" />
        <Section Title="Other" />
      </Tab>
    </ContentTemplate>

我尝试了下面的代码来得到上面的xml

i tried the following code to get the above xml

set @xml.modify( 'insert attribute PortletName {sql:variable("@PortletName")} into (ContentTemplate/Tab)[1]')

它只是更新第一个子节点.

its just update the first sub node.

如何更新xml的所有子节点..

how to update all the sub nodes of the xml..

提前致谢

推荐答案

变量中的 XML

DECLARE @xml XML=
N'<ContentTemplate>
  <Tab Title="Lesson">
    <Section Title="Lesson Opening" />
    <Section Title="Lesson/Activity" />
  </Tab>
  <Tab Title="Wrap Up and Assessment">
    <Section Title="Lesson Closing" />
    <Section Title="Tracking Progress/Daily Assessment" />
  </Tab>
  <Tab Title="Differentiated Instruction">
    <Section Title="Strategies - Keyword" />
    <Section Title="Strategies – Text" />
    <Section Title="Resources" />
    <Section Title="Acceleration/Enrichment" />
  </Tab>
  <Tab Title="District Resources">
    <Section Title="Related Content Items" />
    <Section Title="Other" />
  </Tab>
</ContentTemplate>';

1) FLWOR

.modify() 语句允许您更改 XML 中的一个合适的点,但是您需要多次调用才能更改许多位置.FLWOR 允许您重新构建 XML:

1) FLWOR

The .modify()-statement allows you to change one decent point in your XML, but you'd need many calls to change many places. FLWOR allows you to re-build the XML out of itself:

SET @xml=@xml.query(
'<ContentTemplate>
{
for $t in /ContentTemplate/Tab
   return 
   <Tab Title="{$t/@Title}" PortletName="CommunitiesViewer">
   {$t/*}
   </Tab>
}
</ContentTemplate>');

SELECT @xml

2) 使用 SELECT ... FOR XML PATH()

重建

你会用这种方法达到同样的效果:再次重建 XML,但这次它被切碎并用作新的 SELECT ... FOR XML PATH

SELECT tb.value('@Title','nvarchar(max)') AS [@Title]
      ,'CommunitiesViewer' AS [@PortletName]
      ,tb.query('*')
FROM @xml.nodes('/ContentTemplate/Tab') AS A(tb)
FOR XML PATH('Tab'),ROOT('ContentTemplate')

这篇关于如何在sql中为xml的所有子节点添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)