I. XSLT 1.0 ソリューションは次のとおりです (XSLT 2.0 ソリューションの方がはるかに簡単です)。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vLine" select=
"concat('**********',
'**********',
'**********',
'**********',
'**********',
'**********',
'**********',
'**********'
)
"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:variable name="vsplitResult">
<xsl:call-template name="split">
<xsl:with-param name="pText" select="translate(., '
', '')"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="vnumLines" select=
"ceiling(string-length($vsplitResult) div 81)"/>
<xsl:choose>
<xsl:when test="$vnumLines > 11">
<xsl:value-of select="substring($vsplitResult, 1, 81*11 -1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vremLines" select="11 - $vnumLines"/>
<xsl:value-of select="substring($vsplitResult, 1, 81*($vnumLines -1))"/>
<xsl:call-template name="padRight">
<xsl:with-param name="pText" select="substring($vsplitResult,81*($vnumLines -1)+1)"/>
</xsl:call-template>
<xsl:for-each select="(document('')//node())[not(position() > $vremLines)]">
<xsl:value-of select="concat('
', $vLine)"/>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
<xsl:template name="split">
<xsl:param name="pText" select="."/>
<xsl:param name="pLineLength" select="80"/>
<xsl:if test="$pText">
<xsl:value-of select="substring($pText, 1, $pLineLength)"/>
<xsl:if test="string-length($pText) > $pLineLength">
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring($pText, $pLineLength+1)"/>
<xsl:with-param name="pLineLength" select="$pLineLength"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="padRight">
<xsl:param name="pText"/>
<xsl:param name="pTotatLength" select="80"/>
<xsl:value-of select=
"concat($pText,
substring($vLine, 1, $pTotatLength - string-length($pText))
)"/>
</xsl:template>
</xsl:stylesheet>
この変換が提供された XML ドキュメントに適用されると、次のようになります。
<Description>
text text text text text text text text text text text text text
text text text text text text text text text text text text text text text
text text text text </Description>
必要な正しい結果が生成されます(*
正確に何が生成されるかを確認するために文字を使用しています):
<Description>text text text text text text text text text text text text texttext text text t
ext text text text text text text text text text text texttext text text text **
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************</Description>
Ⅱ.XSLT 2.0 ソリューション:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:param name="pLineLength" select="80" as="xs:integer"/>
<xsl:param name="pTotalLines" select="11" as="xs:integer"/>
<xsl:param name="pPadChar" select="'*'" as="xs:string"/>
<xsl:variable name="vLine" as="xs:string" select=
"string-join(
(for $i in 1 to $pLineLength
return $pPadChar
),
''
)
"/>
<xsl:template match="/*">
<xsl:variable name="vText" select="translate(., '
', '')"/>
<xsl:copy>
<xsl:value-of separator="
" select=
"(for $numlines in string-length($vText) idiv $pLineLength +1,
$line in 1 to $numlines
return
if($line ne $numlines)
then substring($vText,
1 + ($line -1)*$pLineLength,
$pLineLength)
else
for $lastLine in substring($vText,
1 + ($line -1)*$pLineLength,
$pLineLength)
return
concat($lastLine,
substring($vLine,
1,
$pLineLength - string-length($lastLine))
),
(for $numlines in string-length($vText) idiv $pLineLength +1,
$rem in 1 to $pTotalLines - $numlines
return
$vLine)
)
"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
この変換を同じ XML ドキュメント (上記) に適用すると、同じ正しい結果が生成されます。
<Description>text text text text text text text text text text text text texttext text text t
ext text text text text text text text text text text texttext text text text **
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************</Description>