如何遍历SQL中xml数据类型列中的元素 [英] How to loop through elements in a column of xml datatype in SQL

查看:65
本文介绍了如何遍历SQL中xml数据类型列中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL列中存储了一个svg图像。列数据类型是XML。 xml基本上类似于:

I have stored a svg image in SQL column. The column datatype is XML. The xml basically looks something like:

<svg>
  <rect id="1">
    <rect id="in1" stroke="black"/>
    <circle id="in2" />
    <rect id="in3" stroke="black">
      <rect id="31" fill="Red"/>
    </rect>
    <circle id="in4"/>
  </rect>

 <g>
  <rect id="2">
    <circle>
     <rect id="in5">
  </rect>
 </g>
</svg>





如何循环t通过SQL中的xml,以便找到正确的< rect>标记为id 31并将其填充颜色更改为Blue



How to loop through the xml in SQL so that I can find the correct <rect> tag with id 31 and change its fill color to "Blue"

推荐答案

您可以使用替换值 [ ^ ]。



如下所示:

You can use replace value of[^].

So something like the following:
DECLARE @data xml;
SET @data = '<svg>
  <rect id="1">
    <rect id="in1" stroke="black" />
    <circle id="in2" />
    <rect id="in3" stroke="black">
      <rect id="31" fill="Red" />
    </rect>
    <circle id="in4" />
  </rect>
 
 <g>
  <rect id="2">
    <circle>
     <rect id="in5" />
    </circle>
  </rect>
 </g>
</svg>';

SELECT @data;

SET @data.modify('
  replace value of (//rect[@id=("31")]/@fill)[1]
  with "blue"
');
SELECT @data;


这篇关于如何遍历SQL中xml数据类型列中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆