【发布时间】:2016-12-21 14:28:55
【问题描述】:
我是 XSLT 的新手,我正在尝试编辑一些代码来设置 Sitecore 站点的面包屑导航。现在,它被设置为获取菜单标题字段的值并将其用于面包屑导航。但是,有些模板没有此字段,因此我希望在这些情况下可以选择获取页面标题字段的值。
我可以用 C# 做这种事情,但 XSLT 完全不同。有什么建议么?
这是完整的 XSLT 文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--=============================================================
File: BreadCrumb.xslt
Copyright notice at bottom of file
==============================================================-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sc="http://www.sitecore.net/sc" xmlns:dot="http://www.sitecore.net/dot" exclude-result-prefixes="dot sc">
<!-- output directives -->
<xsl:output method="html" indent="no" encoding="UTF-8" />
<!-- parameters -->
<xsl:param name="lang" select="'en'" />
<xsl:param name="id" select="''" />
<xsl:param name="sc_item" />
<xsl:param name="sc_currentitem" />
<!-- variables -->
<!-- Uncomment one of the following lines if you need a "home" variable in you code -->
<!--<xsl:variable name="home" select="sc:item('/sitecore/content/home',.)" />-->
<!--<xsl:variable name="home" select="/*/item[@key='content']/item[@key='home']" />-->
<!--<xsl:variable name="home" select="$sc_currentitem/ancestor-or-self::item[@template='site root']" />-->
<!-- entry point -->
<xsl:template match="*">
<xsl:apply-templates select="$sc_item" mode="main" />
</xsl:template>
<!--==============================================================-->
<!-- main-->
<xsl:template match="*" mode="main">
<!--==============================================================-->
<div class="bread-crumb-box" style="margin-left:auto; margin-right:auto; margin-top: -25px;">
<div class="container" style="width:100%">
<div class="row">
<small style="font-size:12px; float:left">
<a href="/">Home</a>
<xsl:for-each select="ancestor-or-self::item">
<xsl:if test="position() > 2 and @template != 'folder' and @template != 'blogtypefolder'">
<xsl:choose>
<xsl:when test="position() != last()">
<sc:link class="white">
<xsl:call-template name="GetNavTitle" />
</sc:link>
</xsl:when>
<xsl:otherwise>
<!--<strong>-->
<xsl:call-template name="GetNavTitle" />
<!--</strong>-->
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() != last()">
>
</xsl:if>
</xsl:if>
</xsl:for-each>
</small>
</div>
</div>
</div>
</xsl:template>
<!--This part is what needs to be adjusted, I believe-->
<xsl:template name="GetNavTitle">
<xsl:param name="item" select="." />
<xsl:choose>
<xsl:when test="sc:fld( 'navtitle', $item )">
<xsl:value-of select="sc:fld( 'Menu Title', $item )" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="holder" select="$item/@id" />
<!--<xsl:value-of select="$item/@name" />-->
<xsl:value-of select="sc:fld('Menu Title', sc:item($holder,.))" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
重构为 c# ;)
-
嗯。 Sitecore 社区的其他成员就像 5 年前一样放弃了 XSLT。或者更多。一个建议是坚持使用 c# 而不是尝试学习 XSLT。