XSL 输出方法文本在 xsl 中包含空格
我正在创建一些 xsl 将我的 xml 转换为文本(最终将是 csv)。我用的是VS2008。当我使用编辑器创建 xsl 时,转换后的输出将按照我的 xsl 缩进。但是,如果我编辑 xsl 并删除格式化的空格,它会正确输出 - 但这样做是一场噩梦。
是否可以放入一些 xsl 预处理器命令或标记来防止这种情况发生?我想忽略 xsl 中的任何空格,只使用 或
输出文本。
我的 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 中,我没有告诉它包含一个。
请帮忙!!
谢谢,
安德斯
I am creating some xsl to transform my xml into text (which will be csv eventually). I am using VS2008. When I use the editor to create the xsl, the transformed output is indented as per my xsl. However, if I edit the xsl and remove the formatted whitespaces it outputs correctly - but doing it like this is a nightmare to work with.
Is there some xsl pre-processor commands or markup I can put in to prevent this? I want to ignore any whitespace in my xsl and only output text using <!CDATA[]]>
or <xsl:text>
.
My XSL is as below - this indents the output
<?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>
The output of this is as follows:
CSV Output
row id=0
row id=1
<EOF>
However, the following outputs correctly:
<?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>
This is output correctly as follows:
CSV Output
row id=0
row id=1
<EOF>
I also want to control where a new line is included. In my xsl I am not telling it to include one.
Please help!!
Thanks,
Andez
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XSLT 处理器仅去除模板中 XSLT 元素之间的空白文本节点。
因此,在
xsl:for-each
元素中,有两个空白文本子节点:一个位于xsl:value-of
之后,该子节点已被删除;另一个位于xsl:value-of
之后,该子节点已被删除;另一个位于 CDATA 部分之前,未被剥离。底线:使用
xsl:text
元素。The XSLT processor strips white-space text nodes in the template only between XSLT elements.
So, in
the
xsl:for-each
element has two white-space text child nodes: One afterxsl:value-of
, which is stripped; the other before the CDATA section, which is not stripped.Bottom line: Use
xsl:text
elements.您可以使用
xsl:strip
元素来声明哪些元素不应有空格(或对所有元素使用*
):对应部分是
xsl:preserve
,它允许您声明哪些元素应保留空格。您可以同时使用:You can use the
xsl:strip
element to declare which elements should not have whitespace (or use*
for all elements):The counter part is
xsl:preserve
, which allows you to declare which elements should have whitespace preserved. You could use both: