XSL line breaks

I tried using XSL to list the clients in an XML file, but between the 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> 

Output signal

 DoeNampelluro 

I usually want to get

 Doe Nam Pelluro 

I let go indent = "yes", but it does not work

+6
xml xslt
source share
4 answers

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

 <xsl:template match="//client"> <xsl:value-of select="./nom/." /> </xsl:template> 

It is much better written as equivalent :

 <xsl:template match="client"> <xsl:value-of select="nom" /> </xsl:template> 

And the way to output multi-line text ... well, to use a newline:

 <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 the complete conversion:

 <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 conversion is applied to the following XML document:

 <t> <client> <nom>A</nom> </client> <client> <nom>B</nom> </client> <client> <nom>C</nom> </client> </t> 

the desired, correct result is output:

 A B C 

If you want to create xHtml output (and not just text), then the <br> element must be created instead of the NL character :

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

 A<br/>B<br/>C 

, and it appears in the browser as :

but
IN
FROM

+23
source share

just add the tag <br/> . it works for me.

+2
source share
 <xsl:for-each select="allowedValueList/allowed"> **<br/>**<xsl:value-of select="." /> </xsl:for-each> 
+1
source share

I found out that

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

does the trick.

0
source share

All Articles