I have a server running on Linux system, and I want to edit an XML file of Imagemagick.
The file content is:
<policymap>
<policy domain="..." rights=".." pattern="...." />
<policy domain="..." rights=".." pattern="...." />
..
..
</policymap>
So, I want to add this line:
<policy domain="coder" rights="read | write" pattern="PDF" />
Please how can I do it.
Thank you.
You can do this automatically with an XSLT-1.0 processor and the identity template:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" />
<!-- Identity template - in XSLT-3.0 it can be replaced by
<xsl:mode on-no-match="shallow-copy"/>
-->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="/policymap">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
<!-- Added new line -->
<policy domain="coder" rights="read | write" pattern="PDF" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Its output is:
<?xml version="1.0"?>
<policymap>
<policy domain="..." rights=".." pattern="...."/>
<policy domain="..." rights=".." pattern="...."/>
..
..
<policy domain="coder" rights="read | write" pattern="PDF"/>
</policymap>
The command on *Ubuntu could be
xsltproc transform.xslt source.xml
or using Saxon:
java -jar saxon9he.jar -xsl:b.xslt b.xml