2011-07-21 62 views
2

我有一個包含格式化的文本數據的XML元素:XSLT - 輸出到HTML中包含格式化文本的特定XML元素 - 輸出應該是相同

<MESSAGE> 
     <TranslationReport> 
Translation Report 
================== 
Contains errors ? true 
Contains warnings ? false 
There are 9 entries in the report 

我希望我的XSLT的結果(輸出到html)以正確匹配TranslationReport的內容。 我所做的一切只需要一個數據(全部在一行中 - 見下文)。這似乎很容易,但我已經在我的所有書籍和其他地方搜索...

翻譯報告==================包含錯誤?是否包含 警告?假有報告

+0

我能夠產生你正在尋找一個直XSL轉換的結果,使用例如http://www.shell-tools.net/index.php?op=xslt你如何渲染結果? – Brabster

+0

+1好問題。 –

回答

1

9項您有試過<xsl:output method="text"/>tag和/或封閉在<xsl:text>...</xsl:text>tags的文字文本?

如果以HTML格式呈現結果,問題在於HTML不會在不包含tag like<pre>中的輸出的情況下渲染換行符。輸出一個<pre>標籤,將您的文本輸出包裝到您的XSLT中。

0

如果你要轉成HTML格式,你有兩種選擇:

  • 使用pre標籤準確地呈現在瀏覽器中的文本是。
  • 使用一些高級XPath 2.0函數解析文本,並根據需要處理每個文本行。

這裏的第一個選項傻例如:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="html"/> 

    <xsl:template match="MESSAGE/TranslationReport"> 
     <html> 
      <body> 
       <pre> 
        <xsl:value-of select="."/> 
       </pre> 
      </body> 
     </html> 
    </xsl:template> 

</xsl:stylesheet> 

在第二個選項,我們會分析你的文字與XPath 2.0中功能tokenize,所有分割線和包裝每一個與eanted標籤。

這是愚蠢的例子:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="html"/> 

    <xsl:template match="MESSAGE/TranslationReport"> 
     <html> 
      <body> 
       <xsl:for-each select="tokenize(.,'\n') 
        [not(position()=(1,last()))]"> 
        <p class="TranslationReport"> 
         <xsl:value-of select=".[position()]"/> 
        </p> 
       </xsl:for-each> 
      </body> 
     </html> 
    </xsl:template> 

</xsl:stylesheet> 

在第二種情況下,輸出將是:

<html> 
    <body> 
     <p class="TranslationReport">Translation Report</p> 
     <p class="TranslationReport">==================</p> 
     <p class="TranslationReport">Contains errors ? true</p> 
     <p class="TranslationReport">Contains warnings ? false</p> 
     <p class="TranslationReport">There are 9 entries in the report</p> 
    </body> 
</html>