The script element allows to a run a XED script in the context of a macro.
<script location = anyURI context =XPath expr. returning a node set: "/" argument0 =XPath expressionargument1 =XPath expressionargument2 =XPath expressionargument3 =XPath expressionargument4 =XPath expressionargument5 =XPath expressionargument6 =XPath expressionargument7 =XPath expressionargument8 =XPath expressionargument9 =XPath expression><![CDATA[XED source]]></script>
XED is a very small, very simple scripting language, leveraging the native XPath 1.0 implementation of XMLmind XML Editor, allowing to modify in place the document being edited. The reference manual of XED is found in Part II, “The XED scripting language” in XMLmind XML Editor - Support of XPath 1.0.
The source of the XED script may be found inside the script element. In such case, it's strongly recommended to use a CDATA section.
If the source of the XED script is not found inside the script element, then the location attribute is considered. This attribute must point to an external text file, typically having a .xed extension, containing the source of the script. External XED scripts are cached, therefore this is almost no performance penalty for using external script files.
The main difference between using an internal XED script and using an external XED script is that an internal XED script automatically inherits all the namespace declarations, except the default namespace declaration, which are in scope with the script element. Example:
<configuration
xmlns="http://www.xmlmind.com/xmleditor/schema/configuration"
xmlns:cfg="http://www.xmlmind.com/xmleditor/schema/configuration">
...
<script>
...
</script>is equivalent to:
<configuration
xmlns="http://www.xmlmind.com/xmleditor/schema/configuration"
xmlns:cfg="http://www.xmlmind.com/xmleditor/schema/configuration">
...
<script>
namespace cfg="http://www.xmlmind.com/xmleditor/schema/configuration";
...
</script>Note that the URI contained in the location attribute may have a fragment. This fragment specifies the name of the XED macro-command in XMLmind XML Editor - Support of XPath 1.0 to be executed.
This allows to store different scripts —that is, several unrelated XED macro-commands— in the same script file. Example: script file hello.xed contains 2 XED macros:
macro sayHello() {
message("Hello world!");
}
macro sayGoodbye() {
message("Goodbye world!");
}The following XXE macro-command allows to invoke sayGoodbye() found in script file hello.xed:
<command name="sayGoodbye">
<macro>
<sequence>
<script location="hello.xed#sayGoodbye" />
</sequence>
</macro>
</command>An equivalent XXE macro-command would be:
<command name="sayGoodbye">
<macro>
<sequence>
<script>
include "hello.xed";
sayGoodbye();
</script>
</sequence>
</macro>
</command>Attributes argument0 to argument9 are evaluated as XPath expressions in the context of the node specified using the context attribute. The results of the evaluations are passed to the script by the means of global variables script-argument0 to script-argument9.
If, for any reason, the evaluation of argument fails, the script is nevertheless evaluated. Simply global variables Iscript-argument are not defined. That's why these arguments are typically used as follows:I
set-variable("myOption", defined("script-argument7", "off"));If variable script-argument7 is defined, use its value, otherwise use string "off". See XPath extension function defined() in XMLmind XML Editor - Support of XPath 1.0.
The result of a script is specified in global variable script-result. The result of a script may be used in the subsequent commands contained in the macro by the means of the "%_" special variable. If a script does not set global variable script-result, then this script will not change the value of "%_" special variable.
Example:
<command name="SayHello">
<macro>
<sequence>
<script>
set-variable("script-result", "Hello world!");
</script>
<command name="alert" parameter="%_"/>
</sequence>
</macro>
</command>After it has been run, a script may affect the node or text selection by setting the following global variables:
| Variable | Value | Description |
|---|---|---|
script-selected | node set | Selects first node of the node set. |
script-selected2 | node set | Extends node selection to the first node of the node set. |
script-dot | node set whose first node is a textual node | Moves the caret inside the first node of the node set. This first node must be a textual node of any kind (text, comment, processing instruction). |
script-dot-offset | positive number; defaults to 0 | Specifies the offset of the caret within the textual node obtained using variable script-dot. |
selected-mark | node set whose first node is a textual node | Move the text selection mark inside the first node of the node set. This first node must be a textual node of any kind (text, comment, processing instruction). |
script-mark-offset | positive number; defaults to 0 | Specifies the offset of the text selection mark within the textual node obtained using variable script-mark. |
It's possible to clear a selection mark of any kind but the caret, by setting the corresponding variable to an expression which does not evaluate to a node set. Examples: set-variable("script-selected2", ""), set-variable("script-mark", false()).
Otherwise, if the value of any of the above global variables has an unusable type (e.g. for script-dot, an empty node set or a node set not starting with a textual node), then this variable is simply ignored and no error is reported.
Setting script-dot (respectively script-mark) without setting script-dot-offset (respectively script-mark-offset) cause the caret (respectively the text selection mark) to be located at offset 0.
Always insert newly created nodes. Always replace existing nodes by newly created nodes. Never reuse nodes which have been detached from the document. Use extension function copy() in XMLmind XML Editor - Support of XPath 1.0 when needed to. If you forget to do so, this is likely to break the undo manager of the XML editor.
For example, if you need to move an element from one document location to another, first copy the element to be deleted using copy(), then delete the element, finally insert the copy at the new location.
If you want to see what's printed by XED command message() in XMLmind XML Editor - Support of XPath 1.0, you need to set attribute trace to true on the macro element.