Issue
XML to edit -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<broker xmlns="http://activemq.apache.org/schema">
<jaas-security domain="activemq"/>
<server configuration="file:/Users/gugu/Docs/artemis/var/lib/broker/etc//broker.xml"/>
<web path="web" rootRedirectLocation="console">
<binding uri="https://localhost:8161">
<app url="activemq-branding" war="activemq-branding.war"/>
<app url="artemis-plugin" war="artemis-plugin.war"/>
<app url="console" war="console.war"/>
</binding>
</web>
</broker>
In the above XML, I want to add the below attribute uri="https://localhost:8161"
.
keyStorePath="$path/server-keystore.jks" keyStorePassword="password"
After adding these, need to change http
to https
and also might need to change the port.
Result -
<binding uri="https://localhost:8443" keyStorePath="$path/server-keystore.jks" keyStorePassword="securepass">
The path and password used above for the keystone.jks
should come from two different variables.
Can someone help me how to edit it in place using either XMLstarlet or sed command?
What I tried - To add the attribute, (first I tried without variable)
xmlstarlet edit --inplace \
--insert "/broker/web/binding[not(@keyStorePath)]" --type attr -n keyStorePath --value ".ssl/server-keystore.jks" \
bootstrap.xml
There are no changes in the file
Solution
Your XML file uses namespaces (xmlns=
). This adds two attributes and updates one attribute:
xmlstarlet edit --inplace \
-N x='http://activemq.apache.org/schema' \
--insert '//x:broker/x:web/x:binding' --type attr -n 'keyStorePath' --value '$path/server-keystore.jks' \
--insert '//x:broker/x:web/x:binding' --type attr -n 'keyStorePassword' --value 'password' \
--update '//x:broker/x:web/x:binding/@uri' --value 'https://localhost:8443' \
file.xml
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<broker xmlns="http://activemq.apache.org/schema">
<jaas-security domain="activemq"/>
<server configuration="file:/Users/gugu/Docs/artemis/var/lib/broker/etc//broker.xml"/>
<web path="web" rootRedirectLocation="console">
<binding uri="https://localhost:8443" keyStorePath="$path/server-keystore.jks" keyStorePassword="password">
<app url="activemq-branding" war="activemq-branding.war"/>
<app url="artemis-plugin" war="artemis-plugin.war"/>
<app url="console" war="console.war"/>
</binding>
</web>
</broker>
Information about the syntax: xmlstarlet edit
Answered By - Cyrus Answer Checked By - Dawn Plyler (WPSolving Volunteer)