Bookmark and Share Share

<assign>

Use <assign> to assign values to a variable. Values may be:

  • Literal values

  • Fragments of another variable

  • A whole variable

You use XPath expressions to define the fragments or variables to assign or otherwise modify the resulting variable content.

Consider these general rules when using <assign>:

  • When working with complex variables that have namespaces, you must include either the exact namespace or *: as a namespace wildcard at each step in XPath expressions.

  • You can use <assign> to cast strings of well-formed XML to a document-type variable or vice versa. See Copy All of a Variable for an example.

For examples, see Assign Literal Values, Copy Fragments of a Variable or Copy All of a Variable.

Can Contain Empty
Allowed In mashup, else, elseif, for, foreach, if, macro, operation, sequence, while,

Attributes

Name Required Description
fromvariable  

The variable to copy. <assign> must have either a fromvariable, a fromexpr or a literal attribute.

fromexpr  

An expression identifying a variable or variable fragment and any functions to apply before copying the result. <assign> must have either a fromvariable, a fromexpr or a literal attribute.

literal  

A literal value to assign. <assign> must have either a fromvariable, a fromexpr or a literal attribute.

toexpr  

An XPath expression defining a specific node in a variable that is the target to be assigned. <assign> must have either a toexpr or an outputvariable attribute.

outputvariable  

The variable that is the target to be assigned. <assign> must have either a toexpr or an outputvariable attribute.

<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.