2014-04-14 21 views
2

我的XML文件的工作下一行生成PDF換行處理= 「保留」 不通過XSL-FO

<entries> 
<entry ID="93" ENTRY_TYPE="Text1" ENTRYNM="First line: 
    Second line 
    third line 
    fourth line" ENTRY_DT="12-Jan-2004"/></entries> 

我的XSL-FO

<fo:block linefeed-treatment="preserve" white-space-treatment='preserve' 
     white-space-collapse='false'> 
<xsl:value-of select="./entries/entry/@ENTRYNM"/> 
</fo:block> 

我生成一個包含ENTRYNM PDF哪些應該保留下一行,如xml所示。

Like example: 
First line: 
Second line 
third line 
fourth line 

回答

6

這是因爲Attribute Value Normalization。換行符正在被標準化爲空格。保留這些的唯一方法是在屬性值中使用字符引用。

舉例來說,如果你有這樣的XML:

<entry ID="93" ENTRY_TYPE="Text1" ENTRYNM="First line: 
    Second line 
    third line 
    fourth line" ENTRY_DT="12-Jan-2004"/> 

這XSLT(略XSL-FO的命名空間爲簡潔):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/*">   
     <block linefeed-treatment="preserve"> 
      <xsl:value-of select="@ENTRYNM"/>    
     </block> 
    </xsl:template> 

</xsl:stylesheet> 

你會得到這樣的輸出(標準化):

<block linefeed-treatment="preserve">First line:  Second line  third line  fourth line</block> 

如果在輸入中將換行符更改爲字符引用:

<entry ID="93" ENTRY_TYPE="Text1" ENTRYNM="First line:&#xA; 
    Second line&#xA; 
    third line&#xA; 
    fourth line" ENTRY_DT="12-Jan-2004"/> 

相同XSLT現在產生這樣的輸出:

<block linefeed-treatment="preserve">First line: 
    Second line 
    third line 
    fourth line</block> 

這裏是正常化的另一個視覺例子...

如果我們採取的第一個XML輸入例如:

<entry ID="93" ENTRY_TYPE="Text1" ENTRYNM="First line: 
    Second line 
    third line 
    fourth line" ENTRY_DT="12-Jan-2004"/> 

並嘗試基於&#xA;

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/*">   
     <block linefeed-treatment="preserve"> 
      <xsl:for-each select="tokenize(@ENTRYNM,'&#xA;')"> 
       <token><xsl:value-of select="."/></token> 
      </xsl:for-each> 
     </block> 
    </xsl:template> 

</xsl:stylesheet> 

我們在輸出得到一個單一的token

<block linefeed-treatment="preserve"> 
    <token>First line:  Second line  third line  fourth line</token> 
</block> 

如果我們使用第二XML輸入例如(與&#xA;引用取代了中斷),我們得到4個獨立的token S:

<block linefeed-treatment="preserve"> 
    <token>First line:</token> 
    <token>  Second line</token> 
    <token>  third line</token> 
    <token>  fourth line</token> 
</block>