Copied to clipboard

Flag this post as spam?

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


  • Deepfreezed 8 posts 28 karma points
    Apr 29, 2011 @ 00:58
    Deepfreezed
    0

    Retrieving HTML generated using XSLT through AJAX

    I am new to Umbraco, just want to get input on if the following is the way to go to retrieve HTML generated using XSLT through AJAX.

    1. Create the XSLT Macro that generates the HTML

    2. Place the XSLT Macro in blank page

    3. Call page with AJAX

    Is there a better way to do this? Can we run the XSLT macro programmatically in .NET code and return string using BASE?

     

  • Deepfreezed 8 posts 28 karma points
    May 13, 2011 @ 23:35
    Deepfreezed
    0

    Here is the code I used to just get the HTML for an XSLT macro in Umbraco. Setup a RestExtension to return this content. The code is not refined and hard coded. I stole this from the xsltVisualize.aspx.cs

    public static string GetMacroContent()
       
    {
           
    HttpRequest post = HttpContext.Current.Request;
           
    Member umbMember = Member.GetCurrentMember();
           
    string macroname = post["macroname"];
           
    string content = string.Empty;

           
    if(Member.IsLoggedOn() && !string.IsNullOrEmpty(macroname))
           
    {
               
    string xslt = "";

               
    System.IO.StreamReader xsltFile =
               
    System.IO.File.OpenText(
                   
    IOHelper.MapPath(SystemDirectories.Root + "/xslt/htmlcontent.xslt")
               
    );

                xslt
    = xsltFile.ReadToEnd();
                xsltFile
    .Close();

               
    // prepare support for XSLT extensions
                xslt
    = macro.AddXsltExtensionsToHeader(xslt);

               
    Dictionary<string, object> parameters = new Dictionary<string, object>(1);
                parameters
    .Add("currentPage", library.GetXmlNodeById("1057"));


               
    // apply the XSLT transformation
               
    string xsltResult = "";
               
    XmlTextReader xslReader = null;
               
    try
               
    {
                    xslReader
    = new XmlTextReader(new StringReader(xslt));
                   
    System.Xml.Xsl.XslCompiledTransform xsl = macro.CreateXsltTransform(xslReader, false);
                    xsltResult
    = macro.GetXsltTransformResult(new XmlDocument(), xsl, parameters);
               
    }
               
    catch(Exception ee)
               
    {
                    xsltResult
    = string.Format(
                       
    "<div class=\"error\"><h3>Error parsing the XSLT:</h3><p>{0}</p></div>",
                        ee
    .ToString());
               
    }
               
    finally
               
    {
                    xslReader
    .Close();
               
    }              
           
    }

           
    return content;
       
    }

       
    private static XPathNodeIterator GetXmlNodeById(string id)
       
    {
           
    if(UmbracoContext.Current.GetXml().GetElementById(id) != null)
           
    {
               
    XPathNavigator xp = UmbracoContext.Current.GetXml().CreateNavigator();
                xp
    .MoveToId(id);
               
    return xp.Select(".");
           
    }
           
    else
           
    {
               
    XmlDocument xd = new XmlDocument();
                xd
    .LoadXml(string.Format("<error>No published item exist with id {0}</error>", id));
               
    return xd.CreateNavigator().Select(".");
           
    }

     

Please Sign in or register to post replies

Write your reply to:

Draft