This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Problem while inserting new lines


Seema Kumar wrote:

> I have included the <br / tag within my xml tags to indicate a new line.
> 
>  I am handling it in my xsl as follows :
> 
>  <xsl:template match="text"
>      <xsl:value-of select ="."/
>      <xsl:apply-templates /
>  </xsl:template
> 
>  <xsl:template match="br"
>   <br/
>  </xsl:template
> 
>  The problem is that all the br tags are matched at the end and they appear
>  as a cluster at the end of the text para.
 
Hi Seema,

Try to avoid using element names that are also keywords in XSLT/XPath.

In your case, you most probably wanted to match text nodes -- text() -- and not
elements named "text".

Here's the template that does what you wanted -- after the correction:

xml source:
----------
<xml>
 <text>
 This is a sample . <br/>
 For testing
 </text>
</xml>

Stylesheet:
----------
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:output omit-xml-declaration="yes" />

    <xsl:template match="text()">
        <xsl:value-of select="." />
    </xsl:template>

    <xsl:template match="br">
        <br />
    </xsl:template>
</xsl:stylesheet>

Result:
------

 This is a sample . <br />
 For testing
 

If you really wanted to match "text" element nodes, then you should use 
<xsl:copy-of select="node()"/> instead of xsl:value-of. Then the template matching
"br" becomes unnecessary and will best be removed.

Cheers,
Dimitre Novatchev

 

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]