问题
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>
回答1:
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()"
来源:https://stackoverflow.com/questions/35177847/xmlstarlet-updating-node-with-the-expression