AltF4_
AltF4_

Reputation: 2470

How do i break multi-line element in my XML using XSLT

I have a XML document that looks like this

<ROOT>
 <SUMMARY>
   This is line 1
   this is line 2
 </SUMMARY>
 <STEPSBEFORE>
   this is step 1
   this is step 2
 </STEPSBEFORE>
</ROOT>

My XSLT currently brings back the output like this:

SUMMARY

This is line 1 This is line 2

STEPSBEOFRE

This is step 1 This is step 2

**Here is code for my XSL

                <xsl:stylesheet version="1.0"
                                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                                  <xsl:output omit-xml-declaration="yes"/>
                                  <xsl:strip-space elements="*"/>
                                    <xsl:template match="/">
                                    <xsl:comment>CHANGES TO THIS STRING WILL BE LOST - auto-generated by build process</xsl:comment>
                                      <html><body>
                                          <ROOT>
                                              <h2><b>Summary</b></h2>
                                              <xsl:value-of select="//Summary"/>
                                              <h2><b>StepsBefore</b></h2>                                       
                                               <xsl:value-of select="//StepsBefore"/>
                                               <xsl:for-each select="//StepsBefore">
                                                <xsl:value-of select="current()"/>
                                                <xsl:text>&#xA;</xsl:text>
                                               </xsl:for-each>
                                          </ROOT>
                                          </body></html>
                                  </xsl:template>
                              </xsl:stylesheet>

I want my output to be displayed per line as it is in the original XML file; but this is not working... any idea how i can achieve the per line affect i am after? ideally each with a bullet point.

Upvotes: 0

Views: 1901

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116993

The following stylesheet:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates select="ROOT/*"/>
        </body>
    </html>
</xsl:template>

<xsl:template match="*">
    <h2>
        <xsl:value-of select="local-name()"/>
    </h2>
    <ul>
        <xsl:call-template name="tokenize">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </ul>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="'&#10;'"/>
        <xsl:variable name="token" select="normalize-space(substring-before(concat($text, $delimiter), $delimiter))" />
        <xsl:if test="$token">
            <li>
                <xsl:value-of select="$token"/>
            </li>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

when applied to your input example, will return:

<html>
   <body>
      <h2>SUMMARY</h2>
      <ul>
         <li>This is line 1</li>
         <li>this is line 2</li>
      </ul>
      <h2>STEPSBEFORE</h2>
      <ul>
         <li>this is step 1</li>
         <li>this is step 2</li>
      </ul>
   </body>
</html>

rendered as:

enter image description here

Upvotes: 2

Martin Honnen
Martin Honnen

Reputation: 167571

If you want to transform your XML to HTML and want white space rendered as is then use the HTML pre element e.g.

<xsl:template match="SUMMARY">
  <h2>Summary</h2>
  <pre><xsl:apply-templates/></pre>
</xsl:template>

Then make sure your template matching / does <xsl:apply-templates/>.

Upvotes: 0

Related Questions