1

I am using Visual Studio 2015. I want to find the smallest number in the Comma separated list using XSLT.

<EndUnitizeDate>2020-07-13T15:01:43</EndUnitizeDate>
<InternalRecNum>12,3,44,55,66</InternalRecNum>
<LaunchNum>0</LaunchNum> <LeadingSts>900</LeadingSts>

I used tokenize for splitting but I am getting 'tokenize()' is an unknown XSLT function Error.

<xsl:variable name="smallValue" select="s0:WMWDATA/s0:WMFWUpload/s0:Receipts/s0:Receipt/s0:InternalRecNum/text()" />
<xsl:variable name="tokenizedLine" select="tokenize($smallValue, ',')" />
<xsl:for-each select="$tokenizedLine">
<xsl:sort select="." order="descending" />
<xsl:if test="position() = last()">
Smallest: <xsl:value-of select="." />
</xsl:if>
</xsl:for-each>

enter image description here

Coder Ram
  • 11
  • 2

2 Answers2

3

The tokenize() function requires an XSLT 2.0 processor.

Some XSLT 1.0 processors support tokenizing via en extension function. In pure XSLT 1.0, you need to use a recursive named template.

Here's an example of a template that will both tokenize the input AND find the smallest token:

<xsl:template name="min-token">
    <xsl:param name="input"/>
    <xsl:param name="prev-min"/>
    <xsl:param name="delimiter" select="','"/>
        <xsl:variable name="token" select="substring-before(concat($input, $delimiter), $delimiter)" />
        <xsl:variable name="min">
            <xsl:choose>
                <xsl:when test="not($prev-min) or $token &lt; $prev-min">
                    <xsl:value-of select="$token"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$prev-min"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="contains($input, $delimiter)">
                <!-- recursive call -->
                <xsl:call-template name="min-token">
                    <xsl:with-param name="input" select="substring-after($input, $delimiter)"/>
                    <xsl:with-param name="prev-min" select="$min"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$min"/>
            </xsl:otherwise>
        </xsl:choose>
</xsl:template>

Demo: https://xsltfiddle.liberty-development.net/aiynfe

michael.hor257k
  • 113,275
  • 6
  • 33
  • 51
0

If functions like tokenize() are more important to you than using Visual Studio, then this question describes alternatives you could consider:

https://stackoverflow.com/questions/11205268/how-to-use-xslt-2-0-in-visual-studio-2010`

Michael Kay
  • 156,231
  • 11
  • 92
  • 164