views:

51

answers:

2

I'd like to return a list of items from an XSLT helper function in Sitecore. The items could conceivably be from multiple different places inside the content tree, so a simple XPath expression can't be used.

I suspect that I need to do something with the XPathNodeIterator class, however I can't quite figure out how to create one that exposes a list rather than a contiguous part of the content tree.

I know that I can return a list of IDs and then iterate over those in the XSLT (what I'm currently doing), but it's messy and I'd like to get rid of it. Does anyone know how to do this?

I'm using Sitecore 6.2.

+1  A: 

Yes. Depending on the nature of the item selection you want to do, there are a number of ways one can arrive at this.

The following should do the trick

public XPathNodeIterator MyFunction( Item[] itemsToReturn )
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml( @"<items />" );
    foreach ( Item i in itemsToReturn )
        doc.DocumentElement.AppendChild( XmlUtil.NavigatorToNode( Factory.CreateItemNavigator( i ), doc, true, new string[] { "item" }, new string[ 0 ] ) );
    XPathNavigator nav = doc.CreateNavigator();
    return nav.Select( "/items/*" );
}

I haven't the time to fully test this, but it should send you in the right direction :-)

Mark Cassidy
It seems to work. Thanks very much!
Matt
A: 

I would stick with using IDs, but maybe return them as an XML structure instead of a string to parse. The XPathNodeIterator approach might interfere with inline editing in the page editor, as well as other XSL extensions such as generating URLs.

See also:

http://sdn.sitecore.net/forum//ShowPost.aspx?PostID=6396

John West
@John, thanks for the extra information... I'm aware that I won't get the additional item XML for child items and such. Do the page editors and URL generation functions rely on these items being present?
Matt