Howto: Debugging the XML structure and replacing values for a node
As Google might lead someone else here who has to alter his own XML file with a different structure and nodes, it could be arduous finding the proper syntax in order to change values.
Sidenode: eventough xmlstarlet might be the better tool for replacing values in XML files, most Unices have xmllint pre-installed.
Thus, the following practice shows how to navigate with xmllint in interactive mode (i. e. debug) through any xml file in order to find the Node (i.e syntax) which should be replaced.
xmllint --shell file.xml # starts xmllint in interactive mode
setrootns # see LMCs explanation
cat # shows the complete XML structure
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:aut="http://">
<soapenv:Header>
<aut:Session>
<aut:IPAddress>127.0.0.1</aut:IPAddress>
<aut:SessionToken>true</aut:SessionToken>
<aut:ApplicationToken>861</aut:ApplicationToken>
</aut:Session>
</soapenv:Header>
<soapenv:Body></soapenv:Body>
</soapenv:Envelope>
Now you can walk step by step to the desired node through the XML tree :
cd //soapenv:Envelope # change to the first level
soapenv:Envelope > # the prompt changes on success
cd //soapenv:Envelope/soapenv:Header/aut:Session
aut:Session > cat # appropriate prompt change
<aut:Session>
<aut:IPAddress>127.0.0.1</aut:IPAddress>
<aut:SessionToken>true</aut:SessionToken>
<aut:ApplicationToken>861</aut:ApplicationToken>
</aut:Session>
Show directly the structure and values for a certain node/path (without prior cd):
cat //soapenv:Envelope/soapenv:Header/aut:Session
<aut:Session>
<aut:IPAddress>127.0.0.1</aut:IPAddress>
<aut:SessionToken>true</aut:SessionToken>
<aut:ApplicationToken>861</aut:ApplicationToken>
</aut:Session>
Bear in mind to not have a trailing slash at the end of the path as it won't find the node:
cat //soapenv:Envelope/soapenv:Header/aut:Session/ # trailing slash throws an error
XPath error : Invalid expression
//soapenv:Envelope/soapenv:Header/aut:Session/
^
//soapenv:Envelope/soapenv:Header/aut:Session: no such node
Assuming we would like to change the IP address, its a good idea to check first the proper path:
cat //soapenv:Envelope/soapenv:Header/aut:Session/aut:IPAddress
<aut:IPAddress>127.0.0.1</aut:IPAddress>
Or just get the value of the node:
cat //soapenv:Envelope/soapenv:Header/aut:Session/aut:IPAddress/text()
127.0.0.1
Save first the original file as backup.xml, then apply changes to file.xml, save it and leave xmllint:
cd //soapenv:Envelope/soapenv:Header/aut:Session/aut:IPAddress
aut:IPAddress > cat text() # alternative way to check the value
127.0.0.1
aut:IPAddress > save backup.xml # create backup file
aut:IPAddress > set 1.1.1.1 # change the value
aut:IPAddress > cat text() # crosscheck the changed value
1.1.1.1
aut:IPAddress > save # save changes to file.xml
aut:IPAddress > quit
In interactive mode help will show further details about commands. The section #Shell at xmllint might also be a good resource.
As soon as the proper path for the node (which should be changed) has been identified, you can refer to the more efficient one-liner as mentioned by @LMC to change XML files on the fly.