<annotate> Examples
<annotate> Basics
You must identify the variable you want to work with and use an XPath expression to identify the specific node(s) within that variable to annotate.
<annotate variable="$distributionLists" expr="/lists/list" >
</annotate>
Then define the annotations within the body of <annotate>.
Define a New Child
Use the element keyword followed by the name for the new element and a dynamic mashup expression for the new element’s value. New children are always appended after any existing children.
This example defines a new element with a literal name and value:
<annotate variable="$vendors" expr="/vendor/site" >
element category { "metals" }
</annotate>
The result would look something like this:
<category>metals</category>
Note: | If the parent element you are annotating has a namespace, the new element does not inherit this namespace unless you specify the namespace in the <annotate> statement. If you do not specify the namespace, the new child is added in the default namespace and has xmlns="" added as an attribute. |
You can also set element names or values dynamically and set the namespace for new children. See
Set Names or Values Dynamically or
Define Children with Namespaces for other examples.
Define a New Attribute
Use the attribute keyword followed by the name for the new attribute and a dynamic mashup expression to set the attribute’s value. This example adds an attribute with a literal name and value:
<annotate variable="$distributionLists" expr="/lists/list" >
attribute corporate { "yes" }
</annotate>
Set Names or Values Dynamically
Both the name and the value for new attributes or elements can be set dynamically in
Dynamic Mashup Expressions. The result of the dynamic expression must evaluate to a simple value. For example:
<annotate variable="$vendors" expr="/vendor/site" >
element geo:lat { $georesult//y:Latitude/string() },
element geo:long { $georesult//y:Longitude/string() }
</annotate>
<annotate variable="$distributionLists" expr="/lists/list" >
attribute server { $emailServers/servers/server/name }
</annotate>
<annotate variable="$notifications" expr="/notification" >
attribute { $property } { $colorScheme }
</annotate>
Define Children with Namespaces
You can also specify a namespace for new elements or attributes using the QName(namespace, node-name) function in the syntax for dynamic names.
This example adds an element with a literal namespace and element name:
<annotate variable="$payload" expr="/*:books/*:book" >
element {QName("http://www.books.com/xmlns", "review")}
{ "Five stars!" }
</annotate>
The namespace parameter for QName() should be a URL or URN. If this namespace is not the same namespace as the node being annotated, the child node that is added also has an xmlns attribute added with this namespace.
If the parent node that you are annotating, or any ancestor, has one or more existing namespaces, you can use the QName function combined with the XPath namespace-uri(node) function to have the new element or attribute inherit these namespaces. This example adds a new element, with a dynamic node name and value that inherits all namespaces within the existing result variable:
<annotate variable="$result" expr="/*:records/*:record" >
element {QName(namespace-uri($result/*), $nodeName)}
{ $newValue }
</annotate>
Make Multiple Annotations to the Selected Variable and Nodes
You can apply several annotations to the selected variable and targe node(s). Separate multiple annotations with commas. For example:
<annotate variable="$vendors" expr="/vendor/site" >
element geo:lat { $georesult//y:Latitude/string() },
element geo:long { $georesult//y:Longitude/string() }
</annotate>
Working Samples
The following sample mashups use the <annotate) statement:
Annotation (
annotate.emml) for the basics
Annotation (
annotate2.emml) for an example with a namespace
UserDefinedFunctionSamle (
soundex.emml) for an examle with an XPath expression
See
Mashup Samples for a list of
Presto mashup samples and where to find them.