web.config transform xml element
I'm getting rid of web.config configuration batch file (Hanselman's) and want to use the config transformation feature in vs2010. However I'm having a bit of trouble figuring out of to transform an xml element (as opposed to an attribu开发者_开发知识库te on an element).
This is a snippet from my web.config:
<Federation type="..." xmlns="...">
<SigningCertificate .../>
<AllowedAudienceUris>
<Audience>https://audience.url.com</Audience>
</AllowedAudienceUris>
</Federation>
I want to transform the element by inserting a different url based on the build configuration - can this be done?
Thanks in advance!
/Jasper
If the AllowedAudienceUris and Audience elements occur only once, omitting the xdt:Locator
is also fine:
<?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>
You should be able to do this using the xdt:Locator
and xdt:Transform
attributes.
<?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>
One approach would be the following:
<!-- 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>
精彩评论