2011-04-05 49 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

相關問題