0

I'm new using xmlstarlet, as i got to no point using bash.

I try to replace many values in an XML file at once.

What i tried was:

xmlstarlet ed -L -u "/items/item/property[@name='Stacknumber']/@value" -v '30000' items.xml

<items>    
   <item name="foodEggBoiled">
      <property name="Stacknumber" value="50"/> <!-- This i want to change-->
    </item>
    <item name="foodEggRaw">
      <property name="Stacknumber" value="1"/> <!-- and this not-->
    </item>
</items>

The value i want to change is

<property name="Stacknumber" value="50"/>

I want to change every value from the property Stacknumber with a value above 1 to 30000 like

<property name="Stacknumber" value="30000"/>

But I don't know how to select only properties with values above 1.

4
  • 1
    Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself. Commented Dec 1, 2018 at 15:08
  • Please show the relevant code. Also see How to create a Minimal, Complete, and Verifiable example. Commented Dec 1, 2018 at 15:22
  • Your root in the XML shown (as of revision 3 is item, where the query looks for items. It would also be helpful to try to build a proper minimal reproducible example -- note the Minimal part of that specification, meaning anything unrelated to the problem shouldn't be shown (and the Complete / Verifiable specs, meaning that the code shown should be complete enough someone can see the problem -- maybe you should include two different "Stacknumber"s, one that should be changed and one that shouldn't, to be complete enough that answers can be tested). Commented Dec 1, 2018 at 15:41
  • Sorry for all the mess - i'm completely new to SO. Commented Dec 1, 2018 at 15:51

1 Answer 1

1

With valid XML:

xmlstarlet edit --omit-decl --update '//property[@name="Stacknumber"][@value>"1"]/@value' -v '30000' file.xml

If you want to edit file inplace, add option -L.

Output:

<items>
  <item name="foodEggBoiled">
    <property name="Stacknumber" value="30000"/>
    <!-- This i want to change-->
  </item>
  <item name="foodEggRaw">
    <property name="Stacknumber" value="1"/>
    <!-- and this not-->
  </item>
</items>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.