Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break lines in XSL

Tags:

xml

xslt

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

like image 514
nam Avatar asked Feb 27 '11 10:02

nam


People also ask

What is xsl Strip space?

The <xsl:strip-space> element strips white-space-only text nodes in the specified elements. By default, all white-space-only text nodes are preserved. If an element name matches a name test in an <xsl:strip-space> element, it is removed from the set of white-space-preserving element names.

What is &# xD & xA?

The &#xD;&#xA; are carriage returns and line feeds either within your XML or your XSLT.

What is substring after in XSLT?

substring-after() Function — Returns the substring of the first argument after the first occurrence of the second argument in the first argument. If the second argument does not occur in the first argument, the substring-after() function returns an empty string.


2 Answers

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

like image 154
Dimitre Novatchev Avatar answered Nov 12 '22 19:11

Dimitre Novatchev


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

like image 29
Ionut Ionete Avatar answered Nov 12 '22 18:11

Ionut Ionete