如何将 VS2010 web.config 转换应用于具有命名空间属性的元素? [英] How to apply VS2010 web.config transformation to an element with a namespace attribute?

查看:24
本文介绍了如何将 VS2010 web.config 转换应用于具有命名空间属性的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的 VS2010 web.config 转换功能来更改我的 web.config 文件中 nhibernate 配置中的连接字符串.相关的片段是这样的:

I'd like to use the new VS2010 web.config transformation feature to change the connection string within the nhibernate configuration in my web.config file. The relevant snippet is something like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>

  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
      <property name="connection.connection_string">(test connection string)</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
...

我尝试了以下转换但没有成功:

I've tried the following transformation without success:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
        <session-factory>
            <property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</property>
        </session-factory>
    </hibernate-configuration>
</configuration>

问题似乎出在 nhibernate-configuration 元素的 xmlns 属性中.

The problem seems to be in the xmlns attribute of the nhibernate-configuration element.

在部署期间将(测试连接字符串)替换为(生产连接字符串)的正确转换应该是什么?

What should be the correct transformation to replace (test connection string) with (production connection string) during deployment?

推荐答案

答案可能有点晚,但由于我也需要这个,我想我会发布一个对我有用的答案,以防其他人绊倒关于这个问题.

The answer may be a bit late, but since I needed this as well, I figured I would post an answer that worked for me in the event anyone else stumbles upon this question.

您需要将 xdt:Locator 与 xpath 表达式结合使用才能获得正确的节点.所以这样的事情应该可以工作.

You need to use the xdt:Locator in combination with an xpath expression to get the correct node. So something like this should work.

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   <session-factory>
      <property name="connection.connection_string" xdt:Locator="XPath(//*[local-name()='hibernate-configuration']//*[local-name()='property'][@name='connection.connection_string'])" xdt:Transform="Replace">(production connection string)</property>
   </session-factory>
</hibernate-configuration>

可能有更好的 xpath 表达式,但这对我有用.

There may be a better xpath expression, but this is what worked for me.

唯一的问题,不是什么大问题,是被替换的节点将在节点上重新声明一个命名空间.所以被替换的节点在最终输出中实际上看起来像这样.

The only issue, which isn't that big a deal, is the replaced node will have a namespace redeclared on the node. So the replaced node will actually look like this in the final output.

<property name="connection.connection_string" xmlns="urn:nhibernate-configuration-2.2">(production connection string)</property>

这篇关于如何将 VS2010 web.config 转换应用于具有命名空间属性的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆