插入<br>在 XSL 中的这一行不起作用



我正试图使用以下代码删除富文本格式的"/par"标记,并将其替换为<br/>。该代码可以很好地剥离"/par",但由于某些原因,没有插入<BR/>。我试着用****(只是一些纯文本)替换<BR/>,插入得很好。问题出在底部的"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="'{rtf1ansiansicpg1252deff0deflang1033{fonttbl{f0fnilfcharset0 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="'viewkind4uc1pardf0fs17'"/>
        <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>

有什么线索表明为什么这不起作用吗?

我想我刚刚遇到了同样的问题(这就是我在这个页面上使用atm的原因)。你的问题在于你输出整个东西的代码:

<xsl:value-of select="substring($par,1,$s_length)"/>

尝试使用:

<xsl:copy-of select="substring($par,1,$s_length)"/>

"xsl:value-of"只返回所选标记中的text()元素,而"xsl:copy-of"返回所选标签的所有元素(包括text())。

这实际上意味着"xsl:value of"只是剥离"<BR/>"(xml元素),而不是"***"(text()元素。

<br/>更改为<xhtml:br/>,以告诉xsl br不是xml元素,但实际上应该在结果中呈现。

[编辑]

只有当您实际将转换为xhtml时,这可能才有效-o

<BR/>不是xsl中的有效元素。

您必须定义一个模板,然后将该模板包含在xsl 中

在您的模板中

<xsl:template name="Newline"><xsl:text>
</xsl:text></xsl:template>

在您的xslt 中

<xsl:value-of select="substring-before($thetext,'par')"/>
<xsl:call-template name="Newline" />
<xsl:call-template name="break">
...

最新更新