XSLT以固定宽度线分割文本

时间:2012-01-18 11:38:38

标签: xslt

我有一个我想用XSLT处理的文本。 现在我的所有文本被截断为40个字符,如下所示:

<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 &lt; $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>

有了这个,我将输入分成指定长度的小字符串,这就是我想要实现的目标。

有没有更简单的解决方案?

1 个答案:

答案 0 :(得分:1)

我使用过这样的东西

    <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 &lt; $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>

有了这个,我将输入分成指定长度的小字符串,这就是我想要实现的目标。