Copied to clipboard

Flag this post as spam?

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


  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 17:52
    Aron Gamble
    0

    Add URL to a string in Javascript/xslt

    This is driving me insane - I have an xslt file that contains some dynamic javascript based on child nodes which i'm looping around. I need to add a link to the end of the following javascript string:-

    var myHtml = "<b><xsl:value-of select="./@nodeName"/></b>" +

    "<br /><ul>" +

     

    "<li><b>Capacity: </b> <xsl:value-of select="./data [@alias = 'capacityText']"/></li>" + 

    "<li><b>Location: </b> <xsl:value-of select="./data [@alias = 'locationText']"/></li>" + 

    "<li><b>Project Status: </b> <xsl:value-of select="./data [@alias = 'projectStatus']"/></li>" +  

    LINK NEEDED HERE (something like "<a href='<xsl:value-of select="./data [@alias = 'url']"/>'>click me</a>" +

    "</ul>";

    The problem is the link URL needs to be pulled from the node but i can't for the life of me get the <a href> working. I believe the problem lies with the quotes - I've tried escaping the quotes but i get errors, I've tried using JavaScript variables but no luck. I either get xslt parse errors or JavaScript errors. I can't think of anything else to try, any suggestions would be great.
    Thanks

    Aron

     

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 14, 2010 @ 17:57
    Ismail Mayat
    0

    Aron,

    try

    <a href='{./data [@alias = 'url']
    }'>
  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 18:02
    Aron Gamble
    0

    Ismail

    That's exactly what i want to do but the xslt parser doesn't like it..

    I tried adding this:-

    "<a href='{./data [@alias = 'URL']}'>" +

    so the whole string looked like:-
    var myHtml = "<b><xsl:value-of select="./@nodeName"/></b>" +
    "<br /><ul>" +
    "<li><b>Capacity: </b> <xsl:value-of select="./data [@alias = 'capacityText']"/></li>" + 
    "<li><b>Location: </b> <xsl:value-of select="./data [@alias = 'locationText']"/></li>" + 
    "<li><b>Project Status: </b> <xsl:value-of select="./data [@alias = 'projectStatus']"/></li>" + 
    "<a href='{./data [@alias='locationText']}'>" +
    "</ul>";

    but i get the following xslt error:-

    System.Xml.XmlException: 'locationText' is an unexpected token. Expecting white space. Line 103, position 32. 

    Bugger..

    Aron

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jul 14, 2010 @ 18:11
    Ismail Mayat
    1

    Try replacing the ' quotes with escaped double quotes eg \"

     

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jul 14, 2010 @ 18:11
    Nik Wahlberg
    0

    I think it has to do with the doubling up of single-ticks. Try using double quotes and escaping them for the outer so it would look like this:

    "<a href=\"{./data [@alias='locationText']}\">"

     

    HTH,
    Nik

  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 18:16
    Aron Gamble
    0

    Hi

     

    I tried this:-

    "<a href=\"{./data [@alias = 'url']}\"/>click me</a>"

    But get this xslt error:-

    System.Xml.XmlException: '\' is an unexpected token. The expected token is '"' or '''. Line 103, position 14. 

    It seems like the parser doesn't like the fact were trying to escape the double quote for some reason.

    Aron

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 14, 2010 @ 18:55
    Matt Brailsford
    0

    What about using HTML encoded quotes?

    "

    Matt

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 14, 2010 @ 18:57
    Matt Brailsford
    0

    Oops, got encoded on my phone.

    &quot;

    matt

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jul 14, 2010 @ 19:09
    Nik Wahlberg
    0

    Or, if possible, please wrap your script contents in a CDATA tag. 

  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 20:32
    Aron Gamble
    0

    Thanks for the responses..

    Matt - i tried &quot and the xslt parser doesn't like the ampersand

    Nik - Won't CDATA just spit out everything literally including the xsl tags etc. ?

    Thanks again.

    Aron

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 14, 2010 @ 21:29
    Lee Kelleher
    1

    Hi Aron,

    Try this...

    <script type="text/javascript">
        var myHtml = "<b><xsl:value-of select="@nodeName"/></b>"
        + "<br /><ul>"
        + "<li><b>Capacity: </b><xsl:value-of select="data[@alias='capacityText']"/></li>"
        + "<li><b>Location: </b><xsl:value-of select="data [@alias='locationText']"/></li>"
        + "<li><b>Project Status: </b><xsl:value-of select="data[@alias='projectStatus']"/></li>"
        + <xsl:value-of select="concat('&lt;a href=&quot;', data[@alias='url'], '&quot;&gt;click me&lt;/a&gt;')" disable-output-escaping="yes" />
        + "</ul>";
    </script>

    I've used the concat() function to escape the <a> tag.  Let me know if it works out.

    Cheers, Lee.

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jul 14, 2010 @ 21:31
    Nik Wahlberg
    0

    Snazzy Lee! I like that...

  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 21:56
    Aron Gamble
    0

    Lee

    We're almost there!

    I took your example and added quotes around the line with the link so the whole thing looks like this:-

     

    var myHtml = "<b><xsl:value-of select="@nodeName"/></b>"

            + "<br /><ul>"

            + "<li><b>Capacity: </b><xsl:value-of select="data[@alias='capacityText']"/></li>"

            + "<li><b>Location: </b><xsl:value-of select="data [@alias='locationText']"/></li>"

            + "<li><b>Project Status: </b><xsl:value-of select="data[@alias='projectStatus']"/></li>"

            + "<xsl:value-of select="concat('&lt;a href=&quot;', data[@alias='projectStatus'], '&quot;&gt;click me&lt;/a&gt;')" disable-output-escaping="yes" />"

            + "</ul>";

     

    Unfortnately the link gets rendered like this:-
    "<a href="Operational.aspx">click me</a>"
    I think i just need the single quotes around Operational and it should work. I tried &apos and &#39 instead but the parser doesn't like it, but it's ok with &quot !!!
    Aron

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jul 14, 2010 @ 21:59
    Lee Kelleher
    1

    Try putting slashes in-front of the double-quotes. (I'm stealing Ismail's suggestions now) ;-)

    + "<xsl:value-of select="concat('&lt;a href=\&quot;', data[@alias='projectStatus'], '\&quot;&gt;click me&lt;/a&gt;')" disable-output-escaping="yes" />"

    That will escape them in the outputted JavaScript.

    Failing this... there is an alternative - and that's mixing in-and-out of <xsl:text> tags - it looks horrid, but it will work.

    Cheers, Lee.

  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 22:10
    Aron Gamble
    0

    Lee

    That seems to have done it!

    The section gets rendered like this:-

    var myHtml = "<b>Barlockhart Moor</b>"
    + "<br><ul>"
    + "<li><b>Capacity: </b>10MW</li>"
    + "<li><b>Location: </b>Dumfries &amp; Galloway</li>"
    + "<li><b>Project Status: </b>Consented</li>"
    + "<a href=\"http://www.cnn.com\">click me</a>"
    + "</ul>";
    The link gets rendered in the map ok and seems to work!
    Thanks everyone for your suggestions, i can now go and watch the Sopranos in peace...
    Cheers
    Aron

  • Nik Wahlberg 639 posts 1237 karma points MVP
    Jul 14, 2010 @ 22:12
    Nik Wahlberg
    1

    Glad to see it working. Aren't you missing the <li> around the link though? Just a thought...

  • Aron Gamble 63 posts 76 karma points
    Jul 14, 2010 @ 22:21
    Aron Gamble
    0

    Well spotted - I'll tidy it up and put the <li> tags in.

    Cheers

    Aron

     

Please Sign in or register to post replies

Write your reply to:

Draft