Paweł Zakrzewski
Paweł Zakrzewski

Reputation: 41

XMLStarlet updating node with the expression

I am struggling with the xmlstarlet's update function. I am trying to update xml node value with the value of another.

In my exmaple I'm trying to copy FVALUE field value to QUANTITY field with the command:

xml ed -u "INVOICE_ITEM_LIST/INVOICE_ITEM/QUANTITY" -x "INVOICE_ITEM_LIST/INVOICE_ITEM/PRODUCT_FEATURES/FEATURE/FVALUE"

Using -v "value" option works perfectly but using -x "xpath" parameter doesn't work. What I'm doing wrong?

<INVOICE_ITEM_LIST>
    <INVOICE_ITEM>
      <PRODUCT_NAME>Product1</PRODUCT_NAME>
      <PRODUCT_FEATURES>
        <FEATURE>
          <FNAME>TotalQuantity</FNAME>
          <FVALUE>6.500</FVALUE>
        </FEATURE>
      </PRODUCT_FEATURES>
      <QUANTITY></QUANTITY>
     </INVOICE_ITEM>
     <INVOICE_ITEM>
      <PRODUCT_NAME>Product2</PRODUCT_NAME>
      <PRODUCT_FEATURES>
        <FEATURE>
          <FNAME>TotalQuantity</FNAME>
          <FVALUE>12</FVALUE>
        </FEATURE>
      </PRODUCT_FEATURES>
      <QUANTITY></QUANTITY>
     </INVOICE_ITEM>
</INVOICE_ITEM_LIST>

Upvotes: 2

Views: 538

Answers (1)

PBI
PBI

Reputation: 335

In this case the -x option needs an xpath relative to the element that you want to update. Moreover, you want the contents of that element, I guess:

xmlstarlet ed -u "//INVOICE_ITEM/QUANTITY" -x "../PRODUCT_FEATURES/FEATURE/FVALUE/text()"

Upvotes: 4

Related Questions