插入
在 xsl 中的这一行不起作用
我尝试使用以下代码删除富文本格式“/par”标记并将其替换为
。该代码可以很好地剥离“/par”,但由于某种原因,未插入
。我尝试将
>
替换为 ****
(只是一些纯文本),并且插入得很好。问题出在底部的“break”模板中。
<?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" />
</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>
关于为什么这不起作用的任何线索?
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" />
</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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想我也遇到了同样的问题(这就是我在这个页面上的原因)。您的问题在于输出整个内容的代码:
尝试使用:
“xsl:value-of”仅返回所选标记内的 text() 元素,而“xsl:copy- of" 返回所选标记的所有元素(包括 text())。
这实际上意味着“xsl:value-of”简单地剥离了“
” (一个 xml 元素),但不是“****”(一个 text() 元素)。
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:
try using:
"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.
将
更改为
以告诉 xslbr
不是 xml 元素,但应该实际上会呈现在结果中。[编辑]
这可能仅在您实际转换为 xhtml 时才有效。 :-o
change
<br/>
to<xhtml:br/>
to tell xsl thatbr
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
不是 xsl 中的有效元素。您必须定义一个模板,然后将该模板包含在您的 xsl 中
模板中
您的 xslt
<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
in your xslt