XSLT - 在 2 个 Sitecore 字段中选择一个
XSLT - Choose between one of 2 Sitecore fields
我对 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>
您的 XSLT 需要更改为:
<xsl:choose>
<xsl:when test="sc:fld( 'Menu Title', $item ) != '' ">
<xsl:value-of select="sc:fld( 'Menu Title', $item )" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="sc:fld('Page Title', $item )" />
</xsl:otherwise>
</xsl:choose>
我对 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>
您的 XSLT 需要更改为:
<xsl:choose>
<xsl:when test="sc:fld( 'Menu Title', $item ) != '' ">
<xsl:value-of select="sc:fld( 'Menu Title', $item )" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="sc:fld('Page Title', $item )" />
</xsl:otherwise>
</xsl:choose>