在你的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。
沒有。問題是一樣的。我在這裏添加了它 \t ' –
@NikhilAgrawal很奇怪。我使用示例XSLT更新了我的答案。以及示例輸入和輸出。正如你所看到的空間被保留。也許問題在於你的XSLT處理器? –
請看我的回答。謝謝你的幫助。從我+1。 –