Chris
Chris

Reputation: 31

xsl, xml Line break inside for-each loop

I have something like the following xml tree:

<block>
   <house>
      <room>
         <chair>number1</chair>
         <chair>number2</chair>
         <chair>number3</chair>
         <chair>number4</chair>
      </room>
      ...
   </house>
   ...
</block>

I am trying to return the results (number1,...number4) using the loop below.

<xsl:template match="g:block">
    <xsl:for-each select="g:house">
        <xsl:value-of select="g:room" />
    </xsl:for-each>
</xsl:template> 

However, I get everything in a single row and I can't add line breaks somehow.

number1 number2 number3 number4

Is it possible to return each chair tag individually and then add /br to the output to get the following result?

number1
number2
number3
number4

I also tried to go deeper by adding another loop for each chair, but then I cannot extract the value of each chair tag.

Upvotes: 1

Views: 2985

Answers (2)

Florent Georges
Florent Georges

Reputation: 2327

Use the following:

<xsl:value-of select="g:room/string-join(g:chair, '&#10;')"/>

or the following:

<xsl:value-of select="g:room/g:chair" separator="&#10;"/>

or the following (which works also in XSLT 1.0, but as you did not specify it, I assume you use XSLT 2.0):

<xsl:for-each select="g:room/g:chair">
   <xsl:value-of select="."/>
   <xsl:text>&#10;</xsl:text>
</xsl:for-each>

Upvotes: 0

Chris
Chris

Reputation: 31

Problem solved.

<xsl:template match="g:block">
    <xsl:for-each select="g:house/g:room/g:chair">
        <xsl:value-of select="text()" />
    </xsl:for-each>
</xsl:template> 

Upvotes: 0

Related Questions