在XSL中打破行

nam*_*nam 6 xml xslt

我试图使用XSL在XML文件中输出客户的liste,但值之间没有断行

<?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>
Run Code Online (Sandbox Code Playgroud)

输出是

DoeNampelluro
Run Code Online (Sandbox Code Playgroud)

我想要得到的

Doe
Nam
Pelluro
Run Code Online (Sandbox Code Playgroud)

我让缩进="是",但这不起作用

Dim*_*hev 24

首先,提供的XSLT代码很奇怪:

<xsl:template match="//client">
  <xsl:value-of select="./nom/." />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这更好地写为等效:

<xsl:template match="client">
 <xsl:value-of select="nom" />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

输出多行文本的方法是......好吧,使用换行符:

<xsl:template match="client">
 <xsl:value-of select="nom" />
 <xsl:if test="not(position()=last())">
   <xsl:text>&#xA;</xsl:text>
 </xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这是一个完整的转变:

<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>&#xA;</xsl:text>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

当此转换应用于以下XML文档时:

<t>
 <client>
   <nom>A</nom>
 </client>
 <client>
   <nom>B</nom>
 </client>
 <client>
   <nom>C</nom>
 </client>
</t>
Run Code Online (Sandbox Code Playgroud)

产生了想要的正确结果:

A
B
C
Run Code Online (Sandbox Code Playgroud)

如果您想生成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>
Run Code Online (Sandbox Code Playgroud)

现在,输出是:

A<br/>B<br/>C
Run Code Online (Sandbox Code Playgroud)

它在浏览器中显示为:

A
B
C.