Adding newline between elements in XSLT

L.krez

I'm beggining in XML and XSLT and I have a problem with adding a new line beetwen elements

Here's XML:

<?xml version="1.0" encoding="UTF-8"?>
<numbers>
    <person id="1">
        <phone>
         <phone_nr>111111111</phone_nr>
         <phone_nr>222222222</phone_nr>
        </phone>
    </person>
    <person id="2">
        <phone>
          <phone_nr>333333333</phone_nr>
            </phone>
    </person>
</numbers>

XSLT looks like:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <xsl:for-each select="numbers/person">
    <table border="1">
      <tr>
       <td>
       <table>
         <td><xsl:value-of select="phone"/></td>
       </table>
      </td>
     </tr>
    </table>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

and it gives me this (with borders) :

111111111 222222222
333333333

but what I want is:

111111111
222222222
333333333

The problem is that XML must be like this and I don't know, how to creating a new line in XSLT.

Tim C

You are outputting HTML, so to do a "newline" you need to output a <br> tag. The problem you have at the moment is that you are outputting the text value of the phone element, which concatenates all the text nodes under it together. You really need to handle the child phone_nr nodes separately, with an xsl:for-each for example

   <td>
       <xsl:for-each select="phone/phone_nr">
          <xsl:value-of select="."/><br />
       </xsl:for-each>
    </td>

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <xsl:for-each select="numbers/person">
    <table border="1">
      <tr>
       <td>
           <xsl:for-each select="phone/phone_nr">
              <xsl:value-of select="."/><br />
           </xsl:for-each>
        </td>
     </tr>
    </table>
    </xsl:for-each>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Adding a newline in a spool file

From Dev

adding an element in between a row of floated elements

From Dev

Adding additional newline between each line

From Dev

adding an element only between two elements of given class (with css)

From Dev

Adding a newline in css

From Dev

XSLT is adding additional characters to HTML image elements

From Dev

XSLT : Insert new line between two elements in a template

From Dev

Adding a newline in a substitute() expression

From Dev

Python YAML preserving newline without adding extra newline

From Dev

Adding newline to PHP string

From Dev

Create newline between two ionic form elements

From Dev

Adding newline between elements in XSLT

From Dev

How to merge elements between two documents using XSLT 2.0?

From Dev

Adding a tab and newline to my terminal prompt

From Dev

XSLT - Adding a sequence number/counter to an element while copying all other elements

From Dev

Why is Vim adding a newline? Is this a convention?

From Dev

Adding a newline in a textarea using Javascript

From Dev

How to insert newLine Between Text

From Dev

Adding new XML elements using XSLT

From Dev

Adding newline to PHP string

From Dev

Adding commas between elements of a binary matrix in text file

From Dev

Command substitution is adding a newline?

From Dev

Selecting between text with a newline spanning

From Dev

Adding a newline in CodenameOne's SpanLabel

From Dev

Delete newline characters between patterns

From Dev

Very new to XSLT! Having difficulty adding new line between records

From Dev

Strip encoded values between XML elements and namespaces in XSLT 1.0

From Dev

Adding Environment.NewLine into filestream

From Dev

Adding elements between elements in list

Related Related

HotTag

Archive