nam
nam

Reputation: 3632

Break lines in XSL

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

<?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

Upvotes: 8

Views: 36404

Answers (4)

Ionut Ionete
Ionut Ionete

Reputation: 19

just add: <br/> tag. it work for me .

Upvotes: 2

Vino Kumar
Vino Kumar

Reputation: 11

<xsl:for-each select="allowedValueList/allowed">
**<br/>**<xsl:value-of select="." />
</xsl:for-each>

Upvotes: 1

Jimi
Jimi

Reputation: 1228

I found out that

<xsl:strip-space elements="*" />

does the trick.

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243569

First of all, the provided XSLT code is quite strange:

<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>&#xA;</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>&#xA;</xsl:text>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<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:

<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

Upvotes: 25

Related Questions