![Actualizar parámetro XML, con espacio de nombres, usando xmlstarlet](https://rvso.com/image/192237/Actualizar%20par%C3%A1metro%20XML%2C%20con%20espacio%20de%20nombres%2C%20usando%20xmlstarlet.png)
Tengo este archivo XML:
<?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>
Estoy intentando actualizar el POS_PRINTER_PORT_ID
valor, pero me cuesta hacerlo bien.
Probé los siguientes comandos:
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
También lo estaba intentando, xmllint
pero leí que ese xmlstarlet
es el camino a seguir.
Respuesta1
Su último intento es casi correcto, pero olvidó agregar el espacio de nombres a los nodos name
y :value
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
O usar parámetros importados desde la línea de comando en lugar de valores codificados en las expresiones:
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
Usandoxq
:
xq -x '( .engineConfiguration.engineParameter[] |
select(.name == "POS_PRINTER_PORT_ID").value ) |= 9999' file.xml
Con parámetros:
xq --arg n 'POS_PRINTER_PORT_ID' --arg v 9999 \
-x '( .engineConfiguration.engineParameter[] |
select(.name == $n).value ) |= $v' file.xml