简体   繁体   English

如何根据同一节点中不同属性值的匹配来查找和更新XML文档中的节点属性值?

[英]How can I find and update a node attribute value in an XML document based on match of a different attribute value in the same node?

Given my T-SQL stored procedure with: 鉴于我的T-SQL存储过程:

Declare @SearchID varchar(35)
Set @SearchID = '5280301.2019050148902.00023'

Declare @DocListXML XML  

Contains the following XML data 包含以下XML数据

<JobList ListItems="7">
  <Job JobFriendlyName="EMAIL INVOICES">
    <DocumentList>
      <Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
      <Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
      <Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="NEW" />
      <Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
      <Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
    </DocumentList>
  </Job>
  <Job JobFriendlyName="INVOICES">
    <DocumentList>
      <Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
      <Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
    </DocumentList>
  </Job>
</JobList>

I need T-SQL XML code to update this document and replace existing Status value with "OLD" for the node having ID attribute value matching the value of T-SQL local variable @SearchID . 我需要T-SQL XML代码来更新此文档,并将具有ID属性值与T-SQL本地变量@SearchID值匹配的节点的现有Status值替换为“OLD”。

After the update, the resulting document in @DocListXML should contain: 更新后, @DocListXML的结果文档应包含:

<JobList ListItems="7">
  <Job JobFriendlyName="EMAIL INVOICES">
    <DocumentList>
      <Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
      <Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
      <Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="OLD" />
      <Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
      <Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
    </DocumentList>
  </Job>
  <Job JobFriendlyName="INVOICES">
    <DocumentList>
      <Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
      <Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
    </DocumentList>
  </Job>
</JobList>

I have found the modify/replace code to modify an attribute value of "X" "Y", but cannot find code for modify/update updating attribute [a] value in node with attribute [b] value matching a local variable. 我找到修改/替换代码来修改属性值“X”“Y”,但找不到修改/更新更新属性[a]值的代码,其中属性[b]值与局部变量匹配。

Any help or suggestions would be appreciated. 任何帮助或建议将不胜感激。

Okay, after your last question you decided to got the CURSOR route, correct? 好的,在您上一个问题之后,您决定使用CURSOR路线,对吗? ;-) ;-)

Try it like this. 试试吧。

--Your mockup up
Declare @SearchID varchar(35) = '5280301.2019050148902.00023';

Declare @DocListXML XML=
N'<JobList ListItems="7">
  <Job JobFriendlyName="EMAIL INVOICES">
    <DocumentList>
      <Document Doc="1" ID="5280301.2019050148902.00020" Date="05-03-2019" Status="NEW" />
      <Document Doc="2" ID="5280301.2019050148902.00022" Date="05-03-2019" Status="NEW" />
      <Document Doc="3" ID="5280301.2019050148902.00023" Date="05-03-2019" Status="NEW" />
      <Document Doc="4" ID="5280301.2019050104301.00055" Date="05-02-2019" Status="NEW" />
      <Document Doc="5" ID="5280301.2019050104301.00056" Date="05-02-2019" Status="NEW" />
    </DocumentList>
  </Job>
  <Job JobFriendlyName="INVOICES">
    <DocumentList>
      <Document Doc="6" ID="5280300.2019050148901.00001" Date="05-03-2019" Status="NEW" />
      <Document Doc="7" ID="5280300.2019050148901.00002" Date="05-03-2019" Status="NEW" />
    </DocumentList>
  </Job>
</JobList>';

--This is the modifying command - 这是修改命令

SET @DocListXML.modify(N'replace value of (/JobList
                                           /Job
                                           /DocumentList
                                           /Document[@ID=sql:variable("@SearchID")]
                                           /@Status)[1] with "OLD"');

--check the result - 检查结果

SELECT @DocListXML;

Hint 1: This works only in cases where there is just one document with the given ID per XML! 提示1:这仅适用于每个XML只有一个具有给定ID的文档的情况!

Hint 2: You might shorten the command to this: 提示2:您可以将命令缩短为:

SET @DocListXML.modify(N'replace value of (//Document[@ID=sql:variable("@SearchID")]/@Status)[1] with "OLD"');

The deep search (triggered by the double slash // at the beginning of the XPath) tells the engine to find a <Document> fullfilling the predicate anywhere in your XML. 深度搜索 (由XPath开头的双斜杠//触发)告诉引擎在XML中的任何位置找到一个<Document>填充谓词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL根据兄弟节点属性值选择XML节点 - SQL Select XML Node based on sibling node attribute value 对于SQL Server XML数据类型,当使用另一个属性进行搜索以找到xml节点时,如何更新value属性? - With SQL Server XML data type, how to update the value attribute when searching with another attribute to locate the xml node? 如何使用sql server在XML文档中获得包含具有给定属性值的子节点的节点? - How do you get a node that contains a child node with a given attribute value in an XML document using sql server? 通过节点属性查询XML节点值 - Query XML Node Value by Node Attribute 根据来自 XML 类型列的匹配项更新 XML 节点值 - Update XML node value based on a match from XML type column 更新给定属性的 SQL XML 字段节点值 - Update a SQL XML Field node value for a given attribute 如何将XML文本节点添加到按属性值查询的XML片段 - How to add XML text node to an XML fragment queried by attribute value 更改xml根节点属性的值 - Change value of a xml root node attribute 如何在sql server中更新一个XML节点,而不是包含另一个具有特定值XML的节点 - How can I update an xml node than contains another node with a specific value XML in sql server 使用父节点上的where条件获取XML子节点的属性值 - get attribute value of XML child node with where condition on parent node
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM