开发者

xsl transform: problem with Ampersand URL parameters

开发者 https://www.devze.com 2022-12-31 20:01 出处:网络
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.

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;Id='<xsl:value-of disable-output-escaping="yes" select="$app" />'")
      </xsl:attribute>

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

URL transformed:

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

URL Expected:

https://server/image.gif?a=10&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 t开发者_高级运维hrown.


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;Id=</xsl:text>
        <xsl:value-of disable-output-escaping="yes" select="$app" />
        <xsl:text>')</xsl:text>
      </xsl:attribute>
    </div>
  </body>


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;Id='1052391'&quot;)"
></div>

acutally is valid HTML, while this

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消