HOW-TO: Remove embedded HTML in CQWP roll-ups

本文介绍如何通过XSL模板去除SharePoint Content Query Web Part中富文本字段的HTML标记,实现讨论帖子内容的整洁展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

On a recent project, we were rolling up discussion board posts, which include a RichHTML field. The design called for the contents of the most recent posts to be summarized on the site's landing page using a Content Query Web Part (CQWP).

The RichHTML field in the post is a mixed blessing. Having the ability to supply rich formatting within a discussion board post? Great. Having that markup screw up the styling of the landing page when it's rolled up? Less great.

So we set out to display just the content of each post without the formatting. The solution turned out to be reasonably straightforward.

(Note: If you're not familiar with customizing the ItemStyle.xsl or ContentQueryMain.xsl files to format CQWP output, start here.)

The first step was to find some XSL that removes the HTML markup, which I found here; this post adds just a bit more context.

I put this XSL template into the ContentQueryMain.xsl file.

   1: <xsl:template name="OuterTemplate.RemoveHtmlTags">
   2:   <xsl:param name="html"/>
   3:   <xsl:choose>
   4:     <xsl:when test="contains($html, '&lt;')">
   5:       <xsl:value-of select="substring-before($html, '&lt;')"/>
   6:       <!-- Recurse through HTML -->
   7:       <xsl:call-template name="OuterTemplate.RemoveHtmlTags">
   8:         <xsl:with-param name="html" select="substring-after($html, '&gt;')"/>
   9:       </xsl:call-template>
  10:     </xsl:when>
  11:     <xsl:otherwise>
  12:       <xsl:value-of select="$html"/>
  13:     </xsl:otherwise>
  14:   </xsl:choose>
  15: </xsl:template>

And then this additional template goes in the ItemStyle.xsl file:

   1: <xsl:template name="Discussions" match="Row[@Style='Discussions']" mode="itemstyle">
   2:     <xsl:variable name="SafeLinkUrl">
   3:       <xsl:call-template name="OuterTemplate.GetSafeLink">
   4:         <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
   5:       </xsl:call-template>
   6:     </xsl:variable>
   7:     <xsl:variable name="DisplayTitle">
   8:       <xsl:call-template name="OuterTemplate.GetTitle">
   9:         <xsl:with-param name="Title" select="@Title"/>
  10:         <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
  11:       </xsl:call-template>
  12:     </xsl:variable>
  13:     <xsl:variable name="LinkTarget">
  14:       <xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
  15:     </xsl:variable>
  16:     <xsl:variable name="pureTextBody">
  17:       <xsl:call-template name="OuterTemplate.RemoveHtmlTags">
  18:         <xsl:with-param name="html" select="@Body" />
  19:       </xsl:call-template>
  20:     </xsl:variable>
  21:  
  22:     <table width="100%" border="0">
  23:       <tr>
  24:         <td style="width: 12px;" valign="top">
  25:           <div id="linkitem" class="listing font_1 link-item cqwpbullet">
  26:             <img src="/_LAYOUTS/1033/IMAGES/4_bullets.gif" border="0" style="margin:4px 0px 0px 0px;" />
  27:           </div>
  28:         </td>
  29:         <td valign="top">
  30:           <div id="linkitem" class="listing font_1 link-item cqwpbullet">
  31:             <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}" class="listinHrf">
  32:               <xsl:value-of select="substring($pureTextBody, 1, 100)" disable-output-escaping="yes" />...
  33:             </a>
  34:           </div>
  35:         </td>
  36:       </tr>
  37:     </table>
  38: </xsl:template>

Note that the pureTextBody XSL variable (line 16) is called by passing the Body attribute to the OuterTemplate.RemoveHtmlTags template (lines 17-18) that we put into the ContentQueryMain.xsl file -- and then that variable, cleansed of its HTML markup, is rendered as output (line 32).

That's it. Now the content of the discussion posts roll up and have a consistent presentation on the site landing page, but have their formatting intact when the user clicks through to look at the full post.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值