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.
Create the XSLT Macro that generates the HTML
Place the XSLT Macro in blank page
Call page with AJAX
Is there a better way to do this?
Can I run the XSLT macro programmatically in .NET code and return the result? This way I do not have to put the XSLT macro on a blank page.
You may try umbraco.library.RenderMacroContent
to render an Xslt macro.
http://our.umbraco.org/wiki/reference/umbracolibrary/rendermacrocontent
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(".");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.