Web.Config转换xml注释

时间:2012-04-28 00:58:57

标签: xpath web-config transform xdt-transform

如何使用Web.config转换选择注释或插入注释?

我无处可以找到有关此事的任何信息。

我正在努力做到: 1)在Web.config

中的现有注释之前插入一段xml(<serviceAuthorization impersonateCallerForAllOperations="true"/>

OR

2)在一组兄弟姐妹的末尾插入注释和xml:

据我所知,Web.config转换不支持xPath轴,我尝试了一些尝试在第一个注释之前插入节点:

<serviceAuthorization impersonateCallerForAllOperations="true" xdt:Transform="InsertBefore(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[@name='behaviorOne']/serviceMetadata/preceding::comment()[1])"/>

<serviceAuthorization impersonateCallerForAllOperations="true" xdt:Transform="InsertBefore(/configuration/system.serviceModel/behaviors/serviceBehaviors/behavior[@name='behaviorOne']/child::node()[1]"/>

我尝过其他几个,但你明白了。我在预览转换时遇到错误。

我似乎也无法找到有关如何插入评论的任何信息。我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

我认为转换仅限于对元素或属性进行操作。至少,xdt:Transform上的documentation都没有提及任何可用于添加评论的内容。

至于Locator,似乎有某些支持轴,因为我可以使用以下内容:

<spring >
<objects xmlns="http://www.springframework.net">
  <object >
    <constructor-arg index="0" type="System.Uri" value="https://example.com/test" xdt:Locator="Condition(../@name='requestConfig' and @index='0')" xdt:Transform="SetAttributes(value)"/>
  </object>
</objects>

操作以下内容时:

<spring >
<objects xmlns="http://www.springframework.net">
    <object name="requestConfig" type="Example.Namespace.RequestConfig, Example" singleton ="true">
        <constructor-arg index="0" type="System.Uri" value="https://example.com/production"/>
        <constructor-arg index="1" value="45000"/>
    </object>
</objects>

如您所见,上面使用父轴以匹配要转换的元素。

答案 1 :(得分:0)

参加聚会有点晚,但是对于那些谷歌搜索我发现了一些解决方法-如果您插入节点,它将在节点内插入任何注释,显然您需要先将其删除。即:

  <customErrors xdt:Transform="Remove" />
  <customErrors mode="On" defaultRedirect="Hub/Error" xdt:Transform="Insert">
      <!-- comment -->          
  </customErrors>

不利之处在于它破坏了子节点,但我尝试将注释尽量降低以减轻损害。

相关问题