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

At the last telcon, I was asked to provide a modified proposal for string templates (formerly known as smart quotes).

How's this?

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 is suitable for containing fragments of JavaScript, JSON,
CSS, SPARQL or other languages that might use curly braces, double and
single quotes, or be difficult or tedious to construct directly in
XQuery using other mechanisms.

[201] StringTemplate ::= "<[" . StringTemplateText "]>"
[203] StringTemplateText := ((Char* - "<[") | StringTemplateExpr )*
[xxx] StringTemplateExpr := "<{" Expr "}>"

An string template allows 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 ]>

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() }> }
        ]
      }
    }} ]>;

Example:

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

Example:

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




Received on Wednesday, 16 September 2015 19:28:27 UTC