清除xdt元素(如果没有子元素)

问题描述:

所以我开始玩nuget,它是web.config install/uninstall.xdt值.

So I'm starting to play with nuget and it's web.config install/uninstall.xdt values.

我的问题是,是否存在xdt:Transform将清除空元素.我什么都没找到. https://msdn.microsoft.com/en-us/library/dd465326%28v = vs.110%29.aspx

My question would be, Is there and xdt:Transform that will clear out empty elements. I didn't find anything here. https://msdn.microsoft.com/en-us/library/dd465326%28v=vs.110%29.aspx

这是我的例子.

我当前的Web.config.install.xdt看起来像这样

My current Web.config.install.xdt looks like this

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation xdt:Transform="InsertIfMissing">
      <assemblies xdt:Transform="InsertIfMissing">
        <add xdt:Transform="InsertIfMissing" xdt:Locator="Match(assembly)" assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

我的卸载看起来像这样

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation>
      <assemblies>
        <add xdt:Transform="Remove" xdt:Locator="Match(assembly)" assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

这是我的web.config之前(简体)

Here's my web.config before (simplified)

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>

这是install.xtd之后的我的web.config

Here's my web.config after the install.xtd

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>

这是卸载后的我的web.config

Here's my web.config after the uninstall

  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5" />
  </system.web>

反正还有摆脱标签的地方吗?

Is there anyway to get rid of the tag?

您似乎可以在一个元素上指定多个转换.因此,您可以从安装中删除内容,然后检查该元素是否具有子元素,如果没有则删除该元素.

It appears you can specify multiple transformations on an element. Therefore you could remove the content from your install and then check if the element has children, if not remove the element.

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.web>
    <compilation>
      <assemblies>
        <add xdt:Transform="Remove" xdt:Locator="Match(assembly)" assembly="MyAssembly, Version=4.5.4.0, Culture=neutral, PublicKeyToken=asdfasdfasdfasdf" />
      </assemblies>
      <assemblies xdt:Locator="Condition(count(*) = 0)" xdt:Transform="Remove"/>
    </compilation>
  </system.web>
</configuration>