title: Web Config Transforms: Insert If Not Exists
body: <p>I would like to apply a transformation if and only if a matched element <em>does not</em> exist in the target. Trying various xpath expressions using <a href="http://webconfigtransformationtester.apphb.com/">http://webconfigtransformationtester.apphb.com/</a> but no luck so far.</p>
  
  <p>E.g. if the target web.config looks like this:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" /&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>then the output should look like this:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="MyCs" provider="System.Data.SqlClient" connectionString="" /&gt;
      &lt;add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" /&gt;
    &lt;/connectionStrings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" /&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>But if the target looks like this:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" /&gt;
    &lt;/connectionStrings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" /&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>then the result of the transformation should look like this:</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="MyCs" provider="System.Data.IChangedIt" connectionString="my connection string here" /&gt;
      &lt;add name="SomeOtherCs" provider="System.Data.SqlClient" connectionString="" /&gt;   
    &lt;/connectionStrings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" /&gt;
    &lt;/system.web&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>In other words, I just want to add the named connection string to configuration but let the administrator fill it in with his own values. I thought it would as simple as <code>xdt:Transform="Insert" xdt:Locator="XPath(count(/configuration/connectionStrings)=0)"</code> (to add a cs config section if none existed) but apparently not.</p>