2

以下のような単語の分割を避けるために\L、文字列の25文字の後に文字列(この場合は改行)を挿入しますが、次に使用可能な空白にのみ挿入します。

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でこれを実装する最も簡単な方法は何でしょうか?

4

2 に答える 2

2

これがXSLT2.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
于 2012-09-10T13:40:43.803 に答える
1

私はここで提案された解決策を使用することになりました:

let $text := 'This is a longer example for you; it actually contains more than 50 characters.'
let $text-output := replace(concat($text,' '),'(.{0,25}) ','$1\\L')
return $text-output

これは、上記の@dimitre-novatchevからのXSLTと同じ結果を返します。

This is a longer example\L
for you; it actually\L
contains more than 50\L
characters.\L
于 2012-09-10T14:47:44.037 に答える