Xpath is only a query language for XML documents - it alone cannot modify an XML document or create a new XML document .
A language specifically designed to transform an XML document is called XSLT.
Here is a very short and simple XSLT transformation that solves your problem :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="add[@name='bleh']"> <xsl:copy> <xsl:copy-of select="@*"/> <option name="foo" value="bar"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
when this conversion is applied to the provided XML document :
<configuration> <plugins> <add name="bleh"/> </plugins> </configuration>
required, the correct result is obtained :
<configuration> <plugins> <add name="bleh"> <option name="foo" value="bar"/> </add> </plugins> </configuration>
Dimitre novatchev
source share