gpt4 book ai didi

string - 如何在 XQuery 中的一定数量的字符串字符后插入一个字符串?

转载 作者:行者123 更新时间:2023-12-04 05:33:00 25 4
gpt4 key购买 nike

我想在 25 个字符后插入一个字符串(在本例中为换行符 \L),但 只有在下一个可用的空白处,为了避免拆分如下单词:

This is the example sente\L nce for you.

正确的输出应该是这样的:
This is the example sentence\L for you.

换行符应该出现在每行大约 25 个字符之后,因此更长的示例如下所示:
This is a longer example\L
for you; it actually contains\L
more than 50 characters.

在 XQuery 中实现它的最简单方法是什么?

最佳答案

这是一个 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&#xA;')"/>
</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

关于string - 如何在 XQuery 中的一定数量的字符串字符后插入一个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12349793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com