Copied to clipboard

Flag this post as spam?

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


  • Edward Dudley 80 posts 121 karma points
    Nov 19, 2009 @ 17:48
    Edward Dudley
    0

    Ampersand in XSLT

    Hi,

    I've googled this and I'm sure I've done it before but I can't remember how!

    I'm generating a url in XSLT which has query string parameters but with the XSLT escaping I can't get it to work.

    My XSLT produces:

    http://www.mysite.com/mypage.aspx?param1=1&param2=2

    How can I turn this & into just &  ?

    Thanks!

  • Edward Dudley 80 posts 121 karma points
    Nov 19, 2009 @ 18:06
    Edward Dudley
    1

    Phew - done it.

    For anyone else with this problem, the solution is to store the URL as a variable, then output it disablig output escaping.

    ie.

    <xsl:variable name="test" select="http://www.mysite.com/mypage.aspx?param1=1&amp;param2=2" />
    <xsl:value-of select="$test" disable-output-escaping="yes" />

     

    If there's a better method I'd still be interested to know though!

  • dandrayne 1138 posts 2262 karma points
    Nov 19, 2009 @ 18:27
    dandrayne
    0

    You could try

    &#38;

    For more see http://www.w3schools.com/tags/ref_entities.asp

    Dan

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Nov 19, 2009 @ 20:58
    Chriztian Steinmeier
    0

    Hi Edward,

    Assuming you're outputting HTML (or XML), there's actually nothing wrong with the ampersands in your URL - in fact, it's how you're supposed to write an ampersand in HTML & XML.

    Before the XSLT file is handed to the XSLTProcessor it's "just" an XML file which gets parsed by an XML parser. One of the tasks that the parser takes care of, is resolving entity references to their actual characters/expansions - you can try that in Internet Explorer, by creating a simple XML document like this:

    <?xml version="1.0"?>
    <title>Thelma &amp; Louise</title>

    Save that as "movie.xml" and open it in Internet Explorer - the default XML rendering in IE shows you how the XSLT Processor will see the document after being parsed by the XML parser. See the ampersand entity has been resolved? (And by the way - to the XSLT Processor there's no difference between &amp; and &#38; (or &#x26; for that matter) - they all result in the same character when resolved - try them in the little XML above...)

    If you're generating HTML or XML, you'll want the XSLT Processor to handle the escaping for you in the output too, so that characters which needs escaping gets escaped in the output.

    Agreed, there are times when you would want to output without escaping the various characters (mostly when converting text to actual HTML) which is what the disable-output-escaping attribute is for.

    /Chriztian

     

  • Edward Dudley 80 posts 121 karma points
    Nov 23, 2009 @ 11:22
    Edward Dudley
    0

    Thanks for the replies!

    I've tried the two suggested approaches but the approach I described before is the only one that works as required.

    @dandrayne - I've tried putting this in my XSLT but the link produces still comes out as http://www.mysite.com/page?param1=1&amp;param2=2

    @Chriztian - I think maybe you misunderstood my requirements.  Thanks for the in depth response about XML parsing standards though!

  • Paul A 133 posts 368 karma points
    Aug 29, 2012 @ 00:04
    Paul A
    0

    This worked for me (rather than method="xml")

    <xsl:output method="html" omit-xml-declaration="yes"/>
Please Sign in or register to post replies

Write your reply to:

Draft