Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

XML: replace value of - why is not working

i have this xml
<InvoiceList xmlns="http://schemas.adventure-works.com/Invoices">
  <Invoice InvoiceNo="1000">
    <Customer>Kim Abercrombie</Customer>
    <Items>
      <Item Product="1" Price="1.99" Quantity="2" />
      <Item Product="3" Price="2.49" Quantity="1" />
    </Items>
  </Invoice>
  <Invoice InvoiceNo="1001">
    <Customer>Sean Chai</Customer>
    <Items>
      <Item Product="1" Price="2.45" Quantity="2" />
    </Items>
  </Invoice>
</InvoiceList>

why is not working
UPDATE #Stores
SET Invoices.modify('declare default element namespace "http://schemas.adventure-works.com/Invoices";
  replace value of (/InvoiceList/Invoice/Items[@Price="1.99"]/@Price)[1]
  with "1.69"')
WHERE StoreID = 1
0
enrique_aeo
Asked:
enrique_aeo
2 Solutions
 
CarlsbergFTWCommented:
There isn't any 'StoreID' value in the xml you posted tho your query is using it to define specific data.
0
 
enrique_aeoAuthor Commented:
it is exits
XML-storeID.jpg
0
 
jonaskaCommented:
Your xpath is incomplete as I see it.
Should be:
UPDATE #Stores
SET Invoices.modify('declare default element namespace "http://schemas.adventure-works.com/Invoices";
  replace value of (/InvoiceList/Invoice/Items/Item[@Price="1.99"]/@Price)[1]
  with "1.69"')
WHERE StoreID = 1
0
 
Anthony PerkinsCommented:
The solution offered is absolutely correct, however just a word of caution, doing this type of replacement is risky at best.  If there are two values with price 1.99 in the same node (see below), then only the first will be replaced.
<InvoiceList xmlns="http://schemas.adventure-works.com/Invoices">
  <Invoice InvoiceNo="1000">
    <Customer>Kim Abercrombie</Customer>
    <Items>
      <Item Product="1" Price="1.99" Quantity="2" />
      <Item Product="3" Price="1.99" Quantity="1" />
    </Items>
  </Invoice>
  <Invoice InvoiceNo="1001">
    <Customer>Sean Chai</Customer>
    <Items>
      <Item Product="1" Price="2.45" Quantity="2" />
    </Items>
  </Invoice>
</InvoiceList>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now