xml XSL 中的断行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5132613/
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
Break lines in XSL
提问by nam
I've tried to use XSL to output the liste of the customer in a XML file but there is no break lines between values
我尝试使用 XSL 在 XML 文件中输出客户列表,但值之间没有断线
<?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="ISO-8859-1"
doctype-public="-//W3C//DTD HTML 4.01//EN"
doctype-system="http://www.w3.org/TR/html4/strict.dtd"
indent="yes" />
<xsl:template match="/">
<xsl:apply-templates select="//client"/>
</xsl:template>
<xsl:template match="//client">
<xsl:value-of select="./nom/." />
</xsl:template>
</xsl:stylesheet>
The output is
输出是
DoeNampelluro
Normallly I want to get
通常我想得到
Doe
Nam
Pelluro
I've let indent="yes" but that does not do the job
我让 indent="yes" 但这不起作用
回答by Dimitre Novatchev
First of all, the provided XSLT code is quite strange:
首先,提供的 XSLT 代码很奇怪:
<xsl:template match="//client">
<xsl:value-of select="./nom/." />
</xsl:template>
This is much better written as the equivalent:
这更好地写成等价物:
<xsl:template match="client">
<xsl:value-of select="nom" />
</xsl:template>
And the way to output multi-line text is... well, to use the new-line character:
输出多行文本的方式是......好吧,使用换行符:
<xsl:template match="client">
<xsl:value-of select="nom" />
<xsl:if test="not(position()=last())">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
Here is a complete transformation:
这是一个完整的转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="client">
<xsl:value-of select="nom" />
<xsl:if test="not(position()=last())">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document:
当此转换应用于以下 XML 文档时:
<t>
<client>
<nom>A</nom>
</client>
<client>
<nom>B</nom>
</client>
<client>
<nom>C</nom>
</client>
</t>
the wanted, correct result is produced:
产生了想要的、正确的结果:
A
B
C
In case you want to produce xHtml output (not just text), then instead of the NL character, a <br>element has to be produced:
如果您想生成 xHtml 输出(不仅仅是文本),则<br>必须生成一个元素而不是 NL 字符:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="client">
<xsl:value-of select="nom" />
<xsl:if test="not(position()=last())">
<br />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Now, the output is:
现在,输出是:
A<br/>B<br/>C
and it displays in the browser as:
它在浏览器中显示为:
A
B
C
甲
乙
Ç
回答by Ionut Ionete
just add: <br/>tag.
it work for me .
只需添加:<br/>标签。它对我有用。
回答by Vino Kumar
<xsl:for-each select="allowedValueList/allowed">
**<br/>**<xsl:value-of select="." />
</xsl:for-each>
回答by Jimi
I found out that
我发现
<xsl:strip-space elements="*" />
does the trick.
诀窍。

