开发者

XSL changing spaces in a link to %20

I need to change links that are automatically created by MOSS07 with spaces to include %20.


Example:

{$SafeLinkURL}

which would output https://st开发者_StackOverflowackoverflow.com/example of spaces

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


If anyone can shed some light on this please do.

Thanks in advance,

Nick


The XSLT 2.0 function(s) Dimitrie mentioned are:

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

See the links for detailed specification and examples. In your case (if you could've used a XSLT 2.0 processor) the fn:iri-to-uri() would've solved your problem.

But none of these functions will not work in your current XSLT 1.0 environment. So please see this post as a future reference for other people.


It isn't clear what exactly is asked for in this question.

In case the problem is to replace all space characters in a given string with "%20", here is an XSLT solution:

<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>

when this transformation is applied on this XML document:

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

the wanted, correct result is produced:

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


My input was

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

I wanted to handle this space, applied encode(@href, 'UTF-8') by adding xmlns:u="java:java.net.URLEncoder"

Output:

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

The problem here is + instead of %20. So I replaced that using replace($encoded-name, '[+]', '%20')

Code that you want to copy:

<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')"/>

Final output:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜