title: XDT Transform: InsertBefore - Locator Condition is ignored
body: <p>I have a web.config file in which I need to either insert the <code>&lt;configSections /&gt;</code> element or manipulate children of that node if it already exists.<br>
  If it already exists I don't want to insert it again (obviously, as it is only allowed to exist once).</p>
  
  <p>Normally, that would not be a problem, however:</p>
  
  <blockquote>
    <p>If this element is in a configuration file, it must be the first child element of the  element. </p>
  </blockquote>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/aa903350%28v=vs.71%29.aspx">Source: MSDN</a>.</p>
  
  <p>So if I use <code>xdt:Transform="InsertIfMissing"</code> the <code>&lt;configSections /&gt;</code> element will always be inserted after any existing child elements (and there are always some), violating the above restriction of it having to be the first child element of <code>&lt;configuration /&gt;</code></p>
  
  <p>I attempted to make this work in the following way: </p>
  
  <pre class="lang-xml prettyprint-override"><code> &lt;configSections
      xdt:Transform="InsertBefore(/configuration/*[1])"
      xdt:Locator="Condition(not(.))" /&gt;
  </code></pre>
  
  <p>Which works perfect, if the <code>&lt;configSections /&gt;</code> element doesn't already exist. However, the condition I've specified seems to be ignored.  </p>
  
  <p>In fact, I've tried a few conditions like:</p>
  
  <pre><code>Condition(not(/configuration[configSections]))
  Condition(/configuration[configSections] = false())
  Condition(not(/configuration/configSections))
  Condition(/configuration/configSections = false())
  </code></pre>
  
  <p>Finally, out of desperation, I tried: </p>
  
  <pre><code>Condition(true() = false()) 
  </code></pre>
  
  <p>It still inserted the <code>&lt;configSections /&gt;</code> element.</p>
  
  <p>It is important to note that I'm trying to include this in a NuGet package, so I will be unable to use a custom transform (<a href="http://blog.appharbor.com/2012/07/27/custom-web-config-transforms-and-merges">like the one AppHarbor uses</a>).</p>
  
  <p>Is there any other clever way to get my element in the right place only if it doesn't yet exist?</p>
  
  <p>To test this out, use <a href="https://webconfigtransformationtester.apphb.com/">AppHarbors config transform tester</a>. Replace the Web.config with the following:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
      &lt;section name="initialSection" /&gt;
    &lt;/configSections&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>And Web.Debug.config with the following:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"&gt;
  
    &lt;configSections
      xdt:Transform="InsertBefore(/configuration/*[1])"
      xdt:Locator="Condition(true() = false())" /&gt;
  
    &lt;configSections&gt;
      &lt;section name="mySection" xdt:Transform="Insert" /&gt;
    &lt;/configSections&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>The result will show two <code>&lt;configSections /&gt;</code> elements, the one containing "mySection" being the first, as specified in the InsertBefore Transform.
  Why was the Locator Condition not taken into account?</p>