这是一个 XSLT 2.0 解决方案——只需将其转换为 XQuery:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:value-of select="my:splitAtWords(/*, 25, '\L
')"/>
</xsl:template>
<xsl:function name="my:splitAtWords" as="xs:string?">
<xsl:param name="pText" as="xs:string?"/>
<xsl:param name="pMaxLen" as="xs:integer"/>
<xsl:param name="pRep" as="xs:string"/>
<xsl:sequence select=
"if($pText)
then
(for $line in replace($pText, concat('(^.{1,', $pMaxLen,'})\W.*'), '$1')
return
concat($line, $pRep,
my:splitAtWords(substring-after($pText,$line),$pMaxLen,$pRep))
)
else ()
"/>
</xsl:function>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<t>This is a longer example for you; it actually contains more than 50 characters.</t>
产生了想要的结果:
This is a longer example\L
for you; it actually\L
contains more than 50\L
characters\L
.\L