Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How do I add a new line in a XML file rendered using xsl file

From: "Dimitre Novatchev" <dimitren@---.---.-->
To: NULL
Date: 1/4/2007 5:16:00 PM


<glenpeterthompson@h...> wrote in message 
news:1167904637.638790.151260@i......
> This is my xml file:
> <root>
>  <node>
>    <text>Line 1
> Line 2
> Line 3
>    </text>
>  </node>
>  <node>
>    <text>Line 1
> Line 2
>    </text>
>  </node>
> </root>
>
> This is the output I want:
> Line 1
> Line 2
> Line 3
>
> Line 1
> Line 2
>
> How do I construct my xsl so that it produces a new lines because html
> ignores white space? When using <xsl:value-of
> select="/root/node/text"/>
> I have tried using <br /> in my xml however this does not parse. I have
> tried uising &lt; and &gt; also &#10; and &#13; I have tried using a
> dummy character such as '{n}' in the xml and used the translate xpath
> function to replace the '{n}' with '<br />' however get the dreaded '<'
> is an invalid character error.

Here is one possible solution.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="text">
   <xsl:call-template name="splitLines">
     <xsl:with-param name="pStr" select="string(.)"/>
   </xsl:call-template>
 </xsl:template>

 <xsl:template match="node">
   <br />
   <xsl:apply-templates/>
 </xsl:template>
 <xsl:template name="splitLines">
    <xsl:param name="pStr" select="''"/>

    <xsl:choose>
      <xsl:when test="contains($pStr, '&#xA;')">
        <xsl:value-of select="substring-before($pStr, '&#xA;')"/>
        <br />
        <xsl:call-template name="splitLines">
          <xsl:with-param name="pStr" select=
           "substring-after($pStr, '&#xA;')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$pStr"/>
      </xsl:otherwise>
    </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

When applied on the provided xml document:

<root>
  <node>
    <text>Line 1
Line 2
Line 3
    </text>
  </node>
  <node>
    <text>Line 1
Line 2
    </text>
  </node>
</root>

produces the wanted result and is displayed in the browser as required:

  <br />
    Line 1<br />Line 2<br />Line 3<br />

  <br />
    Line 1<br />Line 2<br />


Cheers,
Dimitre Novatchev 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent