Use java.net.URLEncoder or something else

c-siems's Avatar

c-siems

Jul 11, 2013 @ 08:40 PM

Re: http://help.hannonhill.com/discussions/xslt-formats/26-java-url-encoder-allowed-in-cms

I was able to put java.net.URLEncoder into the XSLT, but spaces in URLs are transcribed to pluses (+).

I've search around and I'm not finding much help. One suggestion was using a toURL function on it, but my attempts failed. Another suggestion was to use java.net.URI with toURI and toURL, but I couldn't get that to either.

Is there another way to encode URLs - specifically, encode/transcribe illegal characters to legal characters?

  1. 1 Posted by Ryan Griffith on Jul 11, 2013 @ 09:02 PM

    Ryan Griffith's Avatar

    Hi,

    Curious, but something you could try is using the JavaScript encodeURIComponent() method. Because you are appending the protocol and host information, this should theoretically work.

    When you have a moment, give the following Xalan component a try and let me know if this produces the expected result:

    <xalan:component functions="uriEncode" prefix="hh2">
        <xalan:script lang="javascript"><![CDATA[
            function uriEncode(str) {
                return encodeURIComponent(str);
            }
        ]]></xalan:script>
    </xalan:component>
    

    Note: because you already have the hh prefix defined, I used hh2. Also, two prefixes cannot use the same URL, so I used xmlns:hh2="http://www.tamu.edu/XSL/Functions" during testing (the URL doesn't really matter).

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Ryan Griffith on Jul 17, 2013 @ 07:10 PM

    Ryan Griffith's Avatar

    Hi,

    Just wanted to follow up to see if you had a chance to view my recent comment.

    Please let us know if you have any questions.

    Thanks.

  3. 3 Posted by c-siems on Jul 18, 2013 @ 02:01 PM

    c-siems's Avatar

    That did it. Thanks Ryan. This seems like a lot of work to escape 1 or 2 URI - especially when our contributors know better than to put spaces in system names.

    For those of you wanting to know how, in the format.xsl.txt file above, I changed the line

        <xsl:variable name="feedlink" select="encoder:encode(substring(string(path),15),'UTF-8')" xmlns:encoder="xalan://java.net.URLEncoder"/>
    

    to

        <xsl:variable name="feedlink" select="hh2:uriEncode(substring(string(path),15),'UTF-8')" xmlns:encoder="xalan://java.net.URLEncoder"/>
    

    I also changed the <guid> tag to match the (escaped) <link> tag. This makes the RSS pass the validator.

  4. 4 Posted by Ryan Griffith on Jul 18, 2013 @ 03:51 PM

    Ryan Griffith's Avatar

    Thank you for following up, I am glad to hear that did the trick.

    This seems like a lot of work to escape 1 or 2 URI - especially when our contributors know better than to put spaces in system names.

    Have you looked into maybe using one of the Asset Factory plugins to generate a system name without spaces? The "Friendly" plugins convert spaces to dashes.

  5. Ryan Griffith closed this discussion on Jul 26, 2013 @ 02:01 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac