Copied to clipboard

Flag this post as spam?

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


  • trfletch 598 posts 604 karma points
    May 24, 2010 @ 11:56
    trfletch
    0

    Pull in list from other Umbraco sites using XSLT

    Hi,

    I have a number of Umbraco 4.0.3 sites and I would like to be able to pull the contents of a list from one of them into the other using XSLT if possible. The list is actually going to be a list of URL's to the other sites in the group but what I want to know is am I going to be able to do this using XSLT? Would I need to output the list as XML then using XSLT to bring it into the other sites? I actually want the list to randomly pull in three items but I think I should be ok working out how to do that (I have done random lists before) as long as I can work out how I pull in the data in the first place. Any suggestions?

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    May 24, 2010 @ 12:23
    Douglas Robar
    0

    If the sites are not in the same umbraco installation, sharing the same content tree, then you'll need to treat this more or less as you would an RSS feed.

    On the 'sharing' site, create a template with a macro that outputs your list in an xml format (see the rss feed macro template for ideas). Just like with an RSS feed you will show the output using an alternate template (the ?altTemplate syntax). That's all that is needed on the 'sharing' site.

    On the 'consuming' site, create a macro that uses the umbraco.library:GetXmlDocumentByUrl(). The url will be to the 'sharing' site. Probably something like: http://www.example.com/default.aspx?altTemplate=mySharingTemplateAlias. Just substitute the real domain and the real template alias for the 'sharing' site.

    At this point your macro will fetch the xml output of the list of urls from the 'sharing' site and you'll have that xml in a variable, which you can then loop through to display the links the way you want.

     

    Make sense?

     

    cheers,
    doug.

  • trfletch 598 posts 604 karma points
    May 26, 2010 @ 11:40
    trfletch
    0

    Hi Doug, I got a bit confused with the RSS feed and couldn't get it to work so I have created my own XSLT as follows but what else do I need to make it output as an XML file?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <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"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <xsl:value-of select="umbraco.library:ChangeContentType('text/xml')"/>
    <sites>
     <xsl:for-each select="umbraco.library:GetXmlNodeById(1106)/node [string(data [@alias='umbracoNaviHide']) != '1']">
                    <site>
                            <sitename><xsl:value-of select="@nodeName" /></sitename>
                            <siteurl><xsl:value-of select="data [@alias = 'websiteurl']" /></siteurl>
                    </site>
            </xsl:for-each>
    </sites>
    </xsl:template>
    </xsl:stylesheet>
  • trfletch 598 posts 604 karma points
    May 26, 2010 @ 12:19
    trfletch
    0

    Please ignore this, I remembered I have already asked this before.

  • trfletch 598 posts 604 karma points
    May 26, 2010 @ 12:55
    trfletch
    0

    I now have this working correctly using the following code, it now randomly pulls in 3 website links but the last thing I need to do is make it so that it doesn't pull in a link to the current page if this is possible. Therefore if I am currently on http://www.mytestsite.com/default.aspx then I do not want it to list a link to http://www.mytestsite.com. Anyone know if this is possible and how I would go about doing this? I'm assuming I would need some sort of IF statement that checks the current URL and compares it to the ones in the list?

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        xmlns:randomTools="http://www.umbraco.org/randomTools"
        exclude-result-prefixes="msxml umbraco.library msxsl randomTools">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>

    <xsl:variable name="numberOfItems" select="number(3)"/>
    <xsl:template match="/">
      <!-- start writing XSLT -->
      <xsl:variable name="MyFeed" select="umbraco.library:GetXmlDocumentByUrl('http://www.testing.co.uk/test-sites.aspx')" />
      <ul>
        <xsl:for-each select="$MyFeed/sites/site">
    <xsl:sort select="randomTools:GetRandom(0,count($MyFeed/sites/site))" order="ascending" />
    <xsl:if test="position() &lt;= $numberOfItems">
    <li>
    <a target="_blank">
     <xsl:attribute name="href">
     <xsl:value-of select="siteurl" />
     </xsl:attribute><xsl:value-of select="sitename" />
     </a>
     </li>
    </xsl:if> 
        </xsl:for-each>
      </ul>
    </xsl:template>
    <msxsl:script language="c#" implements-prefix="randomTools">
        <msxsl:assembly href="../bin/umbraco.dll"/>
        <![CDATA[
            /// <summary>
            /// Gets a random integer that falls between the specified limits
            /// </summary>
            /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param>
            /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param>
            /// <returns>A random integer within the specified range</returns>
            public static int GetRandom(int lowerLimit,int upperLimit) {
                Random r = umbraco.library.GetRandom();
                int returnedNumber = 0;
                lock (r)
                {
                    returnedNumber = r.Next(lowerLimit, upperLimit);
                }
                return returnedNumber;
            }
        ]]>
      </msxsl:script>
    </xsl:stylesheet>
  • trfletch 598 posts 604 karma points
    May 26, 2010 @ 13:24
    trfletch
    0

    Once again, I have managed to resolve it using the following code, thanks Doug, I shall mark your answer as the solution:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library"
        xmlns:randomTools="http://www.umbraco.org/randomTools"
        exclude-result-prefixes="msxml umbraco.library msxsl randomTools">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:variable name="numberOfItems" select="number(3)"/>

    <xsl:template match="/">
     <xsl:variable name="url" select="concat('http://',umbraco.library:RequestServerVariables('HTTP_HOST'))" />
      <xsl:variable name="MyFeed" select="umbraco.library:GetXmlDocumentByUrl('http://www.testsite.co.uk/test-sites.aspx')" />
      <ul>
        <xsl:for-each select="$MyFeed/sites/site [siteurl !=$url] ">
    <xsl:sort select="randomTools:GetRandom(0,count($MyFeed/sites/site))" order="ascending" />
    <xsl:if test="position() &lt;= $numberOfItems">
          <li><a target="_blank">
     <xsl:attribute name="href">
     <xsl:value-of select="siteurl" />
     </xsl:attribute><xsl:value-of select="sitename" />
     </a>
     </li>
    </xsl:if> 
        </xsl:for-each>
      </ul>
    </xsl:template>
    <msxsl:script language="c#" implements-prefix="randomTools">
        <msxsl:assembly href="../bin/umbraco.dll"/>
        <![CDATA[
            /// <summary>
            /// Gets a random integer that falls between the specified limits
            /// </summary>
            /// <param name="lowerLimit">An integer that defines the lower-boundary of the range</param>
            /// <param name="upperLimit">An integer that defines the upper-boundary of the range</param>
            /// <returns>A random integer within the specified range</returns>
            public static int GetRandom(int lowerLimit,int upperLimit) {
                Random r = umbraco.library.GetRandom();
                int returnedNumber = 0;
                lock (r)
                {
                    returnedNumber = r.Next(lowerLimit, upperLimit);
                }
                return returnedNumber;
            }
        ]]>
      </msxsl:script>
    </xsl:stylesheet>
Please Sign in or register to post replies

Write your reply to:

Draft