1
        <?xml version="1.0"?>
<Student xmlns="http://www.tibco.com/schemas/TrainingPOCs/SharedRec/Schemas/Schema.xsd">
  <Phno type="kwh" value="70" name=""/>
  <Phno type="wh" value="100" name="80">
    <Type>D</Type>
    <Mobile>7777777777</Mobile>
    <TPhone>6666666666</TPhone>
  </Phno>
  <Phno type="kwh" value="200" name="">
    <Type>E</Type>
    <Mobile>5555555555</Mobile>
    <TPhone>4444444444</TPhone>
  </Phno
</Student>

My input file need to update name attribute with value attribute value wherever type attribute has kWh Scripts i worked on

xmlstarlet edit -N w="http://www.tibco.com/schemas/TrainingPOCs/SharedRec/Schemas/Schema.xsd" -u "/w:Student/w:Phno[@type='kwh']/@name" -x "/w:Student/w:Phno[@type='kwh']/@value" sample.xml

1 Answer 1

1

You're almost there but use a relative XPath expression for the -x / --expr option:

xmlstarlet edit \
    -N w='http://www.tibco.com/schemas/TrainingPOCs/SharedRec/Schemas/Schema.xsd' \
    -u '/w:Student/w:Phno[@type="kwh"]/@name' \
    -x 'string(../@value)' file.xml

Alternatively, using the the default namespace shortcut,

xmlstarlet ed -u '/_:Student/_:Phno[@type="kwh"]/@name' -x 'string(../@value)' file.xml

and perhaps adding an -L / --inplace option (see xmlstarlet.txt) for in-place editing.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much @urcodebetterznow it's working well now.

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.