2011-09-03 36 views
0

XML如何XSL標題

<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> 
+0

你是什麼意思的「添加段落」嗎?編輯您的帖子以顯示所需的輸出。 –

+0

XML 這是一條測試消息。這是一條測試消息。這是一條測試消息。 這是一條測試消息。這是一條測試消息。這是一條測試消息。 這是一條測試消息。這是一條測試消息。這是一條測試消息。 我需要編寫XSL爲上面的XML得到了這個樣子,所以動態我需要添加每個句子之間

標籤。

這是一條測試消息。

這是一條測試消息。

這是一條測試消息。

這是一條測試消息。

這是一條測試消息。

這是一條測試消息。

user926230

+0

由於&在那裏,您的XML不再有效。你的意思是使用&巧克力嗎? –

回答

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是無效的,因爲在那裏&的。我假設你的意思做&

如果你儘管找單獨的文本輸出,而不是HTML段落標記,你可以更換p的創作第一XSL元素:當,此代碼代替。

<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. 
+0

@_Tim C:如果你沒有錯過週期字符,這個解決方案會很好。 –

+0

我已經試過您的解決方案,您所提供,我還沒有得到輸出爲「

這是一封測試郵件

這是一封測試郵件

這是一封測試郵件

這是一封測試郵件

這是測試消息

這是測試消息

這是測試消息

這是測試消息

這是一條測試消息

「 – user926230

+0

你有目標身份證嗎? – user926230