从 Sitecore XSLT 辅助函数返回项目列表

发布于 2024-09-12 16:43:01 字数 252 浏览 2 评论 0原文

我想从 Sitecore 中的 XSLT 辅助函数返回项目列表。这些项目可能来自内容树内的多个不同位置,因此无法使用简单的 XPath 表达式。

我怀疑我需要对 XPathNodeIterator 类做一些事情,但是我不太清楚如何创建一个公开列表而不是内容树的连续部分的类。

我知道我可以返回 ID 列表,然后迭代 XSLT 中的 ID(我当前正在做的事情),但它很混乱,我想摆脱它。有谁知道该怎么做?

我正在使用 Sitecore 6.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2024-09-19 16:43:01

是的。根据您想要选择的项目的性质,有多种方法可以实现这一点。

以下应该可以解决问题,

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/*" );
}

我没有时间完全测试它,但它应该给你带来正确的方向:-)

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 :-)

罪#恶を代价 2024-09-19 16:43:01

我会坚持使用 ID,但可能会将它们作为 XML 结构返回,而不是要解析的字符串。 XPathNodeIterator 方法可能会干扰页面编辑器中的内联编辑以及其他 XSL 扩展(例如生成 URL)。

另请参阅

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文