How do I code an ampersand into the value of an XML attribute using XSL?

Posted on 2007-10-09
Last Modified: 2013-11-18
How do I code an ampersand into the value of an XML attribute using XSL?

Here is my XML Doc:


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="file:///C:/Temp/TestAmp.xsl"?>


Here is my XSL doc:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="" version="2.0">
    <xsl:template match="/">
        <div xmlns:xdt=""
            xmlns:xs="" xmlns:fo=""
                <xsl:when test="/DocRoot/PWVerified = 'True'">
                        <h3>everything is OK</h3>
                        <font color="red"
                            >You must set a secret question and change your password before you can see this channel.</font>
                    <form method="get">
                        <xsl:attribute name="action"
                                /><xsl:text disable-output-escaping="yes"><![CDATA[&Key=]]></xsl:text><xsl:value-of
                        <input type="submit" value="Set secret question"/>


Here is the '<form/>' statement I get:


<form method="get" action=";Key=100920071002274067A25D0B9D55E19ECE6EA6613BE8"><input type="submit" value="Set secret question"/></form>


Of course, I would like a simple '&' above instead of '&amp;'.  I've tried so many combinations of <![CDATA[ and <xsl:text/> with disable-output-escaping="yes" that I can't see straight any more.  HELP!!
Question by:WJoeMoore
    LVL 11

    Accepted Solution

    Is the &amp; a problem in the string?

    Author Comment

    Yes, when I try to submit the URL from the resulting HTML page.  Is there something else I should code in the <form/> sequence to cause it to translate it at that point?
    LVL 60

    Assisted Solution

    by:Geert Bormans
    The way you use CDATA here is not very usefull, because the processor will see it as a text node
    If you want to create a CDATA section, it needs to be in an element, attributes can't be CDATA sections

     <xsl:text disable-output-escaping="yes">&amp;</xsl:text>
    is the right way to get a "&" in the output text
    but that only works in elements

    attributes have special rules for their content
    and you can't play dirty thricks with them (as the disable output escaping)
    ... meaning, whatever you try, you 'll be stuck with the escaped &amp;

    In short, as far as I know, this can't be done
    You could hope that browsers accept the &amp; style, most of them do by now



    Author Comment

    It worked fine when I used 'method=post' instead of 'method=get'.  Grumble, grumble.  I still mess those up every once in awhile.
    LVL 60

    Expert Comment

    by:Geert Bormans
    have you tried escaping the URL way
    %26 should be the amp
    LVL 11

    Expert Comment

    Take a look at

    &amp; should be allowed to be used in your string.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
    JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now