1.3. XPath variables
Prev | 1. Reference | Next

### 1.3. XPath variables

Totally different from the above macro-variables. A reference to an XPath variable starts with "`$`" and can only occur in `match`, `test`, `get`, `set`.

#### 1.3.1. User variables

A user variable which name has no namespace or which name is in a namespace other than "`http://www.xmlmind.com/xmleditor/namespace/scope/view`" is local to the macro being executed.

A user variable which name is in the "`http://www.xmlmind.com/xmleditor/namespace/scope/view`" namespace uses the view of the document as its scope.

This kind of variable is persistent from an invocation of the macro to the other and/or can be shared with other macros when this macro and/or the other macros are executed in the document view in which the variable has been created.

A easy way to remember this is to consider that the name of this kind of variable is implicitly prefixed with the unique ID of the document view.

#### 1.3.2. Predefined variables

There are many predefined variables, most of them mapped to the selection in the document view in which the macro is executed.

Read example: `<get expression"$selectedChars"/>` returns selected text if any, the empty string otherwise.

Write example: `<set variable="dotOffset" expression="$dotOffset + 1"/>` moves the caret by one character to the right.

Variable | Value | Read | Write
--- | --- | --- | ---
implicitElement | element | Implicitly or explicitly selected element. Empty node set if there is no node selection or if multiple nodes are selected or if the selected node is not an element. | Selects specified element. If the value of the variable is not a valid node set[a], clears the node selection.
selectedElement | element | Explicitly selected element. Empty node set if there is no node selection or if multiple nodes are selected or if the selected node is not an element. | Selects specified element. If the value of the variable is not a valid node set[a], clears the node selection.
implicitNode | node | Implicitly or explicitly selected node. Empty node set if there is no node selection or if multiple nodes are selected. | Selects specified node. If the value of the variable is not a valid node set[a], clears the node selection.
selectedNode | node | Explicitly selected node. Empty node set if there is no node selection or if multiple nodes are selected. | Selects specified node. If the value of the variable is not a valid node set[a], clears the node selection.
selectedChars | string | Characters contained in the text selection. Empty string if there is no text selection. | Selects text starting at first textual node of specified node set and ending at last textual node of specified node set. If the value of the variable is not a valid node set[a], clears the text selection.
selectedNodes | node set containing sibling nodes | Nodes contained in the node selection. Empty node set if there is no node selection. | Selects specified nodes. Does nothing if specified nodes are not adjacent siblings. If the value of the variable is not a valid node set[a], clears the node selection.
selected | node | First selected node in the node selection (first in document order, not first selected by user). Empty node set if there is no node selection. | Clears the node and text selections. Selects specified node. If the value of the variable is not a valid node set[a], clears the node selection.
selected2 | node | Last selected node in the node selection (last in document order, not last selected by user). Empty node set if there is no node selection or if the node selection contains a single node. | Extends node selection to specified node. Does nothing if there is no node selection or if specified node is not a sibling of selected nodes. If the value of the variable is not a valid node set[a], clears the node selection.
dot | text, comment or PI node | Textual node containing the caret. Empty node set if the document does not contain text, comments or PIs. | Moves caret at the beginning of specified textual node.
dotOffset | integer | Offset of the caret within the textual node containing it. First offset is 0. Last valid offset is after last character. -1 if the document does not contain text, comments or PIs. | Moves caret to specified offset.
mark | text, comment or PI node | Textual node containing the ``mark of text selection'' (text selection is between dot and mark). Empty node set if there is no text selection. | Clears the node selection. Moves the ``mark of text selection'' at the beginning of specified textual node. Specified offset is adjusted if it is outside the valid offset range. If the value of the variable is not a valid node set[a], clears the text selection.
markOffset | integer | Offset of the ``mark of text selection'' within the textual node containing it. -1 if there is no text selection. | Moves the ``mark of text selection'' to specified offset. Does nothing if there is no ``mark of text selection''. Specified offset is adjusted if it is outside the valid offset range.
clickedElement | element | Element on which the user has clicked. If the user has clicked on a text node, then this variable contains its parent element. Can only be used when the macro-command is bound to a mouse click or an application event with an origin point such as `drop`. | N/A
clickedNode | node | Node on which the user has clicked. Can only be used when the macro-command is bound to a mouse click or an application event with an origin point such as `drop`. | N/A
clipboard | string | String contained in the system clipboard if any, the empty string otherwise. | Copies string value to the system clipboard.
systemSelection | string | String contained in the system selection[b] if any, the empty string otherwise. | Copies string value to the system selection[b].
[a] The value of the variable must be a non empty node set. All nodes in this node set must be attached to the document for which the macro is executed. When a single node is needed, this node is the first node of the node set in document order. [b] The system selection is emulated using a private clipboard on non Unix/X-Window platforms

Prev | Up | Next
1.2. Macro-variables | Home | 2. Examples