tags:

views:

189

answers:

2

I'm having issues with transforming XSL with parameters in a URL. I'm at a point that I can't change the C# code anymore, only can make changes to xsl file.

C# code:

string xml = "<APPLDATA><APPID>1052391</APPID></APPLDATA>";

XmlDocument oXml = new XmlDocument();
oXml.LoadXml(xml);

XslTransform oXslTransform = new XslTransform();
oXslTransform.Load(@"C:\Projects\Win\ConsoleApps\XslTransformTest\S15033.xsl");

StringWriter oOutput = new StringWriter();
oXslTransform.Transform(oXml, null, oOutput)

XSL Code:

  <body>
    <xsl:variable name="app">
      <xsl:value-of select="normalize-space(APPLDATA/APPID)" />
    </xsl:variable>
    <div id="homeImage" >
      <xsl:attribute name="style">
        background-image:url("https://server/image.gif?a=10&amp;amp;Id='&lt;xsl:value-of disable-output-escaping="yes" select="$app" />'")
      </xsl:attribute>

    </div>
  </body>
</html>

URL transformed:

https://server/image.gif?a=10&amp;amp;Id='1052391'

URL Expected:

https://server/image.gif?a=10&amp;Id='1052391'

How do I fix this? The output (oOutput.ToString()) is being used in an email template so it's taking the URL transformed literally. When you click on this request (with the correct server name of course), the 403 (Access forbidden) error is being thrown.

+1  A: 

What you get is actually what you want. The ampersand must be escaped in HTML, no matter where it occurs. So this

<div 
  id="homeImage" 
  style="background-image:url(&quot;https://server/image.gif?a=10&amp;amp;Id='1052391'&amp;quot;)"
></div>

acutally is valid HTML, while this

<div 
  id="homeImage" 
  style="background-image:url(&quot;https://server/image.gif?a=10&amp;Id='1052391'&amp;quot;)"
></div>

is not (check it in the validator). The error you receive must come from somewhere else.

Tomalak
A: 

The problem is not the ampersand but the single quotes around the id. If they have to be present they have to be url encoded.

So (assuming no quotes are needed around the id) this should work:

  <body>
    <xsl:variable name="app">
      <xsl:value-of select="normalize-space(APPLDATA/APPID)" />
    </xsl:variable>
    <div id="homeImage" >
      <xsl:attribute name="style">
        <xsl:text disable-output-escaping="yes">background-image:url('https://server/image.gif?a=10&amp;amp;Id=&lt;/xsl:text&gt;
        <xsl:value-of disable-output-escaping="yes" select="$app" />
        <xsl:text>')</xsl:text>
      </xsl:attribute>
    </div>
  </body>
Obalix