Open main menu

Wikibooks β

XQuery/Special Characters

< XQuery



You want to control where you put special characters such as newlines and quote characters in your XML files.


We will create XQuery variables (referents) to the decimal encoded character values using the &#NN' notation where NN is the decimal number for this character in the character set. We can then add these variables anywhere in the output stream.

Example ProgramEdit

In this example we will create a variable $nl had have it refer to the newline character. We will then put this in the middle of a string.

xquery version "1.0";
let $nl := "&#10;"
let $quote := "&#34;"
let $string := concat("Hello", $nl, "World")
return $string



The following shows how both quote and newline special characters can be created.

let $nl := "&#10;"
let $quote := "&#34;"
let $string := concat($quote, "Hello", $nl, "World", $quote)
return $string



Note that the string length of these variables string-length($nl) and string-length($quote) is only one character.

Other Useful Escape CharactersEdit

let $open-curly := '&#123;' (: for { :)
let $closed-curly := '&#125;' (: for } :)
let $space := '&#32;' (: space :)
let $tab := '&#9;' (: tab :)
let $ampersand := '&#38;' (: ampersand :)
let $zwsp := '&#8203;' (: zero width space :)

Using Zero Width Space Characters for Line BreaksEdit

If you have URLs that appear in the text to ensure they break gracefully when hyphenation is enabled:

let $zwsp := '&#8203;' (: this is the unicode character for a zero-width space :)
let $break-before-hint := replace($node, '([%?])', concat($zwsp, '$1'))
let $break-after-hint := 
   replace($break-before-hint, '([\.=&amp;])', concat('$1', $zwsp))

Contributed by Joe Wicentowski in March of 2014