Search code examples
xsltumbraco

Umbraco current member details comma separated multiple textstrings list


I'm creating a macro that sources a property from the current member. The property uses the "multiple textstrings" datatype, and each textstring contains a media ID, the version number and the date and time added.

<xsl:output method="xml" omit-xml-declaration="yes"/>    
<xsl:param name="currentPage"/>
<xsl:template match="/">
    <xsl:if test="count(umbraco.library:GetCurrentMember()/assetDownloads/values/value) > 0">
        <ul>
            <xsl:for-each select="umbraco.library:GetCurrentMember()/assetDownloads/values/value">
                <li>
                    <xsl:if test="./preview != ''">
                        <img>
                            <xsl:attribute name="src">
                                <xsl:value-of select="./preview" />
                            </xsl:attribute>
                        </img>
                    </xsl:if>
                    <p><xsl:value-of select="current()"/></p>       
                </li>
            </xsl:for-each>
        </ul>
    </xsl:if>
</xsl:template>

This results in the following:

  • 1475,1,28-Dec-12 01:26:45

  • 1475,1,28-Dec-12 01:27:03

The first values are the media types ID, the second value is its version number and the third value is the date and time it was downloaded.

This is how to display the first value as the preview image and the nodeName:


<!-- First value as preview image and nodeName -->
<xsl:if test="position() = 1">
    <img>
        <xsl:attribute name="src">
            <xsl:value-of select="umbraco.library:GetMedia(., 0)/preview" />
        </xsl:attribute>
    </img>
    <p><xsl:value-of select="umbraco.library:GetMedia(., 0)/@nodeName" /></p>
</xsl:if>

How can I seperate the string so I can display details from the media type? I'd like to display the preview image from media type and the nodeName of the media type.


Solution

  • Try using Umbraco split function separated via , (Comma) as following

    <xsl:variable name="contentSplit" select="umbraco.library:Split(current(), ',')" />
    

    Refactored code:

    <xsl:if test="count(umbraco.library:GetCurrentMember()/assetDownloads/values/value) > 0">
      <ul>
        <xsl:for-each select="umbraco.library:GetCurrentMember()/assetDownloads/values/value">
          <li>
            <xsl:if test="./preview != ''">
              <img>
                <xsl:attribute name="src">
                  <xsl:value-of select="./preview" />
                </xsl:attribute>
              </img>
            </xsl:if>
            <p>
    
              <!-- Split values via , comma using below function -->
              <xsl:variable name="contentSplit" select="umbraco.library:Split(current(), ',')" />
    
              <xsl:for-each select="$contentSplit/value">
    
                <!-- First value as media type id -->
                <xsl:if test="position() = 1">
                  Media Type Id: <xsl:value-of select="." />
                </xsl:if>
    
                <!-- Second value as version number -->
                <xsl:if test="position() = 2">
                  Version: <xsl:value-of select="." />
                </xsl:if>
    
                <!-- Third value as date and time of download -->
                <xsl:if test="position() = 3">
                  Date Time: <xsl:value-of select="." />
                </xsl:if>
    
              </xsl:for-each>
    
            </p>       
          </li>
        </xsl:for-each>
      </ul>
    </xsl:if>
    

    Let me know if you need more explanation, Thanks