web.config转换XML元素

5
我将放弃使用web.config配置批处理文件(Hanselman的),并希望使用vs2010中的配置转换功能。但是,我有点困惑如何转换xml元素(而不是元素上的属性)。
这是我的web.config中的一段代码片段:
<Federation type="..." xmlns="...">
      <SigningCertificate .../>
      <AllowedAudienceUris>
               <Audience>https://audience.url.com</Audience>
      </AllowedAudienceUris>
</Federation>

我希望根据构建配置,通过插入不同的URL来转换元素 - 这个可以实现吗?
提前感谢!
/Jasper
3个回答

2
如果AllowedAudienceUris和Audience元素只出现一次,则省略xdt:Locator也可以:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <Federation>
    <AllowedAudienceUris xdt:Transform="Replace">
      <Audience>https://example.com</Audience>
    </AllowedAudienceUris>
  </Federation>
</configuration>

1

你应该能够使用 xdt:Locatorxdt:Transform 属性来完成这个任务。

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <Federation>
    <AllowedAudienceUris
        xdt:Transform="Replace"
        xdt:Locator="Condition(//Audience)">
      <Audience>https://example.com</Audience>
    </AllowedAudienceUris>
  </Federation>
</configuration>

看起来这应该可行,但据我所知它不行。 - Jeremy Holovacs

-1

一个方法是以下:

<!-- Copy all nodes -->
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

<!-- Operate just on the AllowedAudienceUris (copy it), setting the Audience element -->
<xsl:template match="/Federation/AllowedAudienceUris">
    <xsl:copy>
        <Audience>https://hello.com</Audience>
    </xsl:copy>
</xsl:template>

@jaspernygaard 这个解决方案假设根元素是/Federation。你需要根据实际的XML结构进行调整。你可以随时发布更完整的问题描述。 - Scott Saad
2
@jaspernygaard,我不明白...问题是关于基于XDT的Web Config Transform,而不是XSLT。此答案提供的代码片段显然是XSLT。这怎么成为被接受的答案了? - Saul Dolgin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接