【问题标题】:Sharepoint XSL - break string into componant partsSharepoint XSL - 将字符串分解为组件部分
【发布时间】:2010-12-24 04:16:43
【问题描述】:

我正在使用启用了多项检查选项的“选择”网站栏,以便用户可以使用该栏的多个选项标记列表项。

然后,此列会为内容查询 Web 部件中的设计功能提供支持 - 在其中附加列选择以创建图像文件名。

  • 选择1
  • 选择2
  • 选择3
  • 选择4

变成

<img src="http://mysite/content-Choice1.jpg />

我遇到的问题是 XSL 解析器输入了一个字符串,该字符串用分号 (;) 和哈希 (#) 分隔选择值。如果勾选了所有 4 个选项,则输入 XSLT 解析器的字符串将是:

;#Choice1;#Choice2;#Choice3;#Choice4

我如何处理字符串并将每个选项分离到其自己的 XSL 变量中?

我已经尝试了各种 substring-before 函数,但我无法得到任何工作。

【问题讨论】:

  • 一般xslt问题应该有输入源、期望的输出和关系。这个问题是针对特定的 XSLT 框架的。作为一般 XSLT 问题的答案是 How can I work through the string and separate each choice into it's own XSL variable? 没有办法。您可以构建一个临时结果树(带有 XSLT 1.0 的扩展函数),为每个“Choice”字符串保存一个元素。请参阅有关 XSLT 中标记化的问题。

标签: sharepoint sharepoint-2007 xslt


【解决方案1】:

由于 XPath 1.0 不支持tokenize() 函数,您必须自己完成所有工作。例如,您可以从选项中递归生成 &lt;img&gt; 元素:

<xsl:template name="RecurseConvertChoicesToImages">
    <xsl:param name="choices" />

    <xsl:variable name="token"
        select="substring-before($choices, ';#')" />
    <xsl:variable name="nextToken"
        select="substring-after($choices, ';#')" />

    <xsl:if test="$token">
        <img src="http://mysite/content-{$token}.jpg" />
    </xsl:if>
    <xsl:if test="$nextToken">
        <xsl:call-template name="RecurseConvertChoicesToImages">
            <xsl:with-param name="choices" select="$nextToken" />
        </xsl:call-template>
    </xsl:if>
</xsl:template>

【讨论】:

  • @Ashutosh Singh,如果你喜欢这个答案,请点击“这个答案很有用”向上箭头。
【解决方案2】:

我会建议使用 JavaScript 来解析字符串并使用 JavaScript 相应地显示图像。

【讨论】:

  • 嗨 - XSL 在到达页面之前在 MOSS 中进行解析,因此我更喜欢服务器端 XSLT 解决方案,而不是页面上的后处理。
  • 然后回复 frederic :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多