Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - How do I add a new line in a XML file rendered using xsl file [Thread Next] Re: How do I add a new line in a XML file rendered using xsl fileTo: 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 < and > also and 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, '
')">
<xsl:value-of select="substring-before($pStr, '
')"/>
<br />
<xsl:call-template name="splitLines">
<xsl:with-param name="pStr" select=
"substring-after($pStr, '
')"/>
</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
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
