home contribute faq download

FunctX XSLT Functions

fn:encode-for-uri

Encodes reserved characters for use in the path of a URI

Google
Webxsltfunctions.com

Description

The fn:encode-for-uri function encodes reserved characters for use in the path of a URI. URIs require that some characters be escaped with their hexadecimal Unicode code point preceded by the % character. This includes non-ASCII characters and some ASCII characters, namely control characters, spaces, and several others.

In addition, certain characters in URIs are separators that are intended to delimit parts of URIs, namely the characters ;,/?:@&=+$[]%. If the intended use of a string is as a segment of a URI path, where such separators have special meaning, the fn:encode-for-uri allows you to replace these escape these separator characters, while also escaping the other necessary special characters.

Like the fn:escape-html-uri and fn:iri-to-uri functions, this function replaces each special character with an escape sequence in the form %xx, where xx is two hexadecimal digits (in uppercase) that represent the character in UTF-8. For example, édition.html is changed to %C3%A9dition.html, with the é escaped as %C3%A9.

This function is the most aggressive of the three encoding functions. All characters except the following are escaped:

  • letters a through z and A through Z
  • digits 0 through 9
  • hyphen (-), underscore (_), period (.), tilde (~)

This description is © Copyright 2007, Priscilla Walmsley. It is excerpted from the book XQuery by Priscilla Walmsley, O'Reilly, 2007. For a complete explanation of this function, please refer to Appendix A of the book.

Arguments and Return Type

NameType
$uri-part xs:string?
return value xs:string

Examples

XPath ExampleResults
encode-for-uri(
   'Sales % Numbers.pdf')
Sales%20%25%20Numbers.pdf
encode-for-uri(
   'http://datypic.com/a%20URI#frag')
http%3A%2F%2Fdatypic.com%2Fa%2520URI%23frag

See Also

fn:escape-html-uriEscapes all characters except printable ASCII characters
fn:iri-to-uriConverts an IRI into a URI

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26W3C, XQuery 1.0 and XPath 2.0 Functions and Operators, http://www.w3.org/TR/xpath-functions/
Datypic XSLT Services

Recommended Reading:

XQuery