1
我有一段要使用XSLT处理的文本。 现在我所有的文字被截断为40个字符,像这样:XSLT在固定宽度的行中拆分文本
<xsl:call-template name="justify">
<xsl:with-param name="value" select="comment"/>
<xsl:with-param name="width" select="40"/>
<xsl:with-param name="align" select=" 'center' "/>
</xsl:call-template>
但现在我想上显示多行的整个文本,每40个字符。 这是如何实现的? 安吉拉
这是我使用的模板:
<xsl:template name="justify">
<xsl:param name="value" />
<xsl:param name="width" select="10"/>
<xsl:param name="align" select=" 'left' "/>
<xsl:variable name="output" select="substring($value,1,$width)"/>
<xsl:choose>
<xsl:when test="$align = 'center'">
<xsl:call-template name="dup">
<xsl:with-param name="input" select=" ' ' "/>
<xsl:with-param name="count"
select="floor(($width - string-length($output)) div 2)"/>
</xsl:call-template>
<xsl:value-of select="$output"/>
<xsl:call-template name="dup">
<xsl:with-param name="input" select=" ' ' "/>
<xsl:with-param name="count"
select="ceiling(($width - string-length($output)) div 2)"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="dup">
<xsl:param name="input"/>
<xsl:param name="count" select="1"/>
<xsl:choose>
<xsl:when test="not($count) or not($input)"/>
<xsl:when test="$count = 1">
<xsl:value-of select="$input"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="$count mod 2">
<xsl:value-of select="$input"/>
</xsl:if>
<xsl:call-template name="dup">
<xsl:with-param name="input"
select="concat($input,$input)"/>
<xsl:with-param name="count"
select="floor($count div 2)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
和XML是非常基本的:
<Receipt>
<store>
<name>This is my new store</name>
<storeID>10000</storeID>
<addressline />
</store>
<transaction>
<!-- some other nodes hier-->
</transaction>
<comment>Here comes a very long comment that needs to be displayed on many lines, 40 chars each.</comment>
</Receipt>
我已经使用了这样的事情
<xsl:template name="for">
<xsl:param name="value"/>
<xsl:param name="start">1</xsl:param>
<xsl:param name="stop"/>
<xsl:param name="step">40</xsl:param>
<xsl:text/>
<xsl:if test="$start < $stop">
<xsl:call-template name="justify">
<xsl:with-param name="value" select="substring($value,$start,$step)"/>
<xsl:with-param name="width" select="40"/>
<xsl:with-param name="align" select=" 'center' "/>
</xsl:call-template>
<xsl:text>
</xsl:text>
<xsl:call-template name="for">
<xsl:with-param name="value">
<xsl:value-of select="$value"/>
</xsl:with-param>
<xsl:with-param name="stop">
<xsl:value-of select="$stop"/>
</xsl:with-param>
<xsl:with-param name="start">
<xsl:value-of select="$start + $step"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
有了这个,我将我的输入分割成指定长度的小字符串,这就是我想要的前夕。
有没有更简单的解决方案?
您尚未提供输入XML或足够的XSLT以回答此问题。请更新它。 – ColinE 2012-01-18 11:43:58
那么'justify'和'dup'模板的预期用途是什么?他们在现实中做了什么? – 2012-01-18 13:16:26
它们用于将每个字符串值截断为40个字符的文本,并根据$ align参数将其与40个字符空间对齐。 (我有一些代码左,右对齐不只是对齐)。但现在我需要从注释节点读取值并将其解析为40个字符块,并将它们分别显示在一个新行中。 – 2012-01-18 13:22:50