Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3280030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:39:32+00:00 2026-05-17T19:39:32+00:00

I am transforming an XML where I am supposed to locate a particular Element

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:39:33+00:00Added an answer on May 17, 2026 at 7:39 pm

    You cannot have variables in template matches; they can be compared to compile-time. The answer to your conundrum is to move the logic out of the match (and perhaps lose a slight penalty in performance). Untested ;

    <xsl:template match="Environment/@id">
       <xsl:if test="[.=$SelectEnvironment]">
          <xsl:attribute name="id">
             <xsl:value-of select="$EnvironmentId"/>
          </xsl:attribute>
       </xsl:if>
    </xsl:template>
    

    However, there’s other, better and faster ways to slice your problem which probably can be explained a bit better than what your example problem points to?

    Here’s my full XSLT that does what you want, fully tested (Is the missing closing of the element a mistake in copy-paste?) ;

    <?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">
            <xsl:choose>
                <xsl:when test="@id=$SelectEnvironment">
                    <Environment id="{$EnvironmentId}" />
                </xsl:when>
                <xsl:otherwise>
                    <Environment id="{@id}" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am transforming an XML document. There is an attribute @prettydate that is a
I am having trouble transforming particular characters from an XML feed into XHTML. I
I'm trying to preserve row breaks in an xml file when transforming it to
can anyone give me a paradigm of transforming an xml schema template like <xs:element
I'm transforming an XHTML file to XML. I'm having issues that the XHML entity
I have an XML(XHTML) file which I'm transforming into another XML file by using
I am transforming some XML, renaming each element named alt-title to Running_Head , provided
I'm transforming an XML document with PHP/XSL. I'm searching for a keyword value. I
I am transforming following XML to generate HTML. XML <clause code=section6> <variable col=1 name=R1C1
Can any one help in transforming my xml though XSLT. I am new to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.