2013-10-04 71 views
0

我有數據來這樣保留在數據空間進來XSLT

<Table1> 
<row_description>Low Touch</row_description> 
<_x0038_ /> 
<_x0039_ /> 
<_x0031_0 /> 
<_x0031_1 /> 
<_x0031_2 /> 
</Table1> 
<Table1> 
<row_description> DMA/ALGO</row_description> 
<_x0038_ /> 
<_x0039_ /> 
<_x0031_0 /> 
<_x0031_1 /> 
<_x0031_2 /> 
</Table1> 
<Table1> 
<row_description> PT</row_description> 
<_x0038_ /> 
<_x0039_ /> 
<_x0031_0 /> 
<_x0031_1 /> 
<_x0031_2 /> 
</Table1> 

和我的XSLT做到這一點

<xsl:for-each select="*"> 
    <TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
     <xsl:if test="position()=1"> 
      <Paragraph> 
       <Span> 
        <Run> 
         <xsl:value-of select="text()" /> 
        </Run> 
       </Span> 
      </Paragraph> 
     </xsl:if> 
     <xsl:if test="position()>1"> 
      <Paragraph TextAlignment="Right"> 
       <Span> 
        <Run> 
         <xsl:value-of select="text()"/> 
        </Run> 
       </Span> 
      </Paragraph> 
     </xsl:if> 
    </TableCell> 
</xsl:for-each> 

,但它是我的文字PT之前移除空間來PT。如何保留數據中的空間?

回答

1

我OP,我這樣做是爲了解決我的問題

<Run xml:space="preserve"><xsl:value-of select="text()" /></Run> 

注:xml:space="preserve"後,請務必<xsl:value-of select="text()" />是在同一直線上。如果我們這樣寫的話

<Run xml:space="preserve"> 
    <xsl:value-of select="text()" /> 
</Run> 

它也會將xsl的縮進視爲文本,正如我們所說的保留空間一樣。

2

在你的XSLT使用:

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

參見:http://www.w3schools.com/xsl/el_preserve-space.asp


當我有以下輸入XML:

<?xml version="1.0" encoding="UTF-8"?> 
<data> 
    <Table1> 
     <row_description>Low Touch</row_description> 
     <_x0038_/> 
     <_x0039_/> 
     <_x0031_0/> 
     <_x0031_1/> 
     <_x0031_2/> 
    </Table1> 
    <Table1> 
     <row_description> DMA/ALGO</row_description> 
     <_x0038_/> 
     <_x0039_/> 
     <_x0031_0/> 
     <_x0031_1/> 
     <_x0031_2/> 
    </Table1> 
    <Table1> 
     <row_description> PT</row_description> 
     <_x0038_/> 
     <_x0039_/> 
     <_x0031_0/> 
     <_x0031_1/> 
     <_x0031_2/> 
    </Table1> 
</data> 

,而且我用下面的XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" encoding="UTF-8" indent="yes"/> 
    <xsl:preserve-space elements="*" /> 

    <xsl:template match="@*|node()"> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:template> 

    <xsl:template match="Table1"> 
     <xsl:for-each select="*"> 
      <TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
       <xsl:if test="position()=1"> 
        <Paragraph> 
         <Span> 
          <Run> 
           <xsl:value-of select="text()" /> 
          </Run> 
         </Span> 
        </Paragraph> 
       </xsl:if> 
       <xsl:if test="position()>1"> 
        <Paragraph TextAlignment="Right"> 
         <Span> 
          <Run> 
           <xsl:value-of select="text()"/> 
          </Run> 
         </Span> 
        </Paragraph> 
       </xsl:if> 
      </TableCell> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

輸出顯示預期:

<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph><Span><Run>Low Touch</Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph><Span><Run> DMA/ALGO</Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph><Span><Run> PT</Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 
<TableCell Padding="10,1" BorderThickness="1,1,1,1" BorderBrush="#FF888888"> 
    <Paragraph TextAlignment="Right"><Span><Run></Run> 
     </Span></Paragraph> 
</TableCell> 

<Run> PT</Run>仍然有空間在裏面。由於XSLT處理器我使用了Altova XMLSpy。

+0

沒有。問題是一樣的。我在這裏添加了它 \t ' –

+0

@NikhilAgrawal很奇怪。我使用示例XSLT更新了我的答案。以及示例輸入和輸出。正如你所看到的空間被保留。也許問題在於你的XSLT處理器? –

+0

請看我的回答。謝謝你的幫助。從我+1。 –