我有这样的数据
<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
。如何保留数据中的空间?
答案 0 :(得分: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。
答案 1 :(得分: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的缩进视为文本,正如我们所说的那样,以保留空间。