2012-01-18 18 views
1

XSLTを使用して処理したいテキストがあります。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の文字とそれぞれの希望: は今、すべての私のテキストは、このように、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

この質問に答えるために入力XMLまたは十分なXSLTを提供していません。それを更新してください。 – ColinE

+0

'justify'と' dup'テンプレートの意図した目的は何ですか?そして彼らは実際に何をしていますか? –

+0

これらは、各文字列の値をテキストに切り捨てて40文字にし、$ alignパラメータに従って40文字のスペースに並べ替えるために使用されます。 (私は左揃えのためのいくつかのコードを持って、右揃えだけではない)。しかし今は、コメントノードから値を読み取り、それを40文字のチャンクに解析し、それぞれを新しい行に表示する必要があります。 –

答えて

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> 

のようなものを使用しています。

関連する問題