简体   繁体   中英

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:

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

Declare @DocListXML XML  

Contains the following XML data

<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 .

After the update, the resulting document in @DocListXML should contain:

<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.

Any help or suggestions would be appreciated.

Okay, after your last question you decided to got the CURSOR route, correct? ;-)

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!

Hint 2: You might shorten the command to this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM