How to limit the number of words in XSLT?

醉酒当歌 提交于 2020-07-10 10:25:40

问题


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?


回答1:


Try (in 3.0 with expand-text enabled):

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


来源:https://stackoverflow.com/questions/62774279/how-to-limit-the-number-of-words-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!