在您的 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。