我正在创建一些 xsl 来将我的 xml 转换为文本(最终将是 csv)。我正在使用 VS2008。当我使用编辑器创建 xsl 时,转换后的输出按照我的 xsl 缩进。但是,如果我编辑 xsl 并删除它正确输出的格式化空格 - 但这样做是一场噩梦。
我可以放入一些 xsl 预处理器命令或标记来防止这种情况发生吗?我想忽略我的 xsl 中的任何空格,只使用 <!CDATA[]]>
输出文本或 <xsl:text>
.
我的 XSL 如下 - 这会缩进输出
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<!-- @* is all class attributes -->
<xsl:template match="/">
<xsl:text>CSV Output</xsl:text>
<!-- Start of output -->
<xsl:for-each select="//rows/row">
<![CDATA[row id=]]><xsl:value-of select="(@id)"/>
</xsl:for-each>
<!-- OK, that is the end of the file -->
<![CDATA[<EOF>]]>
</xsl:template>
</xsl:stylesheet>
输出如下:
CSV Output
row id=0
row id=1
<EOF>
但是,以下输出正确:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="text" indent="no"/>
<!-- @* is all class attributes -->
<xsl:template match="/">
<xsl:text>CSV Output</xsl:text>
<!-- Start of output -->
<xsl:for-each select="//rows/row">
<![CDATA[row id=]]><xsl:value-of select="(@id)"/>
</xsl:for-each>
<!-- OK, that is the end of the file -->
<![CDATA[<EOF>]]>
</xsl:template>
</xsl:stylesheet>
正确输出如下:
CSV Output
row id=0
row id=1
<EOF>
我还想控制包含新行的位置。在我的 xsl 中,我没有告诉它包含一个。
请帮忙!!
谢谢,
安德斯
最佳答案
XSLT 处理器仅在 XSLT 元素之间去除模板中的空白文本节点。
所以,在
<xsl:for-each select="//rows/row">
<![CDATA[row id=]]><xsl:value-of select="(@id)"/>
</xsl:for-each>
xsl:for-each
element 有两个空白文本子节点:一个在 xsl:value-of
之后, 被剥离;另一个在 CDATA 部分之前,没有被剥离。底线:使用
xsl:text
元素。<xsl:for-each select="//rows/row">
<xsl:text><![CDATA[row id=]]></xsl:text>
<xsl:value-of select="@id"/>
</xsl:for-each>
关于xslt - XSL 输出方法文本,包括 xsl 中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3839730/