Wiki source for CommandTie


Show raw source

======Command tie======

====Name====
**tie** Ties an xquery expression to a variable for ease of access

====Synopsis====
tie variable expression
tie variable


====Description====
Tie associates an xquery expression to a variable so it can be queried more with a simplier syntax.
In the second form (with one argument only) the variable is untied.

The expression is any xquery expression and should be quoted or escaped. The expression is evaluated whenever the variable is accessed with either
the syntax

%%
${variable:arg}
%%

or
%%
${variable[index]:arg}
%%

In the first case, the variable becomes context node for the xquery, and must be a single Item (may not be a sequence).
In the second case the variable is treated as a sequence, and the index'd element (1 based) is used as the context item.

In both cases, the text following the : is treated as a string and assigned to the $_ variable.

====Examples====

tie is very useful to access property or map like xml data.

For example, create a property object using xproperties


%%
A=$<( xproperties -a a="A value" -a b="B value")
xecho $A
%%

Results
%%(xml)
<properties version="1.0">
<entry key="b">B value</entry>
<entry key="a">A value</entry>
</properties>
%%

Now tie the variable A

%%
tie A './/entry[@key = $_ ]/string()'
%%

Now properties can be referenced by key value and returned as strings
%%
echo ${A:a}
%%

Result
%%
A value
%%

Compare this to the equivalent expression needed to extract a property value

%%
echo <[ $A//entry[@key="a"]/string() ]>
%%


====Notes====

Tied variables survive resetting of the variable, so you can update variables and the tied expression is maintained.
Unlike the <[ ]> syntax, the tied xquery does NOT have access to the shell variables.




----
[[Commands]]
[[CategoryCommands]]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki