xml 用 xsl:text 解释换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/185101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interpreting newlines with xsl:text?
提问by Greg Mattes
I have an XSL stylesheet with content in an xsl:textnode like this:
我有一个 XSL 样式表,其内容在一个xsl:text节点中,如下所示:
<xsl:text>
foo
bar
baz
</xsl:text>
The stylesheet itself is a text file with "unix-style" newline line terminators. I invoke this stylesheet on Windows as well as unix-like platforms. It would be nice to have the output conform to the conventions of the platform on which it is invoked.
样式表本身是一个带有“unix 风格”换行符的文本文件。我在 Windows 以及类 Unix 平台上调用此样式表。让输出符合调用它的平台的约定会很好。
When I run this stylesheet on Windows, the output has carriage return/newline pairs for everything exceptthe contents of the xsl:textnode.
当我在 Windows 上运行此样式表时,输出对除xsl:text节点内容外的所有内容都有回车/换行符对。
Can I instruct the XSLT processor to translate the newline characters in the content of the xsl:textnode into platform specific end-of-lines?
我可以指示 XSLT 处理器将xsl:text节点内容中的换行符转换为特定于平台的行尾吗?
More context: I'm invoking the stylesheet from the Apache Ant 1.7.1 XSLT tasklike this:
更多上下文:我正在从Apache Ant 1.7.1 XSLT 任务调用样式表,如下所示:
<xslt in="in.xml" out="out.xml" style="stylesheet.xsl"/>
The stylesheet header currently looks like this:
样式表头目前看起来像这样:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="http://xml.apache.org/xslt"
exclude-result-prefixes="xalan">
<!-- contents elided -->
</xsl:stylesheet>
回答by Adam Crume
You could define a parameter for the stylesheet like so:
您可以为样式表定义一个参数,如下所示:
<xsl:param name="br">
<xsl:text> </xsl:text>
</xsl:param>
and pass in the appropriate end of line character(s) by using a nested param element in your Ant script. The default in this example would be a Unix-style newline, of course. I think to output the value, you'd have to use:
并通过在 Ant 脚本中使用嵌套的 param 元素传入适当的行尾字符。当然,此示例中的默认值将是 Unix 样式的换行符。我认为要输出该值,您必须使用:
<xsl:copy-of select="$br"/>
It's verbose, but it works.
它很冗长,但它有效。
回答by Mads Hansen
If you are calling the transform from Ant, then you can test for the OS using a conditional task with a test for the OS family:
如果您正在从 Ant 调用转换,那么您可以使用带有 OS 系列测试的条件任务来测试 OS:
<condition property="linebreak" value="
">
<os family="windows"/>
</condition>
<condition property="linebreak" value="
">
<os family="unix"/>
</condition>
Then pass that parameter to the XSLT to signal which newline character(s) you want to use.
然后将该参数传递给 XSLT 以表示您要使用哪个换行符。
<xslt in="data.xml" out="${out.dir}/out.xml">
<param name="linebreak" expression="${linebreak}" />
</xslt>
回答by Jen A
Im not sure how to do the correct newlines automatically (it may depend on the xslt processor you are using), but you may be able to force the newlines in the contents of your text node. \r\n is 
 
, \n is 
, e.g. you'd use:
我不确定如何自动执行正确的换行(这可能取决于您使用的 xslt 处理器),但您可以在文本节点的内容中强制换行。\r\n 是 
 
,\n 是
,例如你会使用:
<xsl:text>foo
bar
</xsl:text>to get the output you're looking for.
<xsl:text>foo
bar
</xsl:text>以获得您正在寻找的输出。
回答by granadaCoder
Well, I got mine to work (using Saxon) with a combination of 2 ideas above:
好吧,我让我的工作(使用撒克逊人)结合了上述 2 个想法:
<xsl:param name="br">
<xsl:text>
</xsl:text>
</xsl:param>
and then using line(s) like these where needed.
然后在需要的地方使用这些线。
<xsl:value-of select="$br" />
Here is my full (but slimline) xsl
这是我的完整(但超薄)xsl
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--<xsl:strip-space elements="*" />-->
<xsl:output method="text" />
<!-- <xsl:preserve-space elements="*"/>-->
<xsl:param name="br">
<xsl:text>
</xsl:text>
</xsl:param>
<!-- -->
<xsl:template match="/">
<xsl:for-each select="//root/Item">
<xsl:value-of select="@Name" /> <!-- Your xpath will vary of course! -->
<xsl:value-of select="$br" />
</xsl:for-each>
</xsl:template>
Again, I am using Saxon %ProgramFiles%\SaxonHE\bin\Transform.exe on a windows 7 x64 machine.
同样,我在 Windows 7 x64 机器上使用 Saxon %ProgramFiles%\SaxonHE\bin\Transform.exe。

