Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove last comma in XSLT

Tags:

xslt

xslt-1.0

using XSLT 1.0.

<xsl:for-each select="*">
    <xsl:variable name="xxxx" select="@name" />
    <xsl:if test="../../../../fieldMap/field[@name=$xxxx]">...
        <xsl:if test="position() != last()">////this is not work correctly as last() number is actual last value of for loop and position() is based on if condition.
            <xsl:text>,</xsl:text>
        </xsl:if>
    </xsl:if>
</xsl:for-each>

can you suggest me how can i remove last ',' here ?

like image 848
dsi Avatar asked Dec 21 '22 04:12

dsi


1 Answers

position() and last() should be based on the loop, and not the xsl:if. What I think you are saying is that you are actually wanting to check if this is the last element for which the xsl:if is true, as such an element may not actually be the last element in the loop.

I would suggest combining your xsl:for-each and xsl:if into one, and selecting only those elements for which the condition is true. That way you should then be able to check the position in the way you were expecting

<xsl:for-each select="*[@name = ../../../../fieldMap/field/@name]">

    <xsl:if test="position() != last()">
         <xsl:text>,</xsl:text>
    </xsl:if>
</xsl:for-each>
like image 124
Tim C Avatar answered Jan 11 '23 15:01

Tim C