how to add attribute for all subnodes of the xml in sql

泄露秘密 提交于 2021-02-19 08:39:22

问题


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>

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>

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.

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

thanks in advance


回答1:


Your XML in a variable

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

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) Rebuild with SELECT ... FOR XML PATH()

You'd reach the same with this approach: Again the XML is re-built, but this time it is shredded and used as a new 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')


来源:https://stackoverflow.com/questions/40111130/how-to-add-attribute-for-all-subnodes-of-the-xml-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!