Substring, string length function in xslt

Basically, I have a line created from a loop separated by commas, for example. A, B, C, I want to get rid of the last comma.

<xsl:variable name="myConcatString">
   <xsl:for-each select="valueinElement">
        <xsl:value-of select="@attributeValue"/>,
   </xsl:for-each>
</xsl:variable>


<xsl:variable name="valueLength" select="string-length($myConcatString)-1"/>
<xsl:value-of select="substring($myConcatString,1,$valueLength)"/>

Now the last line should give me A, B, C without the "," in the last. Can someone tell me what is going wrong?

+3
source share
2 answers

You display spaces due to the way XML is formatted. You can fix this in two ways. One of them is simply to remove the formatting:

<xsl:for-each select="valueinElement"><xsl:value-of select="@attributeValue"/>,</xsl:for-each>

Another, more reliable way is to change the way you handle spaces:

<xsl:for-each select="valueinElement">
  <xsl:value-of select="@attributeValue"/>
  <xsl:text>,</xsl:text>
</xsl:for-each>

, , - , , for-each .

XSLT XML:

<root>
  <valueinElement attributeValue="dogs"/>
  <valueinElement attributeValue="cats"/>
  <valueinElement attributeValue="mice"/>
  <valueinElement attributeValue="lasers"/>
  <valueinElement attributeValue="frogs"/>
</root>

:

dogs,cats,mice,lasers,frogs
+7

, .

<xsl:value-of select="substring($myConcatString,1,$valueLength)"/>

<xsl:value-of select="substring($myConcatString,0,$valueLength)"/>
+3

Source: https://habr.com/ru/post/1710753/


All Articles