Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I insert elements into web.config with Web Deploy?

Is it possible to insert an XML element into my web.config using Web Deploy's Parameters.xml system?

The XmlFile parameter "kind" appears to be the closest to what I need, but its match attribute only accepts an XPath query, and I don't appear to be able to specify a non-existent element in my XPath query. (Or rather, I can specify a non-existent element - Web Deploy just ignores it.) Specifically, I would like to transform this:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

into this:

<configuration>
   <plugins>
      <add name="bleh">
        <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>

(Unfortunately I can't pre-stock the web.config with an empty option element because this particular plugin system doesn't like unrecognized/empty options.)

Thanks for any ideas!

like image 460
ladenedge Avatar asked Apr 19 '11 16:04

ladenedge


3 Answers

Such things are now possible starting with the Web Deploy V3. See the official documentation.

Here is one example of a parameters.xml file which will add newNode to all nodes including the root in target xml file:

<parameters>
  <parameter name="Additive" description="Add a node" defaultValue="&lt;newNode />" tags="">
    <parameterEntry kind="XmlFile" scope=".*" match="//*" />
  </parameter>
</parameters>
like image 162
Vertigo Avatar answered Oct 20 '22 05:10

Vertigo


Xpath is only a query language for XML documents -- it cannot by itself change an XML document or create a new XML document.

The language especially designed for transforming 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 transformation is applied on the provided XML document:

<configuration>
    <plugins>
        <add name="bleh"/>
    </plugins>
</configuration>

the wanted, correct result is produced:

<configuration>
   <plugins>
      <add name="bleh">
         <option name="foo" value="bar"/>
      </add>
   </plugins>
</configuration>
like image 41
Dimitre Novatchev Avatar answered Oct 20 '22 06:10

Dimitre Novatchev


Have you considered using configSource? This allows you to split up your configuration files into multiple smaller files. The web.config would go from:

<configuration>
   <plugins>
      <add name="bleh"/>
   </plugins>
</configuration>

To this with configSource:

<configuration>
   <plugins configSource="plugins.config"/>
</configuration>

The downside is You won't get a nice UI for editing config values during deployment. It is something to consider if you cannot get parameterization to work. If the install UI is what you are after then you could code an editing tool for your admins that can create and modify the plugin.xml files..

like image 1
ttg Avatar answered Oct 20 '22 05:10

ttg