Global Variables
Refer to the Global variables chapter of the sample file: Arbortext-path/samples/ALD/CountersAndVariables/countersAndVariables.xml. Each paragraph in the chapter contains a pair of varname elements, which each contain different settings for the value of the id and role attributes. In the associated stylesheet, the varname everywhere context includes direct PTC APP source code edits to set, display, use, and test the values of global variables. Note that the source code edits are not included in property sets, as in previous examples. Access the source code for the varname everywhere context by selecting it in the Elements list, and using the > > menu option.
The whole block of PTC APP source defined for the varname everywhere context is shown below, although each section is described separately later:
var attributes = arguments[0].attributes;
switch (attributes["role"]) {
case "set":
application.variables[attributes["id"]] = formatting.evaluateXPath
("string(.)");
break;
case "display":
formatting.write(" [ variable " + attributes["id"] + " = " + application
.variables[attributes["id"]] + " ] ");
break;
case "use":
var s = new fStyle(formatting.currentStyle);
s.color = application.variables[attributes["id"]];
formatting.styleSave();
formatting.styleChange(s);
formatting.write(" [ variable " + attributes["id"] + " = " + application
.variables[attributes["id"]] + " ] ");
formatting.styleRestore();
break;
case "test":
var s = new fStyle(formatting.currentStyle);
switch (application.variables[attributes["id"]]) {
case "small":
s.height = "8pt";
break;
case "medium":
s.height = "12pt";
break;
case "large":
s.height = "18pt";
break;
default:
s.height = "10pt";
break;
}
formatting.styleSave();
formatting.styleChange(s);
formatting.write(" [ variable " + attributes["id"] + " = " + application
.variables[attributes["id"]] + " ] ");
formatting.styleRestore();
break;
}
This block of code provides the following instructions:
• Set up the variable that will examine the attribute name and hold any value provided for the named attribute
See Set up variable for attribute information
You create the variable named attributes and specify that it will expect the values of attributes on the current element as its value.
• Examine the attribute role
See Examine the role attribute
• Configure the processing for role=”set”
See Set the value of the variable
• Configure the processing for role=”display”
See Display the value of the variable
• Configure the processing for role=”use”
See Use the value of the variable
• Configure the processing for role=”test”
See Test the value of the variable
Set up variable for attribute information
The relevant code phrase is given below:
var attributes = arguments[0].attributes;
Examine the role attribute
The relevant code phrase is given below:
switch (attributes["role"]) {
Use a switch statement to create a list of possible options (cases) for processing the value of the role attribute.
The switch statement works in three stages:
1. The value of the role attribute on the current element is evaluated
2. The value of role is compared with the values for each case entry, i.e. for values set, display, use, and test
3. If the value of role matches a case entry in the switch statement, the block of code associated with that case is executed.
An entry of break prevents one case from running into the next
Note the closing curly bracket at the end of the section configuring the processing of role, i.e. just after the final break in the whole source code block.
Set the value of the variable
Sample source from countersAndVariables.xml:
Here you configure the processing of the role attribute when its value is “set”. The relevant code phrase is given below:
case "set":
application.variables[attributes["id"]] = formatting.evaluateXPath("string(.)");
break;
Here you are setting the value of a variable to the value of the content of the current element, i.e. varname. The value of the id attribute on the current element identifies the variable that should hold this information.
With application.variables[attributes["id"]] you pass the current value of the id attribute of the current element as the value of the variables property of the fApplication object.
So, if the current element is <varname id=”var1” role=”set”>variable content</varname>, you are creating a variable called var1.
The phrase = formatting.evaluateXPath("string(.)") specifies that the evaluateXPath() method of the fFormatting object should be called to extract the information that forms the value of the named variable. The evaluateXPath() method will evaluate the expression string(.), which concatenates all child text of the current element.
So, if the current element is <varname id=”var1” role=”set”>variable content</varname>, the expression will extract the string variable content. The current value of the variable var1 is now variable content.
Display the value of the variable
Sample source from countersAndVariables.xml:
Here you configure the processing of the role attribute when its value is “display”. The relevant code phrase is given below:
case "display":
formatting.write(" [ variable " + attributes["id"] + " = " +
application.variables[attributes["id"]] + " ] ");
break;
Here you are outputting the value of a variable in the specific format [ variablename = content of current element ] . The value of the id attribute on the current element identifies the variable that should hold this information.
• formatting.write() calls the write() method of the fFormatting object to output specified information.
• " [ variable " outputs the text [ variable.
• + attributes["id"] outputs the value of the id attribute for the current element. The + appends it to the string output by the previous phrase. So if the current element is <varname id=”var1” role=”display”></varname>, the text var1 is displayed. The output string now reads [ variable var1.
• + " = " outputs the text = . The + appends it to the rest of the extracted string. The string now reads [ variable var1 =
• With application.variables[attributes["id"]] you reference the value of the variables property of the fApplication object. This was set to the content of the previous element with the processing of the previous role=”set” attribute (refer to Set the value of the variable for information). This phrase will therefore output the content of the previous element. So, if the previous element is <varname id=”var1” role=”set”>variable content</varname>, the text variable content is displayed. The + appends it to the rest of the extracted string. The string now reads [ variable var1 = variable content
• + " ] " appends the character ] to the rest of the extracted string. The string now reads [ variable var1 = variable content] in its entirety.
Use the value of the variable
Sample source from countersAndVariables.xml:
Here you configure the processing of the role attribute when its value is “use”. The relevant code phrase is given below:
case "use":
var s = new fStyle(formatting.currentStyle);
s.color = application.variables[attributes["id"]];
formatting.styleSave();
formatting.styleChange(s);
formatting.write(" [ variable " + attributes["id"] + " = " +
application.variables[attributes["id"]] + " ] ");
formatting.styleRestore();
break;
Here you are using the value of a variable as the value of a color property for text in the current element. Once evaluated to set the text height, the value of the variable is output in the specific format [ variablename = content of current element ]. The string is styled with the new text color.
• var s = new fStyle(formatting.currentStyle); creates a new text style based on the fStyle object. The fStyle() constructor is called with the keyword new to create the new object, with the currentStyle property of the fFormatting object reflecting the values of style properties that are currently in effect. A JavaScript variable named s is created to hold details of the new style.
• s.color = application.variables[attributes["id"]]; passes the value of the PTC APP global variable declared by the id attribute as the value of color property for the text style.
◦ s.color declares the color property for the text style. Details of the text style are held in the JavaScript variable named s.
◦ With = application.variables[attributes["id"]]; you reference the value of the id attribute of the current element as the value of the variables property of the fApplication object (PTC APP global variable). This was converted into the content of the previous element (i.e. blue) with the processing of the previous role=”set” attribute. The value of the color property for the text style is now set to blue.
• formatting.styleSave(); calls the styleSave() method of the fFormatting object to note current text properties.
Using styleSave() to mark current text properties means that any temporary change to these, for example changing text color for a few words, can be undone easily if required.
• formatting.styleChange(s); calls the styleChange() method of the fFormatting object to activate the style properties defined in the Javascript variable named s.
• formatting.write(" [ variable " + attributes["id"] + " = " + application.variables[attributes["id"]] + " ] "); outputs the value of a variable in the specific format [ variablename = content of current element ].
◦ formatting.write() calls the write() method of the fFormatting object to output specified information.
◦ " [ variable " outputs the text [ variable.
◦ + attributes["id"] outputs the value of the id attribute for the current element. The + appends it to the string output by the previous phrase. So if the current element is <varname id=”var3” role=”use”></varname> (see above), the text var3 is displayed. The output string now reads [ variable var3.
◦ + " = " outputs the text = . The + appends it to the rest of the extracted string. The string now reads [ variable var3 =
◦ With application.variables[attributes["id"]] you reference the value of the variables property of the fApplication object. This was set to the content of the previous element with the processing of the previous role=”set” attribute (refer to Set the value of the variable for information). This phrase will therefore output the content of the previous element. So, if the previous element is <varname id=”var3” role=”set”>blue</varname> (see above), the text blue is displayed. The + appends it to the rest of the extracted string. The string now reads [ variable var3 = blue
◦ + " ] " appends the character ] to the rest of the extracted string. The string now reads [ variable var3 = blue ] in its entirety. The string is displayed in blue text.
• formatting.styleRestore(); calls the styleRestore() method of the fFormatting object to indicate that the process will revert to the previous formatting properties.
Test the value of the variable
Sample source from countersAndVariables.xml:
Here you configure the processing of the role attribute when its value is “test”. The relevant code phrase is given below:
case "test":
var s = new fStyle(formatting.currentStyle);
switch (application.variables[attributes["id"]]) {
case "small":
s.height = "8pt";
break;
case "medium":
s.height = "12pt";
break;
case "large":
s.height = "18pt";
break;
default:
s.height = "10pt";
break;
}
formatting.styleSave();
formatting.styleChange(s);
formatting.write(" [ variable " + attributes["id"] + " = " +
application.variables[attributes["id"]] + " ] ");
formatting.styleRestore();
break;
You are using another switch statement to test the current value of a variable against a set of defined values to provide the height property for text in the current element. Once evaluated to set the text height, the value of the variable is output in the specific format [ variablename = content of current element ]. The string is styled with the matched text height.
• var s = new fStyle(formatting.currentStyle); creates a new text style based on the fStyle object. The fStyle() method is called to create the new object, with the currentStyle property of the fFormatting object reflecting the values of style properties that are currently in effect. A JavaScript variable named s is created to hold details of the new style.
• switch (application.variables[attributes["id"]]) { extracts the value of the PTC APP global variable declared by the id attribute and tests it against a set of value / property pairs to set text height.
Note the closing curly bracket after the final value in the set.
case "small":
s.height = "8pt";
break;
If the value of the PTC APP variable is small, set the value of the height property of the text style to 8pt. Pass that size to the Javascript variable s that holds the text style information.
case "medium":
s.height = "12pt";
break;
If the value of the PTC APP variable is medium, set the value of the height property of the text style to 12pt. Pass that size to the Javascript variable s that holds the text style information.
case "large":
s.height = "18pt";
break;
If the value of the PTC APP variable is large, set the value of the height property of the text style to 18pt. Pass that size to the Javascript variable s that holds the text style information.
default:
s.height = "10pt";
break;
If the value of the PTC APP variable is default, set the value of the height property of the text style to 10pt. Pass that size to the Javascript variable s that holds the text style information.
• formatting.styleSave(); calls the styleSave() method of the fFormatting object to indicate the start of text attribute changes.
• formatting.styleChange(s); calls the styleChange() method of the fFormatting object to activate the style properties defined in the Javascript variable named s.
• formatting.write(" [ variable " + attributes["id"] + " = " + application.variables[attributes["id"]] + " ] "); outputs the value of a variable in the specific format [ variablename = content of current element ].
◦ formatting.write() calls the write() method of the fFormatting object to output specified information.
◦ " [ variable " outputs the text [ variable.
◦ + attributes["id"] outputs the value of the id attribute for the current element. The + appends it to the string output by the previous phrase. So if the current element is <varname id=”var4” role=”test”></varname> (see above), the text var4 is displayed. The output string now reads [ variable var4.
◦ + " = " outputs the text = . The + appends it to the rest of the extracted string. The string now reads [ variable var4 = .
◦ With application.variables[attributes["id"]] you reference the value of the variables property of the fApplication object. This was set to the content of the previous element with the processing of the previous role=”set” attribute (refer to Set the value of the variable for information). This phrase will therefore output the content of the previous element. So, if the previous element is <varname id=”var4” role=”set”>large</varname> (see above), the text large is displayed. The + appends it to the rest of the extracted string. The string now reads [ variable var4 = large.
◦ + " ] " appends the character ] to the rest of the extracted string. The string now reads [ variable var4 = large ] in its entirety. The string is displayed in 18pt text.
• formatting.styleRestore(); calls the styleRestore() method of the fFormatting object to indicate that the process will revert to the previous formatting properties.