gorn
gorn

Reputation: 8177

Xslt selecting remaining tokens in tokenized array

In my Xsl document I need to create a link with text containing line break, creating two sentences.

I'm using tokenize to add words by splitting on the whitespace character.

<xsl:variable name="tokens" select="tokenize($sentece, '\s+')"></xsl:variable>

The first sentence will contain three words, by simply selecting $tokens[1] $tokens[2] $tokens[3] <br />

Now, how do I select the arbitrary amount of remaining words in the tokenize array? Is it possible to do a for-loop on the $tokens array?

Upvotes: 1

Views: 1026

Answers (1)

davmos
davmos

Reputation: 9577

You can do a for-each and check position() > 3 like this...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="sentance">The first sentance. The second...</xsl:variable>
    <xsl:variable name="tokens" select="tokenize($sentance, '\s+')"/>

    <xsl:for-each select="$tokens">
      <xsl:if test="position() > 3">

        <xsl:value-of select="."/>

        <xsl:if test="position() != last()">
          <xsl:text> </xsl:text>
        </xsl:if>
      </xsl:if>
    </xsl:for-each>

  </xsl:template>
</xsl:stylesheet>

Output...

The second...

Upvotes: 1

Related Questions