<?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
CodePudding user response:
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.
