Global Declarations

www.altova.com Print this Topic Previous Page Up One Level Next page

Home >  Programmers' Reference > Scripting Editor >

Global Declarations

The Global Declarations component is present by default in every Scripting Project (see screenshot below), and therefore does not have to be created. In order to add variables and functions to the Global Declarations script of a Scripting Project, you need to open the Global Declarations script and add the code fragment to the Global Declarations script. See Components of a Scripting Project and Creating a Scripting Project for more information.

 

ScrGlobalDeclarations

 

To open the Global Declarations script of a Scripting Project, right-click the Global Declarations item in the Scripting Project Tree (screenshot above), and select Open. The Global Declarations script opens in the Main Window.

 

Note:        Every time a macro is executed or an event handler is called, global declarations are re-initialized.

 

Given below is an example function. Remember that creating a variable or function in the Global Declarations script makes this variable or function accessible to all Forms, Event Handlers, and Macros.

 

 

Example function

A function called RemoveAllNamespaces would have code like this:

 

function RemoveAllNamespaces(objXMLData)

{

 if(objXMLData == null)

         return;

         

 if(objXMLData.HasChildren)        {        

         var objChild;

         

         // spyXMLDataElement := 4

         objChild = objXMLData.GetFirstChild(4);

         

         while(objChild)        {

         RemoveAllNamespaces(objChild);

         

                         try        {

                                 var nPos,txtName;

                                 txtName = objChild.Name;

                         

                                 if((nPos = txtName.indexOf(":")) >= 0)        {

                                         objChild.Name = txtName.substring(nPos+1);

                                 }

                                 

         objChild = objXMLData.GetNextChild();

                         }

                         catch(Err)        {

                                 objChild = null;

                         }

                 }

         }

 }                                                                                                

 

 

 

Note:

 

It is possible to define local variables and helper functions within macros and event handlers. Example:

 

//return value: true allows editing

//return value: false disallows editing

var txtLocal;

function Helper()

{

 txtMessage = txtLocal;

 Application.ShowForm("MsgBox");

}

function On_BeforeStartEditing(objXMLData)

{

 txtLocal = "On_BeforeStartEditing()";

 Helper();

}

 

Recursive functions are supported.

 


© 2019 Altova GmbH