Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Neil Gibbons 144 posts 61 karma points
    Feb 23, 2010 @ 19:44
    Neil Gibbons
    0

    XSLT problem

    Can't believe I haven't ran into this before but I'm trying to create a URL using an xsl:variable but can't find the correct way to add query string parameters.

    I want to render an <img> tag but the source URL of which is created dynamically, (for WebTrends). The "WT" variable is where I intended the magic to happen but I just can't the "&" in the query string parameters without either:

    • Triggering an XML error or
    • Causing the "&" to be rendered as an &amp.
    I am being daft here or what!

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "&#x00A0;"> <!ENTITY amp "&#x0026;">]>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">
    
      <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" />
    
      <xsl:param name="currentPage"/>
    
      <xsl:variable name="WTdomain" select="umbraco.library:GetDictionaryItem('WebTrendsDomain')" />
      <xsl:variable name="WTid" select="umbraco.library:GetDictionaryItem('WebTrendsID')"/>
    
      <xsl:variable name="WTurl">
          <xsl:text>http://</xsl:text>;
          <xsl:value-of select="$WTdomain" />
          <xsl:text>/</xsl:text>
          <xsl:value-of select="$WTid" />
          <xsl:text>/dcs.gif?</xsl:text>
            <!-- Query string parameters -->
                    <xsl:text>WT.seg_2=mobile</xsl:text>
            <!-- The "&" needs to go here -->
                    <xsl:text">RAND=53335</xsl:text>
      </xsl:variable>    
    
      <xsl:template match="/">
          <xsl:comment>
              <xsl:text disable-output-escaping="yes">Render WT tag </xsl:text>
              <xsl:value-of select="$WTdomain" />
              <xsl:text disable-output-escaping="yes"> - </xsl:text>
              <xsl:value-of select="$WTid" />
          </xsl:comment>
          <xsl:text>&#10;&#09;</xsl:text>
    
          <!-- image tag -->
          <img alt="." width="1" height="1">
              <xsl:attribute name="src">
                  <xsl:value-of select="$WTurl" />
              </xsl:attribute>
          </img>
      </xsl:template>
    </xsl:stylesheet>

    Thanks for any help,

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Feb 23, 2010 @ 19:50
    Douglas Robar
    0

    The problem is that the ampersand character is a 'special' character in XSLT, so it must be escaped in your xslt, as &amp;

    In your code, I would do something like this:

    <xsl:variable name="WTurl">
                    <xsl:text>http://</xsl:text>
                    <xsl:value-of select="$WTdomain" />
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="$WTid" />
                    <xsl:text>/dcs.gif?</xsl:text>
                    <!-- Query string parameters -->
                   
    <xsl:text>WT.seg_2=mobile</xsl:text>
                    <!-- The "&" needs to go here -->
    <xsl:text disable-output-escaping="yes">&amp;</xsl:text>
                   
    <xsl:text>RAND=53335</xsl:text>
            </xsl:variable>    

     

    or...

    <xsl:variable name="WTurl">
                    <xsl:text>http://</xsl:text>
                    <xsl:value-of select="$WTdomain" />
                    <xsl:text>/</xsl:text>
                    <xsl:value-of select="$WTid" />
                    <xsl:text>/dcs.gif?</xsl:text>
                    <!-- Query string parameters -->
                   
    <xsl:text>WT.seg_2=mobile</xsl:text>
                    <!-- The "&" needs to go here -->
                   
    <xsl:text disable-output-escaping="yes">&amp;RAND=53335</xsl:text>
            </xsl:variable>    

     

    cheers,
    doug.

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Feb 23, 2010 @ 19:53
    Douglas Robar
    2

    Thinking about this for another few seconds I'm not sure you'll even need the disable-output-escaping="yes" portion of the <xsl:text> tag. Try it without first and you should be okay.

    Do bear in mind that though you don't usually see &amp; on the querystring of a browser it is perfectly valid and supported by all browsers, so don't let it bother you if the url you end up with has &amp; in it rather than the shortened & only... both work just fine in the real world. :)

    cheers,
    doug.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 23, 2010 @ 20:01
    Lee Kelleher
    0

    Hi Neil,

    Remember CDATA is your friend! (modifications in bold below)

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE xsl:stylesheet [
        <!ENTITY nbsp "&#x00A0;">
        <!ENTITY amp "&#x0026;">
    ]>
    <xsl:stylesheet version="1.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxml="urn:schemas-microsoft-com:xslt"
            xmlns:umbraco.library="urn:umbraco.library"
            exclude-result-prefixes="msxml umbraco.library">
    
        <xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8" />
    
        <xsl:param name="currentPage"/>
    
        <xsl:variable name="WTdomain" select="umbraco.library:GetDictionaryItem('WebTrendsDomain')" />
        <xsl:variable name="WTid" select="umbraco.library:GetDictionaryItem('WebTrendsID')"/>
    
        <xsl:variable name="WTurl">
            <xsl:value-of select="concat('http://', $WTdomain, '/', $WTid, '/dcs.gif?')" />
            <!-- Query string parameters -->
            <xsl:text><![CDATA[WT.seg_2=mobile&RAND=53335]]></xsl:text>
        </xsl:variable>
    
        <xsl:template match="/">
            <xsl:comment>
                <xsl:text disable-output-escaping="yes">Render WT tag </xsl:text>
                <xsl:value-of select="$WTdomain" />
                <xsl:text disable-output-escaping="yes"> - </xsl:text>
                <xsl:value-of select="$WTid" />
            </xsl:comment>
            <xsl:text>&#10;&#09;</xsl:text>
    
            <!-- image tag -->
            <img alt="." width="1" height="1">
                <xsl:attribute name="src">
                    <xsl:value-of select="$WTurl" disable-output-escaping="yes" />
                </xsl:attribute>
            </img>
        </xsl:template>
    </xsl:stylesheet>

    Cheers, Lee.

  • Neil Gibbons 144 posts 61 karma points
    Feb 23, 2010 @ 20:32
    Neil Gibbons
    0

    Thanks,

    As Doug pointed out, this results in the following URL:

    dcs.gif?WT.seg_2=mobile&amp;RAND=11111

    I always though it had to be "&" not "&amp;". Its crucial that the query string parameters are retrievable - otherwise the WebTrends tracking will not work.

     

    Neil

  • dandrayne 1138 posts 2262 karma points
    Feb 23, 2010 @ 21:28
    dandrayne
    1

    In fact, using "&" only breaks xhtml validation.  Browsers can work with both though so it's not something to worry about.  From the linked page...

    -----------------------------------------------------------------------------------------------

    To avoid problems with both validators and browsers, always use &amp; in place of & when writing URLs in HTML:

    <a href="foo.cgi?chapter=1&amp;section=2&amp;copy=3&amp;lang=en">...</a>

    Note that replacing & with &amp; is only done when writing the URL in HTML, where "&" is a special character (along with "<" and ">"). When writing the same URL in a plain text email message or in the location bar of your browser, you would use "&" and not "&amp;". With HTML, the browser translates "&amp;" to "&" so the Web server would only see "&" and not "&amp;" in the query string of the request

    -----------------------------------------------------------------------------------------------

    Dan

  • Neil Gibbons 144 posts 61 karma points
    Feb 23, 2010 @ 21:56
    Neil Gibbons
    0

    Thanks Dan,

    But when I click the created link:

    /dcs.gif?;WT.seg_2=mobile&amp;1=1

    the browser (Chrome) windows open with the "&amp;" intact in the address bar. That's to be expected? It wouldn't "translate" the "&amp;" back to "&"?

     

    Neil

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 23, 2010 @ 22:29
    Lee Kelleher
    0

    Did my XSLT not work for you Neil?  Take another look... check you are using the CDATA data - and disable-output-escaping on the value-of.

    Cheers, Lee.

  • Neil Gibbons 144 posts 61 karma points
    Feb 24, 2010 @ 09:15
    Neil Gibbons
    1

    It did Lee - once I read the page Dan linked to and realised that "&amp;" is the valid approach.

    It was just throwing me a little when I clicked the link and saw the "&amp;" in the address bar - I just assumed the browser would change this.

    Thanks

Please Sign in or register to post replies

Write your reply to:

Draft