【问题标题】:XSL changing spaces in a link to %20XSL 更改指向 %20 的链接中的空格
【发布时间】:2010-12-20 15:45:39
【问题描述】:

我需要将 MOSS07 自动创建的带有空格的链接更改为包含 %20。


例子:

{$SafeLinkURL}

这将输出https://stackoverflow.com/example 个空格

https://stackoverflow.com/example%20of%20spaces


如果有人可以对此有所了解,请这样做。

提前致谢,

尼克

【问题讨论】:

  • 好问题,+1。有关完整的 XSLT 1.0 解决方案,请参阅我的答案。 :)

标签: sharepoint sharepoint-2007 xslt


【解决方案1】:

Dimitrie 提到的 XSLT 2.0 函数是:

  1. fn:encode-for-uri()
  2. fn:iri-to-uri()
  3. fn:escape-html-uri()

查看详细说明和示例的链接。在您的情况下(如果您可以使用 XSLT 2.0 处理器)fn:iri-to-uri() 会解决您的问题。

但是这些功能都不会在您当前的 XSLT 1.0 环境中工作。因此,请将此帖子作为其他人的未来参考。

【讨论】:

    【解决方案2】:

    不清楚这个问题到底要问什么。

    如果问题是将给定字符串中的所有空格字符替换为“%20”,这里有一个 XSLT 解决方案

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="link/text()[contains(., ' ')]">
      <xsl:call-template name="replace"/>
     </xsl:template>
    
     <xsl:template name="replace">
      <xsl:param name="pText" select="."/>
      <xsl:param name="pTarget" select="' '"/>
      <xsl:param name="pReplacement" select="'%20'"/>
    
      <xsl:choose>
       <xsl:when test="not(contains($pText, $pTarget))">
        <xsl:value-of select="$pText"/>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select=
          "substring-before($pText, $pTarget)"/>
         <xsl:value-of select="$pReplacement"/>
         <xsl:call-template name="replace">
           <xsl:with-param name="pText" select=
                "substring-after($pText, $pTarget)"/>
           <xsl:with-param name="pTarget" select="$pTarget"/>
           <xsl:with-param name="pReplacement"
                select="$pReplacement"/>
         </xsl:call-template>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    当此转换应用于此 XML 文档时

    <link>http://stackoverflow.com/example of spaces</link>
    

    产生想要的正确结果

    <link>http://stackoverflow.com/example%20of%20spaces</link>
    

    【讨论】:

    • 不判断,但您不认为大量的解码/编码字符串扩展功能更适合吗?带有 url 的东西通常会变得更复杂,而且很快。
    • @Flack:当然可以,但他似乎在问如何使用 XSLT 做到这一点。我认为对于这种 URL 编码,有一个标准的 XPath 2.0 函数。
    • 我有道理,你是对的。但是,我想,EXSLT 字符串或类似字符串的附加链接仍然值得一提:)
    • @Flack:谢谢。他不能使用 EXSLT,因为他在 Share Point 中使用 MSXML。
    【解决方案3】:

    我的意见是

    <a href="a/file name.pdf">
    

    我想处理这个空间,通过添加xmlns:u="java:java.net.URLEncoder" 应用encode(@href, 'UTF-8')

    输出:

    <a href="a%2Ffile+name.pdf">
    

    这里的问题是 + 而不是 %20。所以我用replace($encoded-name, '[+]', '%20')替换了它

    您要复制的代码:

    <xsl:transform version="2.0" 
    xmlns:u="java:java.net.URLEncoder"
    >
    <xsl:param name="encoded-name" select="u:encode(@href, 'UTF-8')"/>
    <xsl:param name="final-name" select="replace($encoded-name, '[+]', '%20')"/>
    

    最终输出:

    <a href="a%2Ffile%20name.pdf">
    

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2011-01-17
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多