Sharepoint 如何使用XSLT1.0或XPath操作HTML字符串
这就是我的问题:下面的代码片段(在Sharepoint 如何使用XSLT1.0或XPath操作HTML字符串,sharepoint,xslt,xpath,Sharepoint,Xslt,Xpath,这就是我的问题:下面的代码片段(在中)不能可靠地使用子字符串-before()和子字符串()函数的组合从字符串中剥离、或标记 我试图格式化的字符串是SharePoint SPS 2003列表项的一个属性—通过富文本编辑器输入的文本。理想情况下,我需要的是一个全面的测试,它总是在换行之前抓取字符串中的文本(实际上是第一段)。我想: <xsl:when test="contains(Story, '
')='True'"> 编辑: 我会试试你的建议。多谢各位 编辑:1
中)不能可靠地使用子字符串-before()
和子字符串()
函数的组合从字符串中剥离、或
标记
我试图格式化的字符串是SharePoint SPS 2003列表项的一个属性—通过富文本编辑器输入的文本。理想情况下,我需要的是一个全面的
测试,它总是在换行之前抓取字符串中的文本(实际上是第一段)。我想:
<xsl:when test="contains(Story, '
')='True'">
编辑:
我会试试你的建议。多谢各位
编辑:12/11/09
只是有机会尝试一下。感谢您的帮助Tomalak-我有一个关于将其呈现为html而不是xml的问题。调用模板removeMarkup时,会收到以下错误消息:
异常:System.Xml.XmlException
消息:“A或
很可能由编辑用或
表示,而不是由&x0a代码>;-)
HTML中的任何地方都不需要换行符,因此如果编辑器决定不包含任何换行符,也可以。依靠换行是你的错误,伊姆霍
除此之外,没有示例XML,任何人都可以猜测XPath会为您带来什么好处
编辑:
我建议使用一个模板(通过递归字符串处理)从字符串中删除任何HTML标记。然后,您可以从结果中提取第一个有意义的文本位并将其打印出来
通过此输入:
<test>
<Story><div><p>The quick brown fox jumped over the lazy dog</p><p>The quick brown fox jumped over the lazy dog</p></div></Story>
<Story><div><p>The quick brown fox jumped over the lazy dog</p><p>The quick brown fox jumped over the lazy dog</p></div></Story>
<Story>The quick brown fox jumped over the lazy dog.<br>The quick brown fox jumped over the lazy dog.</Story>
<Story>The quick brown fox jumped over the lazy dog.</Story>
</test>
敏捷的棕色狐狸跳过了那只懒狗
敏捷的棕色狐狸跳过了那只懒狗
敏捷的棕色狐狸跳过了懒惰的狗。
那只敏捷的棕色狐狸跳过了那只懒狗。
这个样式表:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" />
<xsl:template match="Story">
<xsl:copy>
<original>
<xsl:value-of select="." />
</original>
<processed>
<xsl:variable name="result">
<xsl:call-template name="removeMarkup">
<xsl:with-param name="html" select="." />
</xsl:call-template>
</xsl:variable>
<!-- select the bit of text before the '<>' delimiter -->
<xsl:value-of select="substring-before($result, '<>')" />
</processed>
</xsl:copy>
</xsl:template>
<!-- this template removes all HTML markup (tags) from a string -->
<xsl:template name="removeMarkup">
<xsl:param name="html" select="''" />
<xsl:param name="inTag" select="false()" />
<!-- if we are in a tag, we look for the next '>', otherwise for '<' -->
<xsl:variable name="lookFor">
<xsl:choose>
<xsl:when test="$inTag">></xsl:when>
<xsl:otherwise><</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!-- split the input at the current delimiter char -->
<xsl:variable name="head" select="substring-before(concat($html, '<'), $lookFor)" />
<xsl:variable name="tail" select="substring-after($html, $lookFor)" />
<xsl:if test="not($inTag)">
<xsl:value-of select="$head" />
<!-- now add a uniqe delimiter after the first actual text -->
<xsl:if test="translate(normalize-space($head), ' ', '') != ''">
<xsl:value-of select="'<>'" /> <!-- '<>' as a delimiter -->
</xsl:if>
</xsl:if>
<!-- remove markup for the rest of the string -->
<xsl:if test="$tail != ''">
<xsl:call-template name="removeMarkup">
<xsl:with-param name="html" select="$tail" />
<xsl:with-param name="inTag" select="not($inTag)" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
产生以下结果:
<Story>
<original><div><p>The quick brown fox jumped over the lazy dog</p><p>The quick brown fox jumped over the lazy dog</p></div></original>
<processed>The quick brown fox jumped over the lazy dog</processed>
</Story>
<Story>
<original><div><p>The quick brown fox jumped over the lazy dog</p><p>The quick brown fox jumped over the lazy dog</p></div></original>
<processed>The quick brown fox jumped over the lazy dog</processed>
</Story>
<Story>
<original>The quick brown fox jumped over the lazy dog.<br>The quick brown fox jumped over the lazy dog.</original>
<processed>The quick brown fox jumped over the lazy dog.</processed>
</Story>
<Story>
<original>The quick brown fox jumped over the lazy dog.</original>
<processed>The quick brown fox jumped over the lazy dog.</processed>
</Story>
敏捷的棕色狐狸跳过了那只懒狗
那只敏捷的棕色狐狸跳过了那只懒狗
敏捷的棕色狐狸跳过了那只懒狗
那只敏捷的棕色狐狸跳过了那只懒狗
敏捷的棕色狐狸跳过了懒惰的狗。
那只敏捷的棕色狐狸跳过了那只懒狗。
那只敏捷的棕色狐狸跳过了那只懒狗。
那只敏捷的棕色狐狸跳过了那只懒狗。
免责声明:与HTML输入上的所有字符串处理一样,这不是100%的傻瓜式,某些格式错误的输入可能会破坏它。contains()返回布尔值,因此contains(Story,'
“)=”True“表示强制转换操作。W3C XSLT规范不清楚字符串与布尔值的转换优先级,因此一些XSLT处理器将布尔值转换为字符串,而其他XSLT处理器将字符串转换为布尔值。在第二种情况下,字符串(True())返回'True'而不是'True'
无论如何,您的测试是多余的,只需使用contains()返回的布尔值:
为什么要执行contains()=“True”
?这是多余和不必要的(通常是一种反模式)contains()
已返回布尔值,无需再与字符串'True'
进行比较。此外,我不知道SharePoint SPS 2003列表项的外观。包含示例XML是个好主意。感谢您提供的contains()技巧-stil并没有让我更接近于解决这个问题;)我认为您不需要示例xml。我试图格式化的字符串(故事)是-包含以下html标记之一组合的文本,
,或者使用换行符是我的最后选择,因为我无法以任何其他方式捕获字符串的第一段-可以接受建议。您正在询问如何在某个字符串上使用字符串函数的技巧。为什么您认为没有必要显示示例输入字符串?提供示例代码不仅比解释不那么含糊不清,而且还提供了一种很好的方法,让人们可以开始测试他们的解决方案。我现在正在做的是:编写一些类似于您的问题的XML,以便针对它编写和测试XSLT。基本上,这是我在浪费时间,因为发布实际的XML要容易得多。谢谢你的回复。我应该说和
标记并不总是导致换行
;如前一条评论中所述,我需要捕获字符串第一段中的文本。因为我不能保证哪个html标记将被用来表示一个段落-我需要使用换行符-除非你能建议一个替代方法?谢谢你的评论-我已经按照建议更改了对contains()的调用-但是正如前面所说的,主要问题是我不能可靠地从字符串中提取第一个段落?有什么帮助吗?
<Story>
<original><div><p>The quick brown fox jumped over the lazy dog</p><p>The quick brown fox jumped over the lazy dog</p></div></original>
<processed>The quick brown fox jumped over the lazy dog</processed>
</Story>
<Story>
<original><div><p>The quick brown fox jumped over the lazy dog</p><p>The quick brown fox jumped over the lazy dog</p></div></original>
<processed>The quick brown fox jumped over the lazy dog</processed>
</Story>
<Story>
<original>The quick brown fox jumped over the lazy dog.<br>The quick brown fox jumped over the lazy dog.</original>
<processed>The quick brown fox jumped over the lazy dog.</processed>
</Story>
<Story>
<original>The quick brown fox jumped over the lazy dog.</original>
<processed>The quick brown fox jumped over the lazy dog.</processed>
</Story>
<xsl:when test="contains(Story, '
')">