1

I have this XML file:

<?xml version="1.0" encoding="UTF-8" ?>
<engineConfiguration xmlns="http://bla.com/engine/management/engineConfiguration">
    <engineParameter>
        <name>PORT_ID</name>
        <value>47827</value>
    </engineParameter>
    <engineParameter>
        <name>POS_PRINTER_PORT_ID</name>
        <value>27001</value>
    </engineParameter>
    <engineParameter>
        <name>PDS_WS_LOCATION</name>
        <value>http://localhost:8080/pds-jbrain-ws/pdsservice?wsdl</value>
    </engineParameter>
</engineConfiguration>

I'm trying to update the POS_PRINTER_PORT_ID value, but I'm having a hard time getting it right.

I've tried the below commands:

xmlstarlet ed -u '/engineConfiguration/engineParameter/POS_PRINTER_PORT_ID' -v 9999 engineConfiguration.xml
xmlstarlet ed -N s=http://bla.com/engine/management/engineConfiguration -u '/s:engineConfiguration/s:engineParameter[name = "POS_PRINTER_PORT_ID"]/value' -v 999 engineConfiguration.xml

I was also trying with xmllint but read that xmlstarlet is the way to go.

1 Answer 1

2

Your last try is almost correct, but you forgot to add the namespace to the name and value nodes:

xmlstarlet ed \
    -N s=http://bla.com/engine/management/engineConfiguration \
    -u '/s:engineConfiguration/s:engineParameter[s:name = "POS_PRINTER_PORT_ID"]/s:value' \
    -v 9999 file.xml

Or using parameters imported from the command line rather than hard-coded values in the expressions:

xmlstarlet ed \
    -N s='http://bla.com/engine/management/engineConfiguration' \
    --var n "'POS_PRINTER_PORT_ID'" --var v "'9999'" \
    -u '/s:engineConfiguration/s:engineParameter[s:name = $n]/s:value' \
    -x '$v' file.xml

Using xq:

xq -x '( .engineConfiguration.engineParameter[] | 
         select(.name == "POS_PRINTER_PORT_ID").value ) |= 9999' file.xml

With parameters:

xq --arg n 'POS_PRINTER_PORT_ID' --arg v 9999 \
    -x '( .engineConfiguration.engineParameter[] | 
          select(.name == $n).value ) |= $v' file.xml

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .