RE: String Templates (was RE: ACTION 614-12: Smart Quotes)

Very helpful feedback - thanks.

Take 2 ...

Jonathan

[128]  PrimaryExpr	   ::=   ... | StringTemplate

3.16.5.1 String Templates


A <term>String Template</term> is a string constructor that allows
embedded expressions, which are evaluated and used to create a string.

The syntax makes it simpler to construct strings containing
expressions in JavaScript, JSON, CSS, SPARQL, XQuery, XPath itself, or
other languages that might use braces, quotation marks, or other
strings used as delimiters in XPath.

[201] StringTemplate ::= '<[' StringTemplateText ']>'
[202] StringTemplateText ::= (StringTemplateChars,
                             (StringTemplateExpr, StringTemplateChars)*)
[nnn] StringTemplateChars ::= Char* - (Char* ('<{' | ']>') Char*)) 
[nnn] StringTemplateExpr ::= '<{' Expr '}>'

An string template can contain embedded expressions, which are called
string template expressions.

The string value of each string template expression $e is computed
using the expression string-join($e ! string(.), ' ').  
Thus, <[ <{ 1 to 3 }> ]> evaluates to the string "1 2 3".

When a string template is evaluated, string template text is treated
as literal text.  Line endings are processed as elsewhere in XQuery;
no other processing is performed on string template text. Each string
template expression is evaluated and converted to its string value,
then concatenated with string value text to create one string, which
is the value of the string template expression.

Note: 

  In string template text, & is not recognized as special, and < is
  only recognized when immediately followed by "{".  Thus, <[ &lt; ]>
  evaluates to the string " &lt; ", not the < character, and <[ <[ ]>
  evaluates to the string " <[ ".

Example:

for $s in ("one", "two", "red", "blue")
return <[<{$s}> fish ]>

evaluates to

"one fish two fish red fish blue fish "

Example:

The following example from json.org adds a session ID
member that is generated dynamically:

    declare variable $json := <[ {"menu": {
      "id": "file",
      "value": "File",
      "popup": {
        "menuitem": [
          {"value": "New", "onclick": "CreateNewDoc()"},
          {"value": "Open", "onclick": "OpenDoc()"},
          {"value": "Close", "onclick": "CloseDoc()"},
          {"callback": null },
          {"session-id": <{ get-session-id() }> }
        ]
      }
    }} ]>;

If the session id is 23, this evaluates to the following string:

 {"menu": {
    "id": "file",
    "value": "File",
    "popup": {
      "menuitem": [
        {"value": "New", "onclick": "CreateNewDoc()"},
        {"value": "Open", "onclick": "OpenDoc()"},
        {"value": "Close", "onclick": "CloseDoc()"},
        {"callback": null },
        {"session-id": 23 }
      ]
    }
  }} 

Example:

Embedded expressions can contain string templates to created nested
string templates.

Example:

<[ <{ $i, <[ literal text ]>, $j, <[ more literal text ]>  }> ]>

If $i is 1 and $j is 2, then this evaluates to:

" 1 literal text 2 more literal text  "

Received on Wednesday, 16 September 2015 20:49:58 UTC