XMLTemplate

XMLTemplate["string"]
yields a TemplateObject that represents an XML template to be applied using functions like TemplateApply.

XMLTemplate[src]
uses File[], URL[], or CloudObject[] as the source for the string template.

XMLTemplate[form,args]
yields a TemplateObject with arguments, suitable for cloud deployment or other evaluation.

Details and OptionsDetails and Options

  • The following basic tags can be used in the XML:
  • <wolfram:slot>TemplateSlot
    <wolfram:expr>TemplateExpression
    <wolfram:if>TemplateIf
    <wolfram:which>multiple TemplateIf
    <wolfram:sequence>TemplateSequence
    <wolfram:with>TemplateWith
    <wolfram:template>TemplateObject
    <wolfram:get>Get
    <wolfram:comment>comment to be ignored
    <wolfram:verbatim>verbatim XML
  • <wolfram:slot inserter=f> specifies the option setting InsertionFunction->f in TemplateSlot.
  • <wolfram:expr inserter=f> specifies the option setting InsertionFunction->f in TemplateExpression.
  • <wolfram:slot/> is equivalent to TemplateSlot[1].
  • <wolfram:if test=test>body</wolfram:if> specifies that body should be rendered if test evaluates to True.
  • <wolfram:which> ... </wolfram:which> can enclose any number of <wolfram:if ...> ... </wolfram:if>, and optionally a single <wolfram:else> ... </wolfram:else>. The "if" tests are evaluated in sequence, with the first one that evaluates to True being the final form of the "which".
  • <wolfram:sequence attr>body</wolfram:sequence> supports the following attributes:
  • values(required)list or association over which to repeat body
    slot1name of the template slot provided to body
    index2name of the position index provided to body
    delimitersSequence[]delimiters to insert between repeats of body
    defaultSequence[]form to use if there are no values
    inserterInheritedsetting for InsertionFunction option
  • <wolfram:with key1=val1 key2=val2 ...>body</wolfram:with> specifies that keyi should be replaced by vali in body when the template is applied.
  • <wolfram:template attr>body</wolfram:template> supports the following attributes, which define options for the TemplateObject to be used with body:
  • inserterInheritedsetting for InsertionFunction option
    combinerInheritedsetting for CombinerFunction option
  • <wolfram:get attr/> supports the following attributes:
  • path(required)file or URL path
    inserterInheritedsetting for InsertionFunction option
    combinerInheritedsetting for CombinerFunction option
  • XMLTemplate has the following options:
  • InsertionFunction"HTMLFragment"function or format to apply before inserting expressions
    CombinerFunctionStringJoinfunction to apply to combine pieces before returning a result
  • In XMLTemplate[File[]] and XMLTemplate[URL[]], the content of the source file etc. is imported as a string.

ExamplesExamplesopen allclose all

Basic Examples  (2)Basic Examples  (2)

Apply an XML template with slots named a and b:

In[1]:=
Click for copyable input
Out[1]=

An equivalent form using TemplateApply:

In[2]:=
Click for copyable input
Out[2]=

XMLTemplate supports the same syntax of StringTemplate; expressions inside <* ... *> are evaluated when the template is applied:

In[1]:=
Click for copyable input
Out[1]=

Include a template slot as well as an expression in the template string:

In[2]:=
Click for copyable input
Out[2]=

An equivalent syntax using XML tags:

In[3]:=
Click for copyable input
Out[3]=

String template syntax is useful when you need to inject data inside XML attributes:

In[4]:=
Click for copyable input
Out[4]=
Introduced in 2014
(10.0)