使用 SQL 编辑 XML 列。不那么结构化的 XML

2024-01-09

这个问题是一个这个问题的后续 https://stackoverflow.com/q/40080985/5089204

如何通过获取 ' 的新标签来编辑 XMLXXX',不是那么结构化的 xml。需要帮助,我对 XML 和 XQuery 非常陌生。如果 X 没有,则必须获取新标签(节点),如果是 1,则只需插入 1。有什么方法可以更大规模地操作字符串

<NewAttributeRules>
<items>
<NewAttributeItem>
  <Scale>CAAA</Scale>
  <ScaleName>OC07</ScaleName>
  <comment />
  <positiveRules>
    <NewAttributeRule type="POSITIVE">
      <conditions>
        <InCondition column="PPRD" colDataType="STRING">
          <values>
            <string>CAAAEXTENDED</string>
          </values>
        </InCondition>
      </conditions>
    </NewAttributeRule>
  </positiveRules>
  <negativeRules />
 </NewAttributeItem>


 <NewAttributeItem>
  <Scale>high TOTAL OTHERS</Scale>
  <ScaleName>b007</ScaleName>
  <comment />
  <positiveRules>
    <NewAttributeRule type="POSITIVE">
      <conditions>
        <InCondition column="ATC3" colDataType="STRING">
          <values>
            <string>B10787 EXT</string>
          </values>
        </InCondition>
      </conditions>
    </NewAttributeRule>
  </positiveRules>
  <negativeRules>
    <NewAttributeRule type="NEGATIVE">
      <conditions>
        <InCondition column="PPRD" colDataType="STRING">
          <values>
            <string>hkJKKK</string>
            <string>GAGHA</string>
            </values>
        </InCondition>
      </conditions>
    </NewAttributeRule>
    </negativeRules>
   </NewAttributeItem>
      

<NewAttributeItem>
  <Scale>***XXX***</Scale>
  <ScaleName>OC07</ScaleName>
  <comment />
  <positiveRules />
  <negativeRules />
</NewAttributeItem>
<NewAttributeItem>
  <Scale>***XXX***</Scale>
  <ScaleName>OC07</ScaleName>
  <comment />
  <positiveRules />
  <negativeRules />
 </NewAttributeItem>

<NewAttributeItem>
  <Scale>***XXX***</Scale>
  <ScaleName>b007</ScaleName>
  <comment />
  <positiveRules />
  <negativeRules />
 </NewAttributeItem>
<NewAttributeItem>
  <Scale>***XXX***</Scale>
  <ScaleName>b007</ScaleName>
  <comment />
  <positiveRules />
  <negativeRules />
 </NewAttributeItem>

</items>
</NewAttributeRules>

好的,现在 XML 是有效的......

连同您的信息其他问题 https://stackoverflow.com/q/40080985/5089204我建议这种方法:

我将您的 XML 放入已声明的变量中

declare @xml xml=
N'<NewAttributeRules>
  <items>
    <NewAttributeItem>
      <Scale>CAAA</Scale>
      <ScaleName>OC07</ScaleName>
      <comment />
      <positiveRules>
        <NewAttributeRule type="POSITIVE">
          <conditions>
            <InCondition column="PPRD" colDataType="STRING">
              <values>
                <string>CAAAEXTENDED</string>
              </values>
            </InCondition>
          </conditions>
        </NewAttributeRule>
      </positiveRules>
      <negativeRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>high TOTAL OTHERS</Scale>
      <ScaleName>b007</ScaleName>
      <comment />
      <positiveRules>
        <NewAttributeRule type="POSITIVE">
          <conditions>
            <InCondition column="ATC3" colDataType="STRING">
              <values>
                <string>B10787 EXT</string>
              </values>
            </InCondition>
          </conditions>
        </NewAttributeRule>
      </positiveRules>
      <negativeRules>
        <NewAttributeRule type="NEGATIVE">
          <conditions>
            <InCondition column="PPRD" colDataType="STRING">
              <values>
                <string>hkJKKK</string>
                <string>GAGHA</string>
              </values>
            </InCondition>
          </conditions>
        </NewAttributeRule>
      </negativeRules>
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>OC07</ScaleName>
      <comment />
      <positiveRules />
      <negativeRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>OC07</ScaleName>
      <comment />
      <positiveRules />
      <negativeRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>b007</ScaleName>
      <comment />
      <positiveRules />
      <negativeRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>b007</ScaleName>
      <comment />
      <positiveRules />
      <negativeRules />
    </NewAttributeItem>
  </items>
</NewAttributeRules>';

--和之前一样,CTE会读取ScaleName用于后面的分组,但是会让整个节点as is

WITH ScaleNames AS
(
    SELECT  ai.query('.') AS AiNode
           ,ai.value('(ScaleName)[1]','nvarchar(100)') AS ScaleName
    FROM @xml.nodes('/NewAttributeRules/items/NewAttributeItem') AS A(ai)
    WHERE ai.value('(Scale)[1]','nvarchar(100)')<>'***XXX***'
)

--此 SELECT 将使用现有节点重建整个 XML,并添加两倍的节点XXX nodes.

SELECT
(
    SELECT (
                SELECT x.AiNode AS [node()]
                FROM ScaleNames AS x
                WHERE x.ScaleName=ScaleNames.ScaleName
                FOR XML PATH(''),TYPE
           ) AS [node()]
          ,(SELECT
             (SELECT '***XXX***' AS Scale, ScaleName, '' AS comment, '' AS positiveRules, '' AS negativRules FOR XML PATH('NewAttributeItem'),TYPE )
            ,(SELECT '***XXX***' AS Scale, ScaleName, '' AS comment, '' AS positiveRules, '' AS negativRules FOR XML PATH('NewAttributeItem'),TYPE)
            FOR XML PATH(''),TYPE
           ) AS [node()]
    FROM ScaleNames
    GROUP BY ScaleName
    ORDER BY ScaleName
    FOR XML PATH(''),ROOT('items'),TYPE
)
FOR XML PATH(''),ROOT('NewAttributeRules')

结果

<NewAttributeRules>
  <items>
    <NewAttributeItem>
      <Scale>high TOTAL OTHERS</Scale>
      <ScaleName>b007</ScaleName>
      <comment />
      <positiveRules>
        <NewAttributeRule type="POSITIVE">
          <conditions>
            <InCondition column="ATC3" colDataType="STRING">
              <values>
                <string>B10787 EXT</string>
              </values>
            </InCondition>
          </conditions>
        </NewAttributeRule>
      </positiveRules>
      <negativeRules>
        <NewAttributeRule type="NEGATIVE">
          <conditions>
            <InCondition column="PPRD" colDataType="STRING">
              <values>
                <string>hkJKKK</string>
                <string>GAGHA</string>
              </values>
            </InCondition>
          </conditions>
        </NewAttributeRule>
      </negativeRules>
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>b007</ScaleName>
      <comment />
      <positiveRules />
      <negativRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>b007</ScaleName>
      <comment />
      <positiveRules />
      <negativRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>CAAA</Scale>
      <ScaleName>OC07</ScaleName>
      <comment />
      <positiveRules>
        <NewAttributeRule type="POSITIVE">
          <conditions>
            <InCondition column="PPRD" colDataType="STRING">
              <values>
                <string>CAAAEXTENDED</string>
              </values>
            </InCondition>
          </conditions>
        </NewAttributeRule>
      </positiveRules>
      <negativeRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>OC07</ScaleName>
      <comment />
      <positiveRules />
      <negativRules />
    </NewAttributeItem>
    <NewAttributeItem>
      <Scale>***XXX***</Scale>
      <ScaleName>OC07</ScaleName>
      <comment />
      <positiveRules />
      <negativRules />
    </NewAttributeItem>
  </items>
</NewAttributeRules>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SQL 编辑 XML 列。不那么结构化的 XML 的相关文章

随机推荐

  • 将制表符分隔的文本文件读取到 Pandas 数据框中时出现 RunTimeError

    我正在将一个制表符分隔的文本文件读入 pandas 数据帧 在阅读本文时 我遇到了运行时错误 我已经浏览了与此错误相关的帖子 所有这些帖子都暗示了在迭代时不应修改字典的规则他们 就我而言 我所做的就是读取文件 这个问题如何与迭代和更改 di
  • 公共本机移动应用程序中的 WSO2 Api Manager OAuth2 DCR 安全性

    我正在为 iOS 和 Android 设计一个公共本机移动应用程序的安全性 该应用程序使用 WSO2 Api Manager APIM 使用公开可用的 API 因此 我了解与此设置相关的安全问题 并且我想将 OAuth2 应用于本机应用程序
  • Firebase - 在 NestJS 框架中处理云事件

    我在用着NestJS https nestjs com作为我的后端框架和 Firebase 要在 HTTP 请求上将 Nest 与 Firebase 集成起来很简单 只需将 Nest 的 Express 实例附加到 Firebase 即可
  • MIPS 是字节可寻址的

    我一直在观看以下 URL 上解释 MIPS ISA 的讲座 据我目前的理解 对于32位MIP 主存储器有一个32位地址输入总线 存储器中的每个插槽保存8位 因此每个地址可以引用8位存储器 这就是它的字节可寻址的原因 由于寄存器大小是 32
  • jQuery 在 rtl 方向上的scrollLeft - FireFox 和 Chrome 中的不同值

    我有以下简单的HTML div div table tr td T1 td td T2 td td T3 td td T4 td td T5 td td T6 td td T7 td tr table div div br span spa
  • US-ASCII 中的无效字节序列(Ruby 1.9 + Rails 2.3.8 + mongodb + mongo_mapper)

    我的设置是 linux Ruby 1 9 Rails 2 3 8 mongodb mongo mapper 我跟着http railscasts com episodes 194 mongodb and mongomapper http r
  • 使用 Cloud Functions for Firebase 获取匿名用户

    我正在使用 Cloud Functions 来管理项目中的数据库和身份验证 我按照这个例子https github com firebase functions samples tree master delete unused accou
  • 将浮点输入字符串流时出现“浮点无效操作”

    我有一段简单的代码 它从 FORTRAN 生成的 REAL 数组中提取浮点数 然后将其插入流中进行记录 尽管这适用于前 30 个案例 但在第 31 个案例中 它因 浮点无效操作 而崩溃 代码是 int FunctionDeclaration
  • 分配给映射中的匿名结构值

    我正在使用 go 1 3 如何访问映射的匿名结构 ValueType 的字段 package main import fmt type Words map string struct pos int n int func main w ma
  • Liquibase 先决条件不起作用

    我正在尝试使用 liquibase 来跟踪使用 dropwizard migrations 的 postgresql 数据库的更改 我希望能够在现有的生产数据库上运行迁移 而不是从头开始重建 现在我正在分阶段进行测试 我创建了一个带有前提条
  • 根据角度和距离求出Y点

    在我的项目中 我想从 X 点到 Y 点画一条线 虽然我知道 X 点的位置 但我只知道 Y 点的角度和距离 所以我的问题是通过角度 距X点 和距离得到Y点的坐标 我在这个项目中使用 JavaScript 并且不想使用任何图形库 例如 X 点
  • AppEngine/Go:将新版本的 Go 与 SDK 结合使用

    目前 Go SDK 附带的 Go 版本是 1 6 2 但最新版本是 1 7 1 我需要一些自 1 6 2 以来发布的增强功能 错误修复 但是 当我更换goroot在 SDK 目录中包含 Go 1 6 2 且符号链接指向 1 7 1 的目录中
  • 使用 Promise.all 解决获取请求

    我有一个包含 4 个请求对象的数组 我想在它们上使用 Fetch API 并获取承诺 然后我想解决每一个承诺并取回价值 这是我构建请求对象的方法 let requestsArray urlArray map url gt let reque
  • jQuery 动画 - 什么时候是异步的,什么时候不是?

    我有两个要设置动画的 div div div div div 我在 jQuery 中调用 animate 如下所示 sprite animate width 1 400 character animate width 1 400 chara
  • 在 pandas 中使用 groupby 过滤数据

    我有一个 DataFrame 其中包含以下数据 每行代表电视剧每集中出现的一个单词 如果一个单词在一个剧集中出现 3 次 则 pandas 数据框有 3 行 现在我需要过滤一个单词列表 这样我应该只得到出现超过或等于 2 次的单词 我可以通
  • 捕获单个任务中的异常并重新启动它们

    如果我创建一系列asyncio顶级类中的任务 所有这些任务基本上都应该永远运行 如下所示 asyncio create task asyncio create task asyncio create task self event loop
  • UICollectionView 单元格不可见

    我最近在我的故事板中添加了一个 UICollectionView 它目前被另一个视图推入视图 这似乎工作正常 但是 使用故事板编辑器 我将视图设置为包含 35 个单元格 这些单元格在编辑器中看起来很好 但是当我运行应用程序中的单元格是不可见
  • 使用azure数据工厂更新Azure机器学习模型

    当我使用数据工厂更新 Azure ML 模型时 如文档所述 https learn microsoft com en us azure data factory v1 data factory azure ml update resourc
  • 如何使用 C++ 更改/设置 DNS?

    我正在尝试使用 C 更改 设置 DNS 目前我无法找到这方面的任何资源 public static NetworkInterface GetActiveEthernetOrWifiNetworkInterface var Nic Netwo
  • 使用 SQL 编辑 XML 列。不那么结构化的 XML

    这个问题是一个这个问题的后续 https stackoverflow com q 40080985 5089204 如何通过获取 的新标签来编辑 XMLXXX 不是那么结构化的 xml 需要帮助 我对 XML 和 XQuery 非常陌生 如