Sitecore XSL开发求助:获取指定项子项及关联字段值
Hey there! Let's work through this Sitecore XSLT migration together—since you're new to XSLT, I'll break down exactly what you need and fix the issues you're hitting.
First, let's start with a fully working XSLT snippet that pulls all the data you require, then I'll explain each part so you understand what's happening:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sc="http://www.sitecore.net/sc" exclude-result-prefixes="sc"> <xsl:template match="/"> <!-- Grab all child items under the Social Media Link Group --> <xsl:variable name="socialMediaParent" select="sc:item('/sitecore/content/Home/Social Media Link Group', .)" /> <xsl:variable name="socialMediaItems" select="$socialMediaParent/item" /> <div class="social-media-container"> <!-- Loop through each social media link item --> <xsl:for-each select="$socialMediaItems"> <!-- Resolve the Droplink field to its linked Sitecore item --> <xsl:variable name="sourceItemId" select="sc:field('Social Media Source Name', .)" /> <xsl:variable name="socialSourceItem" select="sc:item($sourceItemId, .)" /> <!-- Only render if the Droplink has a valid linked item --> <xsl:if test="$socialSourceItem"> <!-- Pull fields from the linked source item --> <xsl:variable name="sourceDisplayName" select="sc:field('Social Media Source Name', $socialSourceItem)" /> <xsl:variable name="sourceCssClass" select="sc:field('Social Media Source Css Class', $socialSourceItem)" /> <!-- Pull the Link field from the current social media item --> <xsl:variable name="socialLinkField" select="sc:field('Social Media Link', .)" /> <xsl:variable name="socialLinkUrl" select="sc:path($socialLinkField)" /> <!-- Render the final link element --> <a href="{$socialLinkUrl}" class="social-link {$sourceCssClass}" aria-label="{$sourceDisplayName}"> <!-- If you want an icon, you could add it here using the CSS class --> <span class="social-icon"></span> </a> </xsl:if> </xsl:for-each> </div> </xsl:template> </xsl:stylesheet>
Key Fixes & Explanations:
Sitecore XSL Namespace: I added
xmlns:sc="http://www.sitecore.net/sc"—this is mandatory to use Sitecore's built-in XSL helper functions (likesc:item()andsc:field()), which you were probably missing in your first attempt (causing syntax/function errors).Resolving Droplinks: Droplink fields in Sitecore store the ID of the linked item, not the field values directly. So we first grab the ID with
sc:field('Social Media Source Name', .), then usesc:item()to convert that ID into the actual Sitecore item we need to pull fields from. This is likely why your second snippet could loop but couldn't get field values—you weren't resolving the Droplink to its target item.Context Awareness: In the
<xsl:for-each>, the.refers to the current social media item in the loop. When pulling fields from the linked source item, we pass$socialSourceItemas the context tosc:field()instead of.—this tells the function to get the field from the linked item, not the current loop item.Error Prevention: The
<xsl:if test="$socialSourceItem">checks that the Droplink actually has a valid linked item, so we don't render broken links if the field is empty.
Common Mistakes to Avoid:
- Field Name Spelling: Sitecore field names are case-sensitive—double-check that your field names match exactly what's in Sitecore (e.g.,
Social Media Source Css Classvs.socialMediaSourceCssClass). - Missing Helper Functions: Always use Sitecore's
sc:*functions when working with Sitecore items/fields—plain XSL won't understand how Sitecore stores its data. - Context Confusion: Make sure you're targeting the right item when pulling fields—using
.inside the loop targets the current child item, while$socialSourceItemtargets the linked Droplink item.
内容的提问来源于stack exchange,提问作者Erica Stockwell-Alpert

