XMLStarlet updating node with the expression

我只是一个虾纸丫 提交于 2021-02-10 12:52:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!