.net 通过NANT的Xsl转换过程不会更新所有xpath';要求

.net 通过NANT的Xsl转换过程不会更新所有xpath';要求,.net,msbuild,nant,xslcompiledtransform,.net,Msbuild,Nant,Xslcompiledtransform,这对我来说很难解释,但我会尽力的。对我可能遗漏的任何内容添加澄清/补充意见 背景资料: NANT(生成过程)版本0.85.2478.0,执行msbuild CruiseControl.NET Windows Server 2008 R2 64位 .NET Framework 3.5 我正在尝试更新/修改web应用程序的web.config中的一些设置。我们使用NANT代码运行构建过程,该过程通过CruiseControl.NET执行。我们正在通过XslCompiledTransform并使用

这对我来说很难解释,但我会尽力的。对我可能遗漏的任何内容添加澄清/补充意见

背景资料:

  • NANT(生成过程)版本0.85.2478.0,执行msbuild
  • CruiseControl.NET
  • Windows Server 2008 R2 64位
  • .NET Framework 3.5
我正在尝试更新/修改web应用程序的web.config中的一些设置。我们使用NANT代码运行构建过程,该过程通过CruiseControl.NET执行。我们正在通过XslCompiledTransform并使用样式表和设置文件更新配置文件

下面是“UpdateConfig.xslt”文件的外观:

<?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

      <!-- Set the file output mode -->
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />

      <!-- Get the parameters from the calling application -->
      <xsl:param name="defaultSettings" select="'C:\BuildServer\Configuration\ConfigFiles\Defaults.xml'" />
      <xsl:param name="buildTypeSettings" select="'NoFile'" />
      <xsl:param name="environmentSettings" select="'NoFile'" />
      <xsl:param name="projectSettings" select="'NoFile'" />
      <xsl:param name="mode" select="''"/>

      <!-- Load the various ConfigFileSettings XML files. -->
      <xsl:include href="GetSettings.xslt" />
      <xsl:variable name="fileset">
        <xsl:if test="$defaultSettings != 'NoFile'">
          <xsl:element name="File"><xsl:value-of select="$defaultSettings" /></xsl:element>
        </xsl:if>
        <xsl:if test="$buildTypeSettings != 'NoFile'">
          <xsl:element name="File"><xsl:value-of select="$buildTypeSettings" /></xsl:element>
        </xsl:if>
        <xsl:if test="$environmentSettings != 'NoFile'">
          <xsl:element name="File"><xsl:value-of select="$environmentSettings" /></xsl:element>
        </xsl:if>
        <xsl:if test="$projectSettings != 'NoFile'">
          <xsl:element name="File"><xsl:value-of select="$projectSettings" /></xsl:element>
        </xsl:if>
      </xsl:variable>
      <xsl:variable name="Settings">
        <xsl:call-template name="loadSettings">
          <xsl:with-param name="fileset" select="$fileset" />
        </xsl:call-template>
      </xsl:variable>

      <!-- Define the root node template to distinguish output types. -->
      <xsl:template match="/">
        <xsl:choose>
          <xsl:when test="$mode = 'display'">
            <xsl:call-template name="display" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy>
              <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!-- Define the default copy template. -->
      <xsl:template match="*|node()|comment()|processing-instruction()" priority="-100">
        <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
      </xsl:template>

      <!-- Skip certain comments. -->
      <xsl:template match="comment()[contains(., '=tuc0') and contains(., '&lt;publisher')]" />
      <xsl:template match="comment()[contains(., 'add key=&quot;')]" priority="10">
        <xsl:variable name="key" select="substring-before(substring-after(., 'add key=&quot;'), '&quot;')" />
        <xsl:if test="count(//add[@key=$key]) = 0"><xsl:copy /></xsl:if>
      </xsl:template>

      <!-- Look up attribute settings in the $Settings block for all attributes. -->
      <xsl:template match="@*">
        <xsl:choose>
          <xsl:when test="name() = '@key'"><xsl:copy /></xsl:when>
          <xsl:otherwise>

            <!-- First, build a full xpath to this key as it should appear in settings. -->
            <xsl:variable name="setting">
              <xsl:call-template name="getSetting">
                <xsl:with-param name="node" select="." />
              </xsl:call-template>
            </xsl:variable>

            <!-- Next, get the value from the config file if it's there. -->
            <xsl:variable name="value">
              <xsl:choose>
                <xsl:when test="count(msxsl:node-set($Settings)//setting[@xpath=$setting]) = 1">
                  <xsl:value-of select="msxsl:node-set($Settings)//setting[@xpath=$setting]/@value" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="." />
                </xsl:otherwise>
              </xsl:choose>
            </xsl:variable>

            <!-- Finally, return the attribute with its new value -->
            <xsl:attribute name="{name()}"><xsl:value-of select="$value" /></xsl:attribute>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!-- Handle SQL Connection Strings -->
      <xsl:template match="@*[(contains(translate(., 'ACDEGILNORSTU ', 'acdegilnorstu'), 'initialcatalog=')
                           or contains(translate(., 'ABDEST ', 'abdest'), 'database='))
                          and not(contains(translate(., 'PROVIDE ', 'provide'), 'provider='))]"
                priority="1">

        <!-- Normalize the connection string -->
    <xsl:variable name="lcString" select="translate(., 'ABCDEGILNORSTU ', 'abcdegilnorstu')" />

    <!-- Get the database from the string -->
    <xsl:variable name="dbname">
      <xsl:choose>
        <xsl:when test="contains($lcString, 'initialcatalog=')">
          <xsl:value-of select="substring-before(substring-after($lcString, 'initialcatalog='), ';')" />
        </xsl:when>
        <xsl:when test="contains($lcString, 'database=')">
          <xsl:value-of select="substring-before(substring-after($lcString, 'database='), ';')" />
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="SqlDB" select="concat('Initial Catalog=', $dbname, ';')" />

    <!-- Get SQL Settings from $Settings. -->
    <xsl:variable name="SqlAuth">
      <xsl:choose>
        <xsl:when test="translate(msxsl:node-set($Settings)//SqlAuth, 'TRUE', 'true') = 'true'">
          <xsl:text>Trusted_Connection=YES;</xsl:text>
        </xsl:when>
        <xsl:otherwise><xsl:text>Trusted_Connection=NO;</xsl:text></xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="SqlServer">
      <xsl:choose>
        <xsl:when test="count(msxsl:node-set($Settings)//SqlServer) > 0">
          <xsl:value-of select="concat('Data Source=', msxsl:node-set($Settings)//SqlServer, ';')" />
        </xsl:when>
        <xsl:otherwise>.</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="SqlUser">
      <xsl:if test="$SqlAuth = 'Trusted_Connection=NO;' and string-length(msxsl:node-set($Settings)//SqlUser) > 0">
        <xsl:value-of select="concat('user id=', msxsl:node-set($Settings)//SqlUser, ';')" />
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="SqlPass">
      <xsl:if test="$SqlAuth = 'Trusted_Connection=NO;' and string-length(msxsl:node-set($Settings)//SqlPass) > 0">
        <xsl:value-of select="concat('password=', msxsl:node-set($Settings)//SqlPass, ';')" />
      </xsl:if>
    </xsl:variable>
    <xsl:variable name="SqlApp">
      <xsl:if test="string-length(msxsl:node-set($Settings)//SqlApp) > 0">
        <xsl:value-of select="concat('APP=', msxsl:node-set($Settings)//SqlApp, ';')" />
      </xsl:if>
    </xsl:variable>

        <xsl:attribute name="{name()}"><xsl:value-of select="concat($SqlServer,$SqlDB,$SqlAuth,$SqlUser,$SqlPass,$SqlApp)" /></xsl:attribute>
      </xsl:template>

      <xsl:template name="getSetting">
    <xsl:param name="node" />
    <xsl:param name="xpath" select="name($node)" />
    <xsl:param name="r" select="'1'" />

    <xsl:for-each select="$node"> <!-- Sets the node as current context -->
      <!-- Handle any special formatting for identifying settings -->
      <xsl:variable name="setting">
        <xsl:choose>
          <xsl:when test="name(..) = 'FTPLocation' and ../@name">
            <xsl:value-of select='concat("[@name=&apos;", ../@name, "&apos;]/@", $xpath)' />
          </xsl:when>
          <xsl:when test="name() = 'value' and name(..) = 'add' and ../@key">
            <xsl:value-of select='concat("[@key=&apos;", ../@key, "&apos;]/@", $xpath)' />
          </xsl:when>
          <xsl:when test="not(self::*)">
            <xsl:value-of select="concat('/', '@', $xpath)" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="concat('/', $xpath)" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:choose>
        <xsl:when test="name(..) = ''">
          <xsl:value-of select="$setting" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="getSetting">
            <xsl:with-param name="node" select=".." />
            <xsl:with-param name="xpath" select="concat(name(..), $setting)" />
            <xsl:with-param name="r" select="$r + 1" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>
</xsl:template>


很抱歉,这篇文章太长了,无法阅读-我担心您不会得到任何好的答案。谢谢@skolima,我尝试缩短并进行了一些清理…您是否考虑过在Visual Studio 2010中使用配置转换@BabakNaffas在接下来的几个月内将切换到TFS,因此完全编写当前的构建过程没有任何意义。我只想修复这一点,这样我就不必记得在每个月发布后更新这些配置设置。
<setting xpath="/configuration/General/Settings/add[@key='enableAddrValidationService']/@value" value="true" create="false" dynamic="false" />
<setting xpath="/configuration/General/Settings/add[@key='AddrValidationUrl']/@value" value="bogus" create="false" dynamic="false" />
<setting xpath="/configuration/system.serviceModel/client/endpoint[@name='ValidationServiceSoap']/@address" value="bogus" create="false" dynamic="false" />
<setting xpath="/configuration/applicationSettings/ARTS.Properties.Settings/setting[@name='addrValidationUser']/value" value="bogus" create="false" dynamic="false" />
<setting xpath="/configuration/system.serviceModel/client/endpoint[@name='ValidationServiceSoap']/@address" value="bogus" create="false" dynamic="false" />