Hello...I am transforming an XML where I am supposed to locate a particular Element (based on the attribute value) and update the Element and its child attributes.
The sample XML file is as below.
<?xml version="1.0" encoding="utf-8"?>
<Configuration>
<Environments>
<Environment id="Master"/>
<Environment id="Developer"/>
</Environments>
<Common>
<Logging>
<LogFile>log\updater.log</LogFile>
</Logging>
</Common>
<Configuration>
My XSLT file is as below.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:param name="EnvironmentId" />
<xsl:param name="SelectEnvironment" />
<!-- Copy All Elements -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- Modify Element with id = Developer-->
<xsl:template match="Environment/@id[. ='Developer']">
<xsl:attribute name="id">
<xsl:value-of select="$EnvironmentId"/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
In this XSLT, variable EnvironmentId contains the new id; variable SelectEnvironment should contain the value Developer (or any other user provided value passed via C#.NET)
Question
How do I write my XSLT so that the match works based on a user-defined value?
I tried the following
<xsl:template match="Environment/@id[. ='$SelectEnvironment']">
<xsl:attribute name="id">
<xsl:value-of select="$EnvironmentId"/>
</xsl:attribute>
</xsl:template>
No errors. But, the attr id was not updated.
I tried this...
<xsl:template match="Environment/@id[. =$SelectEnvironment]">
<xsl:attribute name="id">
<xsl:value-of select="$EnvironmentId"/>
</xsl:attribute>
</xsl:template>
And I got a run time error of Variables cannot be used within this expression.