2011-04-05 15 views
1

我有2个.config文件必须配置。一个是web.config,另一个是app.config,这两个文件都来自我们代码运行的第三方供应商。所以我们需要对它进行调整,以便看到我们的代码。从安装程序更新第三方的.config转换

我的计划是使用xslt获取我们的.config文件并将其合并到第三方文件中。

我已经看到了一些关于如何用msbuild来做这种事情的例子,但是由于我们现场正在做,我们将不得不使用安装程序来做到这一点。任何帮助,将不胜感激。

例子: 我们开始接触:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <runtime> 
    <gcServer enabled="true"/> 
    </runtime> 
</configuration> 

自定义栏目

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="productName" type="company.productName, company, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" /> 
    </configSections> 
    <productName defaultProvider="Provider1"> 
    <providers> 
     <clear /> 
     <add name="Provider1" type="Company.Product.Authentication.Provider1, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="localhost:5555" /> 
     <add name="Provider2" type="Company.Product.Authentication.Provider2, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="demo.example.com" /> 
    </providers> 
    </productName> 
</configuration> 

,结束时用:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="productName" type="company.productName, company, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" /> 
    </configSections> 
    <productName defaultProvider="Provider1"> 
    <providers> 
     <clear /> 
     <add name="Provider1" type="Company.Product.Authentication.Provider1, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="localhost:5555" /> 
     <add name="Provider2" type="Company.Product.Authentication.Provider2, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="demo.example.com" /> 
    </providers> 
    </productName> 
    <runtime> 
    <gcServer enabled="true"/> 
    </runtime> 
</configuration> 
+1

转换只是为了将一个'配置'的子项复制到另一个中吗? – 2011-04-05 15:48:34

+0

@Alejandro:这是正确的,检查它是否存在,如果没有添加它。如果是这样,就让它独自一人。 – 2011-04-05 18:19:32

+0

这不是我写的。你需要弄清楚“检查它是否存在”和“放弃它”的含义。 – 2011-04-05 18:23:33

回答

1

这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:apply-templates select="document('test.xml')/*"> 
      <xsl:with-param name="pContext" select="*"/> 
     </xsl:apply-templates> 
    </xsl:template> 
    <xsl:template match="*[*]"> 
     <xsl:param name="pContext" select="/.."/> 
     <xsl:variable name="vCurrent" select="."/> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:copy-of select="$pContext/@*"/> 
      <xsl:for-each select="*"> 
       <xsl:apply-templates select="."> 
        <xsl:with-param name="pContext" 
         select="$pContext/*[name()=name(current())]"/> 
       </xsl:apply-templates> 
      </xsl:for-each> 
      <xsl:for-each select="$pContext/*"> 
       <xsl:apply-templates 
        select="(.)[not($vCurrent/*[name()=name(current())])]"/> 
      </xsl:for-each> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="*[not(*)]"> 
     <xsl:param name="pContext" select="/.."/> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:copy-of select="$pContext/@*"/> 
      <xsl:apply-templates 
       select="node()[not($pContext)]|$pContext/node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

有了这个输入:

<configuration> 
    <runtime> 
     <gcServer enabled="true"/> 
    </runtime> 
</configuration> 

test.xml

<configuration> 
    <configSections> 
     <section name="productName" type="company.productName, company, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" /> 
    </configSections> 
    <productName defaultProvider="Provider1"> 
     <providers> 
      <clear /> 
      <add name="Provider1" type="Company.Product.Authentication.Provider1, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="localhost:5555" /> 
      <add name="Provider2" type="Company.Product.Authentication.Provider2, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="demo.example.com" /> 
     </providers> 
    </productName> 
</configuration> 

输出:

<configuration> 
    <configSections> 
     <section name="productName" type="company.productName, company, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c"></section> 
    </configSections> 
    <productName defaultProvider="Provider1"> 
     <providers> 
      <clear></clear> 
      <add name="Provider1" type="Company.Product.Authentication.Provider1, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="localhost:5555"></add> 
      <add name="Provider2" type="Company.Product.Authentication.Provider2, Company.Product, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d9072a6c5128d57c" hostName="demo.example.com"></add> 
     </providers> 
    </productName> 
    <runtime> 
     <gcServer enabled="true"></gcServer> 
    </runtime> 
</configuration> 

注意:三个规则。文档根规则:将遍历树更改为需要更新的源,并将输入源保存为$pContext。具有元素子元素的元素规则:使用属性复制自身,使用$pContext的属性更新属性(由处理器完成,因为creating attributes rules),将模板应用于具有新子元素$pContext的子元素(具有相同名称的旧$pContext的子元素)到$pContext的孩子的模板不匹配任何儿童的名字。没有元素子元素的元素规则:使用$pContext属性更新属性,如果$pContext中有一个节点复制它,从而替换元素内容(或者如果您在$pContext中有空元素,则甚至剥离)。

+0

现在就试试看,谢谢你的帮助! – 2011-04-05 22:08:21

+0

工作得很好,如果你能解释一下它是如何工作的,那就太好了! – 2011-04-05 22:24:51

+0

@Matt Heffernan:不客气!我会回来添加一个解释。 – 2011-04-05 22:56:38