I have this original.xml file tree:
<root>
<element1>
<name>Hi</name>
</element1>
<element2>
<name>Beer</name>
</element2>
<element2>
<name>Balloon</name>
<element2_1>
<name>John</name>
</element2_1>
</element2>
</root>
I am trying to add element2_2 under element2 parent. I have tried to make an insert based on “xsl:element match” as following:
cat <<EOT > insert.xsl
<?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" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root/element2[@name='Balloon']">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
<xsl:element name="element2_2">
<xsl:attribute name="name">Edward</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
EOT
And then use the xsltproc to insert it the insert.xsl into original.xml based on a specific location(tag and element) on Linux:
xsltproc insert.xsl original.xml > ./updated.xml
Unfortunately nothing happens. The only thing I found was working was changing the match pattern to root (“/*”) and redact the xsl:element to actual .xml element, which resulted in concatenating the element after root(not something useful).