2011-07-21 21 views
1

我試圖用XML創建位置文件。我創建了XSLT並且工作正常,除非該字段填充空格。在這種情況下,XSL只返回一個空格。我正在使用MSXML(6.0)。將XML轉換爲位置文本文件時沒有剝離空間的幫助

我都試過,沒有運氣以下:

<xsl:strip-space elements="*"/> 
<xsl:preserve-space elements="*"/> 

<fo:block white-space-collapse="false" white-space-treatment="preserve" > 
    <!-- Code here --> 
</fo:block> 

這裏是XML輸入時,XSLT和輸出。

<Document> 
     <Header> 
     <Title>Long life to the queen </Title> 
     <Author>Sam Catnip  </Author> 
     <Year>1996</Year> 
     <Edition> 1</Edition> 
     <Price>   12.99</Price> 
     <Pages> 1244</Pages> 
     <AuthorNotes>     </AuthorNotes> 
     <Abstract>It is a great book </Abstract> 
    </Header> 
     <Header> 
     <Title>Life and live longer  </Title> 
     <Author>Bill Griffin </Author> 
     <Year>2001</Year> 
     <Edition> 1</Edition> 
     <Price>   2.99</Price> 
     <Pages>  44</Pages> 
     <AuthorNotes>Yeah, right   </AuthorNotes> 
     <Abstract>Wishfull thinking </Abstract> 
    </Header> 
</Document> 

XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
    <xsl:output method="text"/> 
    <xsl:template match="//Document"> 
     <xsl:for-each select="./Header"> 
      <xsl:value-of select="./Title"/> 
      <xsl:value-of select="./Author"/> 
      <xsl:value-of select="./Year"/> 
      <xsl:value-of select="./Edition"/> 
      <xsl:value-of select="./Price"/>   
      <xsl:value-of select="./Pages"/> 
      <xsl:value-of select="./AuthorNotes"/> 
      <xsl:value-of select="./Abstract"/> 
      <xsl:text>&#13;&#10;</xsl:text> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

Long life to the queen Sam Catnip  1996 1   12.99 1244It is a great book 
Life and live longer  Bill Griffin 2001 1   2.99  44Yeah, right   Wishfull thinking  

當它應該是:

Long life to the queen Sam Catnip  1996 1   12.99 1244     It is a great book 
Life and live longer  Bill Griffin 2001 1   2.99  44Yeah, right   Wishfull thinking  

我會很感激,關於如何解決這個任何想法。

感謝,

附庸風雅

+0

您的模板生成所需的輸出(MSXML 6.0) –

回答

2

即使你XSLT包含保留空白的指令,它可能沒有當你輸入XML文檔傳遞給你的XSLT解析之前保存。

你沒有指定你使用什麼語言/平臺或任何東西,所以很難提供一個具體的解決方案,但我知道在C#如果你看過像這樣的XML文檔:

string xmlSource = @"<Document>etc..</Document>"; 
XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xmlSource); 

它已經將任何只有空格的元素視爲空元素,並且在嘗試應用樣式表之前已經清除了該空間。

在C#中,你需要這樣做:

XmlDocument doc = new XmlDocument { PreserveWhitespace = true }; 

時,你甚至加載之前實例化。如果您使用的是其他平臺,則需要研究您的平臺如何執行此操作。

一個更通用的解決方案(雖然繁瑣一點點)是修改你的輸入這樣的XML:

... 
    <AuthorNotes xml:space="preserve">     </AuthorNotes> 
... 

我想你可以把這個根元素,但我不是100%肯定的那。

+0

感謝Flynn, 它確實與根元素中的<... xml:space =「preserve」>一起工作。 – Arty