Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created November 7, 2019 20:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mttjohnson/aca9132fb1582d3891564505a74acf09 to your computer and use it in GitHub Desktop.
Save mttjohnson/aca9132fb1582d3891564505a74acf09 to your computer and use it in GitHub Desktop.
Scripted XML Editing using XSLT and Xpath Expressions (xmllint xsltproc)
# Requirements
# yum install libxml2 libxslt
set +H # disable history expansion
XML_CONTENT=$(cat <<'XML_CONTENT_HD'
<?xml version="1.0" encoding="UTF-8"?>
<confluence-configuration>
<setupStep>complete</setupStep>
<setupType>custom</setupType>
<buildNumber>8100</buildNumber>
<properties>
<property name="admin.ui.allow.daily.backup.custom.location">true</property>
<property name="admin.ui.allow.manual.backup.download">false</property>
<property name="admin.ui.allow.site.support.email">false</property>
<property name="atlassian.license.message">xxxxxxxxxxxxx</property>
<property name="attachments.dir">/shared/jiraconffiles/confluence/attachments</property>
<property name="confluence.setup.server.id">xxxxxxxxxxxxx</property>
<property name="confluence.webapp.context.path"></property>
<property name="hibernate.c3p0.acquire_increment">1</property>
<property name="hibernate.c3p0.idle_test_period">100</property>
<property name="hibernate.c3p0.max_size">60</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.min_size">20</property>
<property name="hibernate.c3p0.preferredTestQuery">select 1</property>
<property name="hibernate.c3p0.timeout">30</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.isolation">2</property>
<property name="hibernate.connection.password">xxxxxxxxxxxxx</property>
<property name="hibernate.connection.url">jdbc:mysql://xxxxxxxxxxxxx:3306/confluencedb</property>
<property name="hibernate.connection.username">confluence</property>
<property name="hibernate.database.lower_non_ascii_supported">true</property>
<property name="hibernate.dialect">com.atlassian.confluence.impl.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.setup">true</property>
<property name="jwt.private.key">xxxxxxxxxxxxx</property>
<property name="jwt.public.key">xxxxxxxxxxxxx</property>
<property name="lucene.index.dir">${localHome}/index</property>
<property name="synchrony.encryption.disabled">true</property>
<property name="synchrony.proxy.enabled">true</property>
<property name="webwork.multipart.saveDir">${localHome}/temp</property>
</properties>
</confluence-configuration>
XML_CONTENT_HD
)
MATCH_NODE="/confluence-configuration/properties/property[@name='attachments.dir']/text()"
REPLACEMENT_VALUE="/ASDFASDF/jiraconffiles/confluence/QWERQWERQWRE"
XSLT_CONTENT=$(cat <<XSLT_CONTENT_HD
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="no"/>
<!-- copy all contents from the existing document -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- modify the text of a specifically matched node -->
<xsl:template match="${MATCH_NODE}">
<xsl:value-of select="'${REPLACEMENT_VALUE}'"/>
</xsl:template>
</xsl:stylesheet>
XSLT_CONTENT_HD
)
set -H # re-enable history expansion
echo "${XML_CONTENT}" | xmllint --xpath "${MATCH_NODE}" -
XML_CONTENT=$(echo "${XML_CONTENT}" | xsltproc <(echo "${XSLT_CONTENT}") -)
echo "${XML_CONTENT}" | xmllint --xpath "${MATCH_NODE}" -
echo "${XML_CONTENT}"
echo "${XSLT_CONTENT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment