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