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.
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 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 slot 1 name of the template slot provided to body index 2 name of the position index provided to body delimiters Sequence[] delimiters to insert between repeats of body default Sequence[] form to use if there are no values inserter Inherited setting 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:
-
inserter Inherited setting for InsertionFunction option combiner Inherited setting for CombinerFunction option - <wolfram:get attr/> supports the following attributes:
-
path (required) file or URL path inserter Inherited setting for InsertionFunction option combiner Inherited setting for CombinerFunction option - XMLTemplate has the following options:
-
InsertionFunction "HTMLFragment" function or format to apply before inserting expressions CombinerFunction StringJoin function 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.
Examples
open all close allBasic Examples (2)
Apply an XML template with slots named a and b:
XMLTemplate["first <wolfram:slot id='a'/> then <wolfram:slot id='b'/>"][<|"a" -> 1234, "b" -> 5678|>]An equivalent form using TemplateApply:
TemplateApply[XMLTemplate["first <wolfram:slot id='a'/> then <wolfram:slot id='b'/>"],
<|"a" -> 1234, "b" -> 5678|>]XMLTemplate supports the same syntax as StringTemplate; expressions in <*…*> are evaluated when the template is applied:
TemplateApply @ XMLTemplate["Values: <* Range[5] *>."]Include a template slot as well as an expression in the template string:
TemplateApply[
XMLTemplate["Range of `a`: <* Range[#a] *>."],
<|"a" -> 5|>
]An equivalent syntax using XML tags:
TemplateApply[
XMLTemplate["Range of <wolfram:slot id='a'/>: <wolfram:expr>Range[#a]</wolfram:expr>."],
<|"a" -> 5|>
]String template syntax is useful when you need to inject data inside XML attributes:
TemplateApply[
XMLTemplate["link: <a href='`url`'><wolfram:slot id='name'/></a>"],
<|"url" -> "http://wolfram.com", "name" -> "wolfram"|>
]Scope (4)
File is fully supported:
XMLTemplate[File["Examples/XML/Sequence.html"]]Templates can be hosted in the Wolfram Cloud:
template = CloudDeploy[XMLTemplate["My name is <wolfram:slot id='Name'/>."]]FileTemplate[template]The second argument of XMLTemplate can be used to bound data to the template:
t = XMLTemplate["a is <wolfram:slot id='a'/>, b is <wolfram:slot id='b'/>", <|"a" :> RandomReal[]|>];
TemplateApply[t]TemplateApply[t, <|"b" :> RandomReal[]|>]URL can be used to fetch templates:
template = URL @@ CloudExport["hello <wolfram:slot id='Name'/>", "String"]URLRead[template, "Body"]XMLTemplate[template]Options (2)
InsertionFunction (1)
The inserter attribute can be a function or a string chosen between $ExportFormats:
t = "
<script type='text/javascript'>
var planets= <wolfram:slot id='planets'>#planets</wolfram:slot>;
</script>
";
data = <| "planets" -> {
"Jupiter",
"Mars",
"Earth"
} |>;
TemplateApply[XMLTemplate[t, InsertionFunction -> "JSON"], data]The above code is equivalent to:
TemplateApply[XMLTemplate[t, InsertionFunction -> Function[body, ExportString[body, "JSON"]]], data]InsertionFunction can be anything:
TemplateApply[
XMLTemplate[
"<h1>Planet</h1>
<p><wolfram:slot id='planet'/> is beautiful</p>",
InsertionFunction -> ToUpperCase
],
<| "planet" -> "Jupiter"|>
]CombinerFunction (1)
The combiner function also accepts formats:
t = "<h1>Template</h1>
This is my <strong>html</strong>.
In javascript everything will be \"escaped\"
";
TemplateApply[XMLTemplate[t, CombinerFunction -> "JSON"]]The combiner function can be anything:
TemplateApply[XMLTemplate[t, CombinerFunction -> Composition[StringJoin, ToUpperCase]]]Applications (9)
<wolfram:slot id='...' inserter='...'>...</wolfram:slot>
XMLTemplate["value: <wolfram:slot id='1'/>"][1]Working with default and inserter:
ToMean[s_String] := StringTrim[s];
ToMean[l_List] := ToString @ N[Mean[l]];
t = XMLTemplate["
slot data: <wolfram:slot id='data' inserter='ToMean'>
No data found.
</wolfram:slot>
"];
TemplateApply[t]TemplateApply[t, <|"data" -> Range[10, 15]|>]<wolfram:expr inserter='...'>...</wolfram:expr>
XMLTemplate["mean: <wolfram:expr>Mean[#1]</wolfram:expr>"][Range[10]]Working with data and inserter:
ToJSON[Missing] := "null";
ToJSON[expr_] := ExportString[expr, "JSON"];
t = XMLTemplate["
<script type='text/javascript'>
var planets = <wolfram:expr inserter=ToJSON>Rule @@@ Transpose[
{#planets, #colors}
]</wolfram:expr>;
</script>
"];
TemplateApply[t, <|"planets" -> {"Mercury", "Earth", "Mars"}, "colors" -> {"Grey", "Light Blue", "Red"}|>]<wolfram:if test='...'>...</wolfram:if>
t = XMLTemplate["This is planet <wolfram:slot id='planet'/>. <wolfram:if test='#beautiful'>It's so beautiful to me.</wolfram:if>"];
TemplateApply[t, <|"planet" -> "Venus"|>]TemplateApply[t, <|"planet" -> "Mars", "beautiful" -> True|>]t = XMLTemplate["This is planet <wolfram:slot id='planet'/>. <wolfram:if test='#planet === #home'>It's our home planet.</wolfram:if>"];
TemplateApply[t, <|"planet" -> "Earth", "home" -> "Earth"|>]<wolfram:which><wolfram:if test='...'>...</wolfram:if></wolfram:which>
Use "which" to nest multiple "else if" clauses:
t = XMLTemplate["<wolfram:which>
<wolfram:if test='StringQ[#planets]'>This is a single planet.</wolfram:if>
<wolfram:if test='ListQ[#planets]'>This is a list of planets.</wolfram:if>
<wolfram:if test='MatchQ[#planets, Missing|_Missing]'>This planet is missing.</wolfram:if>
<wolfram:else>This is not a planet.</wolfram:else>
</wolfram:which>"];
TemplateApply[t]TemplateApply[t, <|"planets" -> {"Jupiter", "Mars", "Earth"}|>]TemplateApply[t, <|"planets" -> JustAnotherSymbol|>]<wolfram:sequence values='...' ...>...</wolfram:sequence>
t = XMLTemplate["<ul>
<wolfram:sequence values='#planets'>
<li><wolfram:slot id='1'/></li>
</wolfram:sequence>
</ul>"];
TemplateApply[t, <|"planets" -> {"jupiter", "mars", "earth"}|>]t = XMLTemplate["<ul>
<wolfram:sequence values='#planets' slot='planet' index='counter'>
<li><wolfram:slot id='counter'/>: <wolfram:slot id='planet'/></li>
</wolfram:sequence>
</ul>"];
TemplateApply[t, <|"planets" -> {"jupiter", "mars", "earth"}|>]CapFirst[a_String] := With[
{chars = Characters[a]},
StringJoin[ToUpperCase[First[chars]], Rest[chars]]
];
CapFirst[a_] := ToString[a];
t = XMLTemplate["<ul>
<wolfram:sequence values='#planets' index='counter' inserter='CapFirst'>
<li><wolfram:slot id='counter'/>: <wolfram:slot id='1'/></li>
</wolfram:sequence>
</ul>"];
TemplateApply[t, <|"planets" -> {"jupiter", "mars", "earth"}|>]Using the default value (a StringTemplate):
t = XMLTemplate["<ul>
<wolfram:sequence values='#results' index='counter' default='<li>No results for: `query`</li>'>
<li><wolfram:slot id='counter'/>: <wolfram:slot id='1'/></li>
</wolfram:sequence>
</ul>"];
TemplateApply[t, <| "query" -> "alpha centauri", "results" -> {} |>]t = XMLTemplate["Insert one of the following planets: <wolfram:sequence values='#1' delimiters=', '><wolfram:slot id='1'/></wolfram:sequence>."];
TemplateApply[t, {{"jupiter", "mars", "earth"}}]<wolfram:with key$1='val$1' ...>...</wolfram:with>
t = XMLTemplate["<wolfram:with mean='Round @ Mean[#data]' total='Total[#data]'>
Mean: <wolfram:slot id='mean'/>
Total: <wolfram:slot id='total'/>
</wolfram:with>
Data: <wolfram:slot id='data'/>"];
TemplateApply[t, <|"data" -> Fibonacci /@ Range[10, 15]|>]<wolfram:get path='...'><wolfram:slot id='...'/>...</wolfram:get>
Include a template from "path". "Path" is a StringTemplate:
t = XMLTemplate["<wolfram:get path='Examples/XML/with.html' />"];
TemplateApply[t, <|"data" -> Range[10]|>]Change insertion and combiner functions for the output:
capitalize = Composition[ToUpperCase, ToString];
strip[body_] := StringReplace[StringJoin[body], WhitespaceCharacter -> ""];
t = XMLTemplate["<wolfram:get combiner='strip' inserter='capitalize' path='Examples/XML/sequence.html'/>"];
TemplateApply[t, <|"data" -> {"jupiter", "mars", "earth"}|>]Extending a template. Example of a template for an HTML page:
Import["Examples/XML/Inheritance/main.html", "String", Path -> $TemplatePath]Using "get" to override slots:
TemplateApply @ XMLTemplate["
<wolfram:get path='Examples/XML/Inheritance/main.html'>
<wolfram:slot id='pageTitle'>Earth</wolfram:slot>
<wolfram:slot id='pageHeader'>Planet earth</wolfram:slot>
<wolfram:slot id='pageContent'>Read all about earth on <a href='http://en.wikipedia.org/wiki/Earth'>Wikipedia</a></wolfram:slot>
<wolfram:slot id='pageFooter'>Thanks for visiting our home planet</wolfram:slot>
</wolfram:get>"]<wolfram:template ...>...</wolfram:template>
Use "template" to create a fragment that overrides the insertion and combiner functions:
capitalize = Composition[ToUpperCase, StringJoin];
TemplateApply @ XMLTemplate["
<wolfram:template combiner='capitalize'>
<p>this is a block of text and <wolfram:slot>slots</wolfram:slot></p>
</wolfram:template>
"]Combine templates and edit an external template:
capitalize = Composition[ToUpperCase, StringJoin];
TemplateApply[
XMLTemplate["
<wolfram:template combiner='capitalize'>
<p>All text is capitalized</p>
<wolfram:get path='Examples/XML/if.html'/>
</wolfram:template>
"],
<| "data" -> "Earth" |>
]<wolfram:comment>...</wolfram:comment>
TemplateApply @ XMLTemplate["
<h1>About planet Mars</h1>
<wolfram:comment>
Insert planet mass here
</wolfram:comment>
<p>Mars is red</p>
"]TemplateApply @ XMLTemplate["
<h1>About planet Jupiter</h1>
<wolfram:comment>
Maybe we should improve this description
</wolfram:comment>
<p>Jupiter is big</p>
"]<wolfram:verbatim>...</wolfram:verbatim>
Use "verbatim" to avoid parsing. An optional insertion function is allowed:
capitalize = Composition[ToUpperCase, StringJoin];
TemplateApply @ XMLTemplate["
Sample usage of comment tag:
<wolfram:verbatim inserter='capitalize'>
<wolfram:comment>
This comment will (not) be deleted
</wolfram:comment>
</wolfram:verbatim>
"]Properties & Relations (1)
XMLTemplate evaluates to a TemplateObject:
XMLTemplate["My name is <wolfram:slot id='Name'/>."]Possible Issues (1)
Data in the second argument of XMLTemplate takes precedence over arguments in TemplateApply:
t = XMLTemplate["My name is <wolfram:slot id='a'/>.", <|"a" -> 1|>];
TemplateApply[t, <|"a" -> 2|>]Neat Examples (1)
You can deploy an XMLTemplate to the cloud. When you visit the URL, TemplateApply will render the template on each visit:
CloudDeploy[
XMLTemplate[
"now is: `Now`",
<|"Now" :> Now|>
]]Related Workflows
- Create a Webpage with Templates
History
Text
Wolfram Research (2014), XMLTemplate, Wolfram Language function, https://reference.wolfram.com/language/ref/XMLTemplate.html.
CMS
Wolfram Language. 2014. "XMLTemplate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/XMLTemplate.html.
APA
Wolfram Language. (2014). XMLTemplate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/XMLTemplate.html
BibTeX
@misc{reference.wolfram_2026_xmltemplate, author="Wolfram Research", title="{XMLTemplate}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/XMLTemplate.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_xmltemplate, organization={Wolfram Research}, title={XMLTemplate}, year={2014}, url={https://reference.wolfram.com/language/ref/XMLTemplate.html}, note=[Accessed: 12-June-2026]}