2011-03-02 83 views
5

我摆脱了web.config配置批处理文件(Hanselman's),并希望在vs2010中使用配置转换功能。不过,我在转换xml元素(而不是元素上的属性)方面遇到了一些麻烦。web.config转换xml元素

这是从我的web.config中的一个片段:

<Federation type="..." xmlns="..."> 
     <SigningCertificate .../> 
     <AllowedAudienceUris> 
       <Audience>https://audience.url.com</Audience> 
     </AllowedAudienceUris> 
</Federation> 

我想通过插入基础上,构建配置不同的URL转换元素 - 可以这样做?

在此先感谢!

/贾斯珀

回答

-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> 
+0

我试了一下,但没有奏效。 – jaspernygaard 2011-03-03 08:45:57

+0

@jaspernygaard解决方案假定根元素是/ Federation。您需要将其调整为真正的xml结构。您始终可以发布更完整的问题图片。 – 2011-03-03 14:13:27

+2

@jaspernygaard我不明白...问题是询问基于XDT的Web配置变换,而不是XSLT。这个答案中提供的代码片段显然是XSLT。这是如何被接受的答案? – 2012-04-01 00:37:00

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> 
+0

看来这应该起作用,但它并没有,据我所知。 – 2012-11-30 16:21:51

2

如果AllowedAudienceUris和观众的元素只出现一次,省略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>