Converting Sitecore items to XML to use in XSLT renderings

Published by on
Tags:

Sometimes you want to retrieve an item or collection of items using C# and then pass these items to an XSLT rendering. The advantage of this is that you get the processing power of C# with the simple layout and Sitecore XSLT controls of a rendering. To do this the following methods and extension methods should help. The interesting stuff, the code:

        public static XPathNodeIterator ToXml(this Item item)
        {
            return ToXml(new Item[] { item });
        }

        public static XPathNodeIterator ToXml(this Item[] items)
        {
            return ConvertToXml(items);
        }

        private static XPathNodeIterator ConvertToXml (Item [] items)
        {                         
            var packet = new Packet("values", new string[0]);
            foreach (Item item  in items)
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(item.GetOuterXml(false));
                packet.AddXml(doc);
            }            
            return GetChildIterator(packet);                
        }

        private static XPathNodeIterator GetChildIterator(Packet packet)
        {
            XPathNavigator xpathNavigator = packet.XmlDocument.CreateNavigator();
            if (xpathNavigator == null)
            {
                return null;
            }
            xpathNavigator.MoveToRoot();
            xpathNavigator.MoveToFirstChild();
            return xpathNavigator.SelectChildren(XPathNodeType.Element);
        }

Using the Packet class which exists in the Sitecore.Xml namespace we can load our item/items into it and then pass the whole lot back to the XSLT rendering by creating an XPathNodeIterator from the packet . The extension methods allow us to more easily call the method. To call the methods:

        public static XPathNodeIterator doQuery(string query)
        {
            Item[] results = Sitecore.Context.Database.SelectItems(query);
            return results.ToXml();
        }

Or


        public static XPathNodeIterator doQuery(string query)
        {
            Item result = Sitecore.Context.Database.SelectSingleItem(query);
            return result.ToXml();
        }	 

Within our XSLT we can then use it like so:

    <xsl:variable select="eic:doQuery($query)" name="results" />
    <xsl:for-each select="$results">      
        <sc:text field="Title" />   
    </xsl:for-each>

I can iterate over the items as I would normally and can call of the standard Sitecore XSLT controls

Manually using Sitecore XSLT Renderings

Published by on
Tags:

If you want a simple way to get the output from an XSLT Rendering using Sitecore in code use the following:

    Sitecore.Web.UI.WebControls.XslFile file = new Sitecore.Web.UI.WebControls.XslFile();
    file.Path = "/xsl/MyRendering.xslt";
    string output = file.RenderAsText(); 

This will give you the output of the rendering as a single string that you can then use for any purpose.One thing that would be really cool would be to create XSLT on the fly and feed it into this XslFile control. If I work this out I will let you know.

 

All blogs tagged with 'xslt'

Projects

Have you read?

Sitecore Sheer XAML Applications - Part 1



Blogs by date