inserting <br> won't work at this one line in xsl
I'm trying to use the following code to remove the rich text formatting "/par" tag and replace it with <br/>
. The code works fine for stripping the "/par" but for some reason the <BR/>
is not inserted. I tried replacing the <BR/>
with ****
(just some plain text) and that was inserted just fine. The problem is in the "break" template at the bottom.
<?xml version='1.0'?>
<xsl:template match="ProjectDataSet">
<html>
<body>
<xsl:for-each select="Project">
<H2>
<xsl:value-of select ="@ProjectID"/>
</H2>
<H3>Information</H3>
Userfield1: <xsl:value-of select="UserField1"/>
Userfield2: <xsl:value-of select="UserField2"/>
<H3>Milestones</H3>
<xsl:for-each select="MilestoneItem">
Date: <xsl:value-of select="Date"/> Event: <xsl:value-of select="Event"/><BR/>
</xsl:for-each>
<H3>Comments</H3>
<xsl:for-each select="CommentItem">
Date: <xsl:value-of select="Date"/><BR/>
Description<BR/>
<xsl:call-template name="ConvertRTF">
<xsl:with-param name="desc" select="Description"/>
</xsl:call-template>
<HR/>
</xsl:for-each>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="ConvertRTF">
<xsl:param name="desc"/>
<xsl:variable name="header1">
<!--remove rtf header part 1-->
<xsl:call-template name="string-replace-all">
<xsl:with-param name="oldtext" select="$desc"/>
<xsl:with-param name="replace" select="'{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}'"/>
<xsl:with-param name="by" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="header2">
<!--remove rtf header part 2-->
<xsl:call-template name="string-replace-all">
<xsl:with-param name="oldtext" select="$header1"/>
<xsl:with-param name="replace" select="'\viewkind4\uc1\pard\f0\fs17'"/>
<xsl:with-param name="by" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="par">
<!--place rtf /par with html <br> ******check this-->
<xsl:call-template name="break">
<xsl:with-param name="thetext" select="$header2"/>
</xsl:call-template>
</xsl:variable>
<!--remove final } and output value-->
<xsl:variable name="s_length" select="string-length($par)-2"/>
<xsl:value-of select="substring($par,1,$s_length)"/>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="oldtext" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="contains($oldtext, $replace)">
<xsl:value-of select="substring-before($oldtext,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="oldtext" select="substring-after($oldtext,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$oldtext" /&g开发者_JAVA技巧t;
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="break">
<xsl:param name="thetext"/>
<xsl:choose>
<xsl:when test="contains($thetext,'\par')">
<xsl:value-of select="substring-before($thetext,'\par')"/>
<BR/>
<xsl:call-template name="break">
<xsl:with-param name="thetext" select="substring-after($thetext,'\par')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$thetext"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Any clues on why this isn't working?
I think I just had the same problem (that's the reason I'm on this page atm). Your problem lies in the code where you output the whole thing:
<xsl:value-of select="substring($par,1,$s_length)"/>
try using:
<xsl:copy-of select="substring($par,1,$s_length)"/>
"xsl:value-of" returns only text() elements within the selected tag(s), while "xsl:copy-of" returns all elements (including text()) of the selected tag(s).
This effectively means that "xsl:value-of" simply strips "<BR/>" (an xml element), but not "****" (a text() element.
change <br/>
to <xhtml:br/>
to tell xsl that br
is not an xml element, but should actually be rendered in the result.
[edit]
That may only work if you're actually converting to to xhtml. :-o
<BR/>
isn't a valid element in xsl.
you will have to define a template, then include that template in your xsl
in your template
<xsl:template name="Newline"><xsl:text>
</xsl:text></xsl:template>
in your xslt
<xsl:value-of select="substring-before($thetext,'\par')"/>
<xsl:call-template name="Newline" />
<xsl:call-template name="break">
...
精彩评论