Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hamish 96 posts 154 karma points
    Oct 25, 2009 @ 01:27
    Hamish
    1

    How to get value at a position from a split result

    Hello everybody :)

    I want to test each value from a split string separated by a coma. How do i grab the value at a position in the split ie what is the value of split variable at position 2?

    I'm currently trying to test for related articles by tags. If there are 2 or more articles found then show list of articles otherwise show nothing.

    The code snippet is below with necessary information. The ???????? is the code bit that I'm unsure of how to write?

        <xsl:param name="tags" select="umbraco.library:Split($currentPage/data[@alias='tags'],',')"/>
        <xsl:template match="/">
            <xsl:call-template name="startModule">
                <xsl:with-param name="tagNum" select="count($tags/value)"/>
                <xsl:with-param name="count" select="1"/>
            </xsl:call-template>
        </xsl:template>
        <xsl:template name="startModule">
            <xsl:param name="count"/>
            <xsl:param name="tagNum"/>
            <xsl:variable name="tagValue" select="?????????"/>
            <xsl:variable name="tagArticleList" select="$currentPage/ancestor-or-self::node[@level='1']//node [@nodeTypeAlias = 'Blog Article' and contains(data[@alias='tags'],$tagValue)]"/>
            <xsl:variable name="tagArticleListNum" select="count($tagArticleList)"/>
            <xsl:choose>
                <xsl:when test="$tagArticleListNum &gt; 1">
                    <xsl:call-template name="module"/>
                </xsl:when>
                <xsl:when test="$count &lt; $tagNum">
                    <xsl:call-template name="startModule">
                        <xsl:with-param name="count" select="$count + 1"/>
                        <xsl:with-param name="tagNum" select="$tagNum"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise></xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        <xsl:template name="module">
            ....
        </xsl:template>
  • dandrayne 1138 posts 2262 karma points
    Oct 25, 2009 @ 18:40
    dandrayne
    2

    If you split the CSV using umbraco.library:Split then for-each through the values, you can test position() to find the current "tag number"

    <xsl:variable name="contentToSplit" select="$currentPage/data[@alias='COMMASEPARATEDVALUE']"/>
    <xsl:variable name="contentSplit" select="umbraco.library:Split($contentToSplit, ',')" />

    <!-- loop through each item -->
    <xsl:for-each select="$contentSplit/value">
    <xsl:variable name="contentSplitItem" select="." />
    <xsl:if test="position() = 2">
    <!-- second in list -->
    </xsl:if>
    </xsl:for-each>

    <!-- are there more than 2 -->
    <xsl:if test="count($contentSplit/value) &gt; 2">

    </xsl:if>

     

    Hope this helps,
    Dan

  • Chris Koiak 700 posts 2626 karma points
    Oct 25, 2009 @ 19:08
    Chris Koiak
    1

    Hi,

    Dan's approach is great.

    Just wanted to say, if you're ever stuck wondering what XML is returned from an extension I would recommend putting it into a textarea on screen. This usually removes any guess work or mystery.

    For example

    <xsl:variable name="contentSplit" select="umbraco.library:Split($contentToSplit, ',')" />
    <textarea cols="20" rows="20">
    <xsl:copy-of select="$contentSplit"/>
    </textarea>

    Cheers,

    Chris

    
    
                
  • Petr Snobelt 923 posts 1535 karma points
    Oct 25, 2009 @ 19:53
    Petr Snobelt
    0

    Hi.

    Dan's approach works well, but if you want only second  item you should use

    <xsl:value-of select="contentSplit/value[2]" />

    It has better performance

  • Hamish 96 posts 154 karma points
    Oct 26, 2009 @ 20:54
    Hamish
    0

    Wow - thanks Dan, Chris and Petr for your responses - 2 solutions and 1 technique for developing/bug fixing - totally awesome....

    Just to put an answer into the loop of things, I've updated the snippet of code above from:

    <xsl:variable name="tagValue" select="?????????"/>

    to:

     

    <xsl:variable name="tagValue" select="$tags/value[$count]"/>

    and it works like a charm :)

Please Sign in or register to post replies

Write your reply to:

Draft