Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update the xml file using xmlstarlet

Tags:

I am using windows version of xmlstarlet to update an xml file, via windows batch file.

xml edit --update "/xml/table/rec[@id=3]/@id" --value 10 %xmlfile%

I expected this to update the id attribute of rec node to 10. When I run this I see the updated xml as expected in the command line, but the file is never updated.

How can I do it, I want to stay away rewriting the whole file as the file could be big one.

before update:

<?xml version="1.0"?>
<xml>
  <table>
    <rec id="1" />
    <rec id="2" />
    <rec id="3" />
  </table>
</xml>

after update:

<?xml version="1.0"?>
<xml>
  <table>
    <rec id="1" />
    <rec id="2" />
    <rec id="10" />
  </table>
</xml>
like image 428
Schu Avatar asked Mar 25 '15 20:03

Schu


People also ask

Can you update an XML file?

To update data in an XML column, use the SQL UPDATE statement. Include a WHERE clause when you want to update specific rows. The entire column value will be replaced. The input to the XML column must be a well-formed XML document.

What is XMLStarlet in Linux?

XMLStarlet is a set of command line utilities (toolkit) to query, transform, validate, and edit XML documents and files using a simple set of shell commands in a way similar to how it is done with UNIX grep, sed, awk, diff, patch, join, etc commands. XMLStarlet.

How do I know if XMLStarlet is installed?

So, open a terminal (or other command line interface) and execute xpath . Since most terminals (or other command line interfaces) have auto-completion you could also simply type xpa and press tab . If it autocompletes to xpath , then you know it is installed.


1 Answers

You did not show your input document, but I assume it is the following, taken from the xmlstarlet documentation:

<xml>
  <table>
    <rec id="1">
      <numField>123</numField>
      <stringField>String Value</stringField>
    </rec>
    <rec id="2">
      <numField>346</numField>
      <stringField>Text Value</stringField>
    </rec>
    <rec id="3">
      <numField>-23</numField>
      <stringField>stringValue</stringField>
    </rec>
  </table>
</xml>

xmlstarlet modifies the file, but the result is sent to standard output, not saved in the original file. Use another option --inplace to modify the file in place:

$ xml ed --inplace -u "/xml/table/rec[@id='3']/@id" -v 5 rec.xml

Then:

$ cat rec.xml
<?xml version="1.0"?>
<xml>
  <table>
    <rec id="1">
      <numField>123</numField>
      <stringField>String Value</stringField>
    </rec>
    <rec id="2">
      <numField>346</numField>
      <stringField>Text Value</stringField>
    </rec>
    <rec id="5">
      <numField>-23</numField>
      <stringField>stringValue</stringField>
    </rec>
  </table>
</xml>

By the way, this question seems to ask something very similar to this question.


EDIT: As suggested by @npostavs, this option is listed in the edit help:

$ xml edit --help
...
-L (or --inplace)   - edit file inplace
...
like image 60
Mathias Müller Avatar answered Sep 20 '22 18:09

Mathias Müller