homepage Welcome to WebmasterWorld Guest from 54.224.202.109
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Need to call a JS function passing in concatenated XSL variables
andrewsmd




msg:4376775
 9:19 pm on Oct 19, 2011 (gmt 0)

I have an xsl file that I need to call a JS function. I need to pass in two variables to this function. A div ID and some content. Here are the variables I need to pass in.

<xsl:value-of select="udt:Name" disable-output-escaping="yes" />
<xsl:value-of select="udt:Driving_x0020_Since" disable-output-escaping="yes" />
<xsl:value-of select="udt:Miles" disable-output-escaping="yes" />

I need to do two things.

1.
I need to pass in a different div name based on the numeric value of the miles element. I'm assuming I can use an xsl chose for that, but how would I create a variable that I set based on the when statement

2.
I need to pass all three xsl variables as some html in the second part of the function. Essentially the xsl would need to output a JS function that looks like this
millionMiles("divIdBasedOnWhenHere", "Name Driving Since Miles"); How do I combine those elements together in this JS function call? Thanks

 

andrewsmd




msg:4376794
 9:49 pm on Oct 19, 2011 (gmt 0)

Figured out the variable thing
<xsl:variable name="divId">
<xsl:choose>
<xsl:when test="number($milesVar) >= 3000000 ">threeMillion</xsl:when>
<xsl:when test="number($milesVar) >= 2000000">twoMillion</xsl:when>
<xsl:otherwise>oneMillion</xsl:otherwise>
</xsl:choose>
</xsl:variable>

Now how do I add all of those select values and output a javascript function call?

httpwebwitch




msg:4378648
 1:38 pm on Oct 24, 2011 (gmt 0)

you can make XSLT render a <script> element, and everything in it.

Dont forget to use <![CDATA[ ]]> nodes so your script doesn't get mangled.

Another option is to output those XSL values into some hidden <input> elements, then write your javascript so it executes on pageload (or domready) and reads those values from the DOM. That way will be far less complicated & easier to code up.

andrewsmd




msg:4378675
 3:01 pm on Oct 24, 2011 (gmt 0)

This is what I ended up with
<xsl:template match="udt:Data" mode="list">
<tr class="Normal">
<td>
<xsl:variable name="milesVar" select="udt:Miles" />
<xsl:variable name="divId">
<xsl:choose>
<xsl:when test="number($milesVar) >= 3000000 ">threeMillion</xsl:when>
<xsl:when test="number($milesVar) >= 2000000">twoMillion</xsl:when>
<xsl:otherwise>oneMillion</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<script type="text/javascript">
<xsl:text>millionMiles('</xsl:text><xsl:call-template name="EditLink" /><xsl:value-of select="udt:Name" disable-output-escaping="yes" /><br />
<xsl:value-of select="udt:Image" disable-output-escaping="yes" /><br />
<xsl:text>Driving since </xsl:text><xsl:value-of select="udt:Driving_x0020_Since" disable-output-escaping="yes" /><br />
<xsl:text>He has over </xsl:text><xsl:value-of select="udt:Miles" disable-output-escaping="yes" /> accident free miles.<br /><br />
<xsl:text>', "</xsl:text><xsl:value-of select="$divId" />");<xsl:text></xsl:text>
</script>

</td>
</tr>
</xsl:template>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved