• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2032
  • Last Modified:

Decode an Encoded URI in Xslt 2.0

I have a URI which contains some encoded data:


I have extracted this data into a variable, however I cannot find a way to decode this data so that it can be processed within my XSLT 2.0 stylesheet. I have tried using the EXSLT str:decode-uri function, however it does not appear to be supported by Saxon.

The variable should read: "abc def"     and NOT "abc%20def"

Simply replacing all occurrences of %20 with a space is not an option. All codepoints need to be correctly decoded.

Is there a native way of achieving this in XSLT 2.0? If not what alternatives are there?
1 Solution
numberkruncherAuthor Commented:
I think that I have now solved the problem with the following source:
<!-- Function: doc:hex-to-dec -->
<!-- Description: Convert hexadecimal xs:string into xs:integer -->
<!-- Based on ref: http://www.liddicott.com/~sam/?p=52 -->
<xsl:function name="doc:hex-to-dec" as="xs:integer">
    <xsl:param name="hex" as="xs:string"/>
    <xsl:variable name="length" select="string-length($hex)" as="xs:integer"/>
    <xsl:value-of select="
        if ($length &gt; 0) then
            if ($length &lt; 2) then
                string-length(substring-before('0 1 2 3 4 5 6 7 8 9 AaBbCcDdEeFf',$hex)) idiv 2
                doc:hex-to-dec(substring($hex,1,$length - 1))*16+doc:hex-to-dec(substring($hex,$length))
<!-- Function: doc:decode-uri-component -->
<!-- Description: Decode URI component. -->
<xsl:function name="doc:decode-uri-component" as="xs:string">
    <xsl:param name="uri-component" as="xs:string"/>
    <xsl:variable name="decoded-component" as="xs:string*">
        <xsl:analyze-string select="$uri-component" regex="%(\d\d)">
                <xsl:value-of select="codepoints-to-string(doc:hex-to-dec(regex-group(1)))"/>
                <xsl:value-of select="."/>
    <xsl:value-of select="string-join($decoded-component,'')"/>

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now