Copied to clipboard

Flag this post as spam?

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


  • ianhoughton 281 posts 605 karma points c-trib
    Jun 14, 2010 @ 18:00
    ianhoughton
    0

    Inserting string into jquery in XSLT

    I thought I was doiing so well to get this far !!!

    I have the following XSLT that creates a string like this:

    [{latitude:53.049779,longitude:-2.171795,html:Community Drive,popup:true},{latitude:53.069779,longitude:-2.191795,html:Greenbrook Court,popup:true}]

    <xsl:param name="currentPage"/>
    <xsl:variable name="source" select="/macro/source"/>
    
    <xsl:template match="/">
    
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node">
        <xsl:if test="data [@alias = 'propertyLatitude'] != ''">
            <xsl:choose>
                <xsl:when test="position() = 1">
                    <xsl:variable name="propertyName" select="data [@alias = 'propertyName']"/>
                    <xsl:variable name="markerLatitude" select="data [@alias = 'propertyLatitude']"/>
                    <xsl:variable name="markerLongitude" select="data [@alias = 'propertyLongitude']"/>
                    <xsl:variable name="markers" select="concat('[{','latitude:', $markerLatitude,',','longitude:', $markerLongitude,',','html:', $propertyName,',','popup:', 'true','},')"/>
                    <xsl:value-of select="$markers"/>
                </xsl:when>
                <xsl:when test="position() != last()">
                    <xsl:variable name="propertyName" select="data [@alias = 'propertyName']"/>
                    <xsl:variable name="markerLatitude" select="data [@alias = 'propertyLatitude']"/>
                    <xsl:variable name="markerLongitude" select="data [@alias = 'propertyLongitude']"/>
                    <xsl:variable name="markers" select="concat('{','latitude:', $markerLatitude,',','longitude:', $markerLongitude,',','html:', $propertyName,',','popup:', 'true','}]')"/>
                    <xsl:value-of select="$markers"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:variable name="propertyName" select="data [@alias = 'propertyName']"/>
                    <xsl:variable name="markerLatitude" select="data [@alias = 'propertyLatitude']"/>
                    <xsl:variable name="markerLongitude" select="data [@alias = 'propertyLongitude']"/>
                    <xsl:variable name="markers" select="concat('{','latitude:', $markerLatitude,',','longitude:', $markerLongitude,',','html:', $propertyName,',','popup:', 'true','}')"/>
                    <xsl:value-of select="$markers"/>
                </xsl:otherwise>
            </xsl:choose>
    
    
        </xsl:if>
    </xsl:for-each>
    
    <!-- start writing XSLT -->
    <script type="text/javascript" charset="utf-8">
        <![CDATA[
        $(document).ready(function () {
            $("#map").gMap({
            latitude: 53.026193,
                      longitude: -2.176452,
            zoom: 12,
            maptype:G_HYBRID_MAP,
            markers: ]]><xsl:value-of select="$markers"/><![CDATA[
            });
        });
        ]]>
    </script>
    
    </xsl:template>

    I tried inserting the string into the jquery function with this code:

    ]]><xsl:value-of select="$markers"/><![CDATA[

    but its currently giving me an error. Any ideas ??

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Jun 14, 2010 @ 18:02
    Ismail Mayat
    0

    ian,

    I suspect its becuase you have the value-of tag in outer cdata tag.  What is the error exactly?

    Regards

    Ismail

  • ianhoughton 281 posts 605 karma points c-trib
    Jun 14, 2010 @ 19:00
    ianhoughton
    0

    Just this:

    Error occured

    Error in XSLT at line 56, char 15
    54:       zoom: 12,
    55:       maptype:G_HYBRID_MAP,
    56: >>>   markers: ]]><xsl:value-of select="$markers"/><![CDATA[ <<<
    57:       });
    58:       });

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 14, 2010 @ 19:11
    Morten Bock
    0

    Well, your $markers variable is not defined in the scope that you are trying to use it in.

    What are you trying to do here? You seem to be assigning the variable multiple times with the for-each, but only using it once?

  • dandrayne 1138 posts 2262 karma points
    Jun 14, 2010 @ 19:19
    dandrayne
    0

    Hi Ian

    Very similar code is working for me at the minute (see below), but I'm certain I saw a more elegant way to do it from chriztian - just can't find it now.  If <xls:value-of is appearing in your source code it seems that it's still stuck inside a CDATA section

    <script type="text/javascript">
    //<![CDATA[
    var postCode = "]]><xsl:value-of select="Postcode" /><![CDATA[";
    var street = "]]><xsl:value-of select="Address1" /><![CDATA[";

    function doSomething(something) {
    return something;
    }
    //]]>
    </script>

    Dan

  • ianhoughton 281 posts 605 karma points c-trib
    Jun 14, 2010 @ 19:24
    ianhoughton
    0

    I have a number of properties with Latitude and Longitute values. I'm trying to get their values and build a string like the example below:

    [{latitude:53.049779,longitude:-2.171795,html:Community Drive,popup:true},{latitude:53.069779,longitude:-2.191795,html:Greenbrook Court,popup:true}]

    This string then needs to be inserted into the jquery after markers:

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 14, 2010 @ 19:29
    Morten Bock
    0

    Ok. Variables in XSLT are immutable, so you cannot add something to a variable once it has been defined.

    You probably want to do something like this:

    <xsl:param name="currentPage"/>
    <xsl:variable name="source" select="/macro/source"/>
    
    <xsl:template match="/">
    
      <xsl:variable name="totalmarkerstring">
        <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node">
          <xsl:if test="data [@alias = 'propertyLatitude'] != ''">
            <xsl:choose>
              <xsl:when test="position() = 1">
                <xsl:variable name="propertyName" select="data [@alias = 'propertyName']"/>
                <xsl:variable name="markerLatitude" select="data [@alias = 'propertyLatitude']"/>
                <xsl:variable name="markerLongitude" select="data [@alias = 'propertyLongitude']"/>
                <xsl:variable name="markers" select="concat('[{','latitude:', $markerLatitude,',','longitude:', $markerLongitude,',','html:', $propertyName,',','popup:', 'true','},')"/>
                <xsl:value-of select="$markers"/>
              </xsl:when>
              <xsl:when test="position() != last()">
                <xsl:variable name="propertyName" select="data [@alias = 'propertyName']"/>
                <xsl:variable name="markerLatitude" select="data [@alias = 'propertyLatitude']"/>
                <xsl:variable name="markerLongitude" select="data [@alias = 'propertyLongitude']"/>
                <xsl:variable name="markers" select="concat('{','latitude:', $markerLatitude,',','longitude:', $markerLongitude,',','html:', $propertyName,',','popup:', 'true','}]')"/>
                <xsl:value-of select="$markers"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:variable name="propertyName" select="data [@alias = 'propertyName']"/>
                <xsl:variable name="markerLatitude" select="data [@alias = 'propertyLatitude']"/>
                <xsl:variable name="markerLongitude" select="data [@alias = 'propertyLongitude']"/>
                <xsl:variable name="markers" select="concat('{','latitude:', $markerLatitude,',','longitude:', $markerLongitude,',','html:', $propertyName,',','popup:', 'true','}')"/>
                <xsl:value-of select="$markers"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
        </xsl:for-each>
      </xsl:variable>
      <!-- start writing XSLT -->
      <script type="text/javascript" charset="utf-8">
        <![CDATA[
          $(document).ready(function () {
                  $("#map").gMap({
                  latitude: 53.026193,
                    longitude: -2.176452,
                  zoom: 12,
                  maptype:G_HYBRID_MAP,
                  markers: ]]><xsl:value-of select="$totalmarkerstring"/><![CDATA[
                  });
          });
          ]]>
      </script>
    
    </xsl:template>
  • ianhoughton 281 posts 605 karma points c-trib
    Jun 14, 2010 @ 20:43
    ianhoughton
    0

    Thanks everyone for helping, Morten's answer fixed the issue.

    I've now got a working Google map with markers set using 2 fields in the property DocumentType.

Please Sign in or register to post replies

Write your reply to:

Draft