问题
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