Typically it is not necessary to explicitly convert from one data type into another. The primary exception to this rule is converting from result tree fragment to node-set, but otherwise most conversions occur automatically.

When the script engine comes to an operator or a statement, and the associated data type is not of the correct type, the script engine attempts to automatically convert it. As an example, when the addition operator is encountered, the two arguments are converted into numbers.

The conversion process works in the following way, based on the original data type:

  • string: strings which consist entirely of appropriate characters for numeric content are converted into the equivalent number, otherwise they are converted to NaN (not a number). When converting to boolean, empty strings convert to false, and non-empty strings convert to true.
  • number: numbers are converted to strings by converting each digit into the appropriate character. The numeric value zero is converted to the boolean value false, all other numeric values are converted to the boolean value true.
  • boolean: when converted to strings, booleans become either “true” or “false.” The boolean value false is converted to the numeric value of 0. True is converted to the numeric value of 1.
  • node-set: a node-set is converted into a string by returning the string value of the first node in the node-set. The string value is the text contents of the node as well as all its child nodes. A node-set converts into a number in a similar fashion. Empty node-sets are converted to the boolean value of false, node-sets with one or more nodes are converted to the boolean value of true.
  • result-tree fragment: result tree fragments are converted to strings by returning all the text content within the XML data structure.

Result Tree Fragment to Node-Set Conversion
var $new-node-set := $rtf-variable;
A result tree fragment contains an unparsed XML data structure. It is not possible to retrieve any of the embedded XML information from this data type, so the := conversion operator was created. This operator converts a variable from a result tree fragment into a node-set. The script can then tell Junos to search the node-set for the appropriate information and extract it.

Data type conversion can be seen in the script shown to the right.

version 1.0;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

match / {
    <op-script-results> {
        /* String variable */
        var $numeric-string = "-700";        
        /* Number variable */
        var $number = 100;        
        /* Output the addition of the two variables to the console */
        <output> $numeric-string + $number;