如何xsl标题

时间:2011-09-03 02:28:15

标签: xslt

XML

<data>The production of opium itself has basically not changed since ancient times...Opium trade became more regular by the seventeenth century, when it was mixed with tobacco for smoking, and addiction was first recognized... This is a test Message3...This is showing off a handful of updates to its line of audio accessories this week at IFA in Berlin. At top of the list is the newly revealed inAir 5000, a hefty tabletop AirPlay speaker that the company is firmly positioning to take on Bowers&Wilkins' Zeppelin line (which also recently got its own AirPlay version)... Like that system, the inAir certainly offers a unique take on aesthetics, with a teardrop design. The company opted not to install an Apple dock on the 110 watt system, given that compatible devices can stream audio wirelessly to the thing via AirPlay...
</data>

我需要在xsl中为...之后的每个句子添加段落,并且段落必须超过1行。

XML

             I need to write XSL for the above xml to get the out like this.

自古以来,鸦片本身的生产基本没有改变。鸦片贸易在十七世纪变得更加规律,当时它与烟草混合吸烟,并首先认识到成瘾。这是测试Message3。

              <p>This is showing off a handful of updates to its line of audio accessories this week at IFA in Berlin. At top of the list is the newly revealed inAir 5000, a hefty tabletop AirPlay speaker that the company is firmly positioning to take on Bowers&Wilkins' Zeppelin line (which also recently got its own AirPlay version).</p>
              <p>Like that system, the inAir certainly offers a unique take on aesthetics, with a teardrop design. The company opted not to install an Apple dock on the 110 watt system, given that compatible devices can stream audio wirelessly to the thing via AirPlay.</p>

1 个答案:

答案 0 :(得分:1)

这是你在找什么?它使用递归模板查找文本中的句点。它将文本放在HTML段落标记的第一个句点中,然后在句点之后递归调用该文本的模板。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html"/>
   <xsl:template match="/data">
      <xsl:call-template name="paragraph">
         <xsl:with-param name="text" select="text()"/>
         <xsl:with-param name="separator" select="'...'"/>
         <xsl:with-param name="replace" select="'.'"/>
      </xsl:call-template>
   </xsl:template>
   <xsl:template name="paragraph">
      <xsl:param name="text"/>
      <xsl:param name="separator"/>
      <xsl:param name="replace"/>
      <xsl:choose>
         <xsl:when test="contains($text,$separator)">
            <xsl:variable name="firsttext" select="normalize-space(substring-before($text,$separator))"/>
            <xsl:if test="string-length($firsttext) &gt; 0">
               <p>
                  <xsl:value-of select="$firsttext"/>
                  <xsl:value-of select="$replace"/>
               </p>
            </xsl:if>
            <xsl:call-template name="paragraph">
               <xsl:with-param name="text" select="normalize-space(substring-after($text,$separator))"/>
               <xsl:with-param name="separator" select="$separator"/>
               <xsl:with-param name="replace" select="$replace"/>
            </xsl:call-template>
         </xsl:when>
         <xsl:when test="string-length($text) &gt; 0">
            <xsl:value-of select="normalize-space($text)"/>
            <xsl:value-of select="$replace"/>
         </xsl:when>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

这会产生以下输出:

<p>This is showing off a handful of updates to its line of audio accessories this week at IFA in Berlin. At top of the list is the newly revealed inAir 5000, a hefty tabletop AirPlay speaker that the company is firmly positioning to take on Bowers&amp;Wilkins' Zeppelin line (which also recently got its own AirPlay version).</p>
<p>Like that system, the inAir certainly offers a unique take on aesthetics, with a teardrop design. The company opted not to install an Apple dock on the 110 watt system, given that compatible devices can stream audio wirelessly to the thing via AirPlay.</p>

请注意,您当前的输入XML无效,因为&amp;在那里。我认为你的意思是&amp;

如果您只查找文本输出,而不是HTML段落标记,则可以使用此替换第一个 xsl:when p 元素的创建而不是代码。

   <xsl:value-of select="$firsttext" />
   <xsl:value-of select="$replace"/>
   <xsl:text>&#13;</xsl:text>
   <xsl:text>&#13;</xsl:text>

执行此操作时,输出如下:

This is showing off a handful of updates to its line of audio accessories this week at IFA in Berlin. At top of the list is the newly revealed inAir 5000, a hefty tabletop AirPlay speaker that the company is firmly positioning to take on Bowers&amp;Wilkins' Zeppelin line (which also recently got its own AirPlay version).

Like that system, the inAir certainly offers a unique take on aesthetics, with a teardrop design. The company opted not to install an Apple dock on the 110 watt system, given that compatible devices can stream audio wirelessly to the thing via AirPlay.