<assign> Examples
Assign Literal Values
You can assign a literal to a variable, identified by name, or to a specific node within a variable, identified by an XPath expression. For example:
<!-- assign literal to a variable -->
<assign literal="Business" outputvariable="$storyType"/>
<!-- assign literal to a node -->
<assign literal="today" toexpr="$someVariable/someNode/date"/>
Copy Fragments of a Variable
The syntax to copy fragments from one variable uses an XPath expression to identify the specific nodes to copy. For example:
<variables>
<variable name="queryResult" type="document"/>
<variable name="itemNames" type="document"/>
<variable name="rotorItems" type="document"/>
</variables>
...
<assign fromexpr="$queryResult/items/item/name"
outputvariable="$itemNames"/>
...
<assign fromexpr="$queryResult/items/item[contains(name,'rotor')]"
outputvariable="$rotorItems"/>
The first example selects all item name nodes. The second example uses the predicate [contains(name,'rotor')] to select only item nodes whose name contains ’rotor’ somewhere in the value.
The previous examples copied fragments to a variable. Fragments can also be copied to a specific node within a variable using the toexpr attribute and an XPath expression. For example:
<assign fromexpr="$queryResult/items/item[1]/name"
toexpr="$results/first/name"/>
Copy All of a Variable
You can also use <assign> to copy an entire variable to another variable or to a specific node in a variable. Use the fromvariable attribute to identify the variable to copy.
One common use case for this is to cast a string of well-formed XML to a document-type. For example:
<variables>
<variable name="queryResult" type="string"/>
<variable name="result" type="document"/>
</variables>
...
<assign fromvariable="$queryResult" outputvariable="$result"/>
You can also cast documents to well-formed XML strings.
Replace Existing Values
Another common usage is to use <assign> to apply an XPath function and replace existing data within a variable. For example, to change the case of one or more nodes.
<assign fromexpr="$result/orders/order/shipCode/upper-case()"
toexpr="$result/orders/order/shipCode"/>
Working Samples
Many sample mashups for MashZone NextGen use the <assign> statement. Some examples of particular interest include:
![*](chapterTOC_bullet.png)
AssignExpressionsSample (
assignments.emml) for basic <assign> examples
![*](chapterTOC_bullet.png)
DateTimeOperations (
datetime.emml) for a sample using an XPath function
![*](chapterTOC_bullet.png)
GoogleFinanceNews (
googlefinancenews.emml) for <assign> examples illustrating many different XPath expressions
![*](chapterTOC_bullet.png)
MacrosSample (
macros.emml) and example using a dynamic mashup expression
See
Mashup Samples for a list of
MashZone NextGen mashup samples and where to find them.