Kaustubh Badrike
Kaustubh Badrike

Reputation: 565

How to limit the number of words in XSLT?

Given the following XML document:

<books>
  <book>
   <name>The problem of the ages</name>
  </book>
  <book>
   <name>Filtering the tap</name>
  </book>
  <book>
   <name>Legend of Atlantis</name>
  </book>
</books>

I want to take at most 2 words from the name of each book. Words can be assumed as being sequences of whitespace-separated characters. Example of output:

<library>
  <record>The problem</record>
  <record>Filtering the</record>
  <record>Legend of</record>
</library>

How would I achieve this using a single XSLT?

Upvotes: 0

Views: 91

Answers (1)

Michael Kay
Michael Kay

Reputation: 163458

Try (in 3.0 with expand-text enabled):

<xsl:template match="book/name">
  <record>{tokenize(.) => subsequence(1, 2)}</record>
</xsl:template>

Upvotes: 1

Related Questions