Wiki source for Variables


Show raw source

======Variables======
xmlsh variables can be either strings or XML types (infoset, including sequences).
There is no limit to the size of variables besides JVM memory.
Variables are created using variable assignment ([[Variables]]). The type of the expression determins
if a variable is a string or XML variable. Variables can be re-assigned and this can change their type.

Example of string assignments
%%(shell)
A=string
A="string"
A='string'
B="another $A"
C=${B}
D=$(echo yet more strings)
E=$(ls)
%%

Example of XML assignments
%%(shell)

A=<[<foo/>]>
A=<[1,2,3,<foo>bar</foo>]>
A=$<(xls)
DOC=<[<foo><bar>text</bar></foo>]>
T=<[<spam>{$DOC//bar}</spam>]>

%%

====Sequence Construction====
Variable assignment supports a simplified sequence expression, similar to bash and ksh array assignment.
var=(elem elem ...)

For example
%%
$ var=(foo bar spam)
%%
Assigns an XML sequence of strings equivilent to
%%
$ var=<[("foo","bar","spam")]>
%%

The values inside () are expanded using Word expansion and Globbing before being assigned.
Note that this is different then non-sequence construction which doesn't do globbing (wildcard expansion).
Assuming there are 21 xml files in the current directory
%%
$ var=(*.xml)
$ echo <[ count($var) ]>
21
$ var=*.xml
$ echo <[ count($var) ]>
1


%%

Assigns var to be the sequence containing all xml files in the current directory.

The empty sequence construct creates an empty sequence. This is different then string assignment which creates a empty string.

%%
var=()
echo <[count($var)]>
%%
returns
%%
0
%%

%%
var=""
echo <[count($var)]>
%%
Returns
%%
1
%%

====Sequence Append====
Sequences can be appended using the syntax
%%
VAR+=value
%%
This is a syntax shortcut for
%%
VAR=($VAR,value)
%%
or
%%
VAR=<[ $VAR , value ]>
%%

If the variable doesn't exist it is created as if assigned using =.



Example: Create a sequence of integers 1 to 10 then append 20 to 30
%%
A=<[ 1 to 10 ]>
A+=<[ 20 to 30 ]>
echo $A
%%
Result
%%
1 2 3 4 5 6 7 8 9 10 20 21 22 23 24 25 26 27 28 29 30
%%

Example Create an element then append to it
%%
A=<[ <foo>text</foo> ]>
A+=<[ <bar>text</bar> ]>
xecho $A
%%

Result
%%(xml)
<foo>text</foo>
<bar>text</bar>
%%






====Array Notation====
xmlsh supports a simplified version of the bash array notation in variable expansion.

The expression ${variable[index]} expands to the index'd element of a sequence variable. Index starts at 1 for compatibility with xpath expressions. ${variable[index]} is equivalent to <[ $variable[index] ]>.

The expression ${#variable} expands to the sequence length of variable. ${#variable} is equivalent to <[ count($variable) ]>

A non-sequenced variable is equivalent to a sequenced variable of length 1.

Unlike bash, sparse arrays are not supported because array notation is just a syntactic simplification over the XML infoset sequence variables which cannot be sparse.

Examples

%%
var=(foo bar spam)
echo ${#var}
echo ${var[2]}
%%
returns
%%
3
bar
%%

%%
var=<[ "foo" , <spam attr="bar"><bletch>text</bletch></spam> , 1 , 2 , 3 ]>
echo ${#var}
xecho ${var[2]}
%%
returns
%%
5
<spam attr="bar"><bletch>text</bletch></spam>
%%


====Tied Variables====

See the [[CommandTie tie]] command for how to tie xquery expressions to variables.




----
See Also [[BasicSyntax Basic Syntax]]
See Also [[CommandTie tie]]
See Also [[BuiltinVariables Builtin Variables]]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki