本文介绍了如何修改包含特定子节点值的 XML 节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有 XML 列的表.如何在包含具有特定值的子节点的节点中添加另一个节点?例如,我想将 foo/bar/e/f
节点从 barId 6699 复制到 barId 66989.我只找到根据属性值选择节点的示例.我需要按子节点值过滤.
I have a table with an XML column. How can I add another node inside a node that contains a child node with a specific value? For example I'd like to copy foo/bar/e/f
node from barId 6699 to barId 66989. I only find examples where you select node based on an attribute value. I need to filter by child node value instead.
<foo>
<bar>
<barId>66988</barId>
<name>baz 3</name>
</bar>
<bar>
<barId>66989</barId>
<name>b </name>
<e>
<g>
<h>
<l>-</l>
<m>k</m>
</h>
<h>
<l>p v</l>
<m>k</m>
</h>
</g>
</e>
</bar>
<bar>
<barId>6699</barId>
<name>n 4 c</name>
<e>
<f>
<h>
<i>k</i>
<j>9.3</j>
</h>
</f>
</e>
</bar>
</foo>
推荐答案
试试这个:
DECLARE @value XML = '<foo><bar><barId>66988</barId><name>baz 3</name></bar><bar><barId>66989</barId><name>b </name><e><g><h><l>-</l><m>k</m></h><h><l>p v</l><m>k</m></h></g></e></bar><bar><barId>6699</barId><name>n 4 c</name><e><f><h><i>k</i><j>9.3</j></h></f></e></bar></foo>';
DECLARE @SourceID INT = 6699;
DECLARE @DestinationID INT = 66989;
DECLARE @temp XML;
SELECT @temp = T.c.query('.')
FROM @value.nodes('/foo/bar[barId = sql:variable("@SourceID")]/e/f') T(c);
SELECT @temp;
SET @value.modify('insert sql:variable("@temp") as last into (/foo/bar[barId =sql:variable("@DestinationID")]/e)[1]')
SELECT @value;
它会给你这个:
<foo>
<bar>
<barId>66988</barId>
<name>baz 3</name>
</bar>
<bar>
<barId>66989</barId>
<name>b </name>
<e>
<g>
<h>
<l>-</l>
<m>k</m>
</h>
<h>
<l>p v</l>
<m>k</m>
</h>
</g>
<f>
<h>
<i>k</i>
<j>9.3</j>
</h>
</f>
</e>
</bar>
<bar>
<barId>6699</barId>
<name>n 4 c</name>
<e>
<f>
<h>
<i>k</i>
<j>9.3</j>
</h>
</f>
</e>
</bar>
</foo>
这篇关于如何修改包含特定子节点值的 XML 节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!