0

I need update specific value in xml in automatic way by bash script. My xml file has a lot of similar line like:

<xml>
   <main>
      <buildElement name="test_one" version="" path="" />
      <buildElement name="test_two" version="" path="" />
   </main>
</xml>

I need find element name "test_one" and edit version. I am trying this, but it's not help:

Expected output:

<xml>
   <main>
      <buildElement name="test_one" version="some_value" path="" />
      <buildElement name="test_two" version="" path="" />
   </main>
</xml>

I am trying get this by xmlstarlet and sed, but is not working f.e:

xmlstarlet edit --update '//xml/main/buildElement/name="test_one"/version' --value 'some_value' myXML.xml
0

2 Answers 2

1

Your xpath syntax is incorrect. You need to use @ to refer to attributes, and to search for a particular element you need a filter expression. You want:

xmlstarlet edit --update \
  '//xml/main/buildElement[@name="test_one"]/@version' \
  -v some_value myXML.xml

Which will output:

<?xml version="1.0"?>
<xml>
  <main>
    <buildElement name="test_one" version="some_value" path=""/>
    <buildElement name="test_two" version="" path=""/>
  </main>
</xml>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I use this but... I see changes in console print, but no in the file. Why it's not store in a file?
ok, I solved my problem by change options - xmlstarlet ed --inplace -u
0
'//xml/main/buildElement/name="test_one"/version'

You want

'//xml/main/buildElement[@name="test_one"]/@version'

(Basically, you seem to be guessing, and that's not going to get you very far with XPath. Do some reading.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.