![Atualizar parâmetro XML, com namespace, usando xmlstarlet](https://rvso.com/image/192237/Atualizar%20par%C3%A2metro%20XML%2C%20com%20namespace%2C%20usando%20xmlstarlet.png)
Eu tenho este arquivo 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>
Estou tentando atualizar o POS_PRINTER_PORT_ID
valor, mas estou com dificuldades para acertar.
Eu tentei os comandos abaixo:
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
Eu também estava tentando, xmllint
mas li que esse xmlstarlet
é o caminho a seguir.
Responder1
Sua última tentativa está quase correta, mas você esqueceu de adicionar o namespace aos nós name
e 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
Ou usando parâmetros importados da linha de comando em vez de valores codificados nas expressões:
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
Com parâmetros:
xq --arg n 'POS_PRINTER_PORT_ID' --arg v 9999 \
-x '( .engineConfiguration.engineParameter[] |
select(.name == $n).value ) |= $v' file.xml