Sitecore XSLHelper.fld Method not working as expec

2019-09-15 01:26发布

问题:

I'm having some troubles with XSLHelper.fld method. I have a very simple XSLT file that I am trying to access sub-values that are inside my Sitecore image item.

Code Sample:

<?xml version="1.0" encoding="UTF-8"?>

<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">

  TEST BACKGROUND IMAGE
  <br/>
  ALT: <br/>
  <xsl:value-of select="sc:fld('background',.,'alt')"/>
  <br/>
  SRC: <br/>
  <xsl:value-of select="sc:fld('background',.,'src')"/>
  <br/>
  Field SRC: <br/>
  <xsl:value-of select="sc:field('background',.,'src')"/>


</xsl:template>

</xsl:stylesheet>

When I test the following code, the results are strange as can be seen from the following screenshot:

You can clearly see that the alt field has a value and the src field is empty. So I definitely have access to my Sitecore image item but cannot get access to the src field.....

Can anyone shed some light on why the src field is empty?

Regards,

Comic Coder

回答1:

As I stated on your other question, there is no longer a src attribute in the raw value of image fields. The alt attribute exists if you modify the Alternate Text in the properties of the image field or it will fall back to the alt text on the image item in the media library.

The sc:field method uses a field renderer to output a full <img> tag. The third parameter to that method is supposed to be for additional parameters like width and height or additional attributes. It should be formatted as a query string (e.g. width=150&height=100). It is probably just ignoring the src value that you are passing because it isn't valid.