The following code sample from the diagnose_bgp.pl and get_chassis_inventory.pl scripts uses XSLT to transform an operational response from the JUNOScript server into a more readable format. A detailed discussion of the functional subsections follows the complete code sample.
my $outputfile = $opt{o} || "";
my $xslfile = $opt{x} || "xsl/text.xsl";
if ($xslfile && ! -f $xslfile) {
die "ERROR: XSLT file $xslfile does not exist";
my $xmlfile = "$deviceinfo{hostname}.xml";
$res->printToFile($xmlfile);
my $nm = $res->translateXSLtoRelease('xmlns:lc', $xslfile, "$xslfile.tmp");
if ($nm) {
print "Transforming $xmlfile with $xslfile...\n" if $outputfile;
my $command = "xsltproc $nm $deviceinfo{hostname}.xml";
$command .= "> $outputfile" if $outputfile;
system($command);
print "Done\n" if $outputfile;
print "See $outputfile\n" if $outputfile;
}
else {
print STDERR "ERROR: Invalid XSL file $xslfile\n";
}
The first line of the preceding code sample illustrates how the scripts read the -o option from the command line to obtain the name of file into which to write the results of the XSLT transformation:
my $outputfile = $opt{o} || "";
From the -x command-line option, the scripts obtain the name of the XSLT file to use, setting a default value if the option is not provided. The scripts exit if the specified file does not exist. The following example is from the diagnose_bgp.pl script:
my $xslfile = $opt{x} || "xsl/text.xsl";
if ($xslfile && ! -f $xslfile) {
die "ERROR: XSLT file $xslfile does not exist";
For examples of XSLT files, see the following directories in the JUNOScript Perl distribution:
The actual parsing operation begins by setting the variable $xmlfile to a filename of the form routing-platform-name.xml and invoking the printToFile function to write the JUNOScript server’s response into the file (the printToFile function is defined in the XML::DOM::Parser module):
my $xmlfile = "$deviceinfo{hostname}.xml";
$res->printToFile($xmlfile);
The next line invokes the translateXSLtoRelease function (defined in the JUNOS::Response module) to alter one of the namespace definitions in the XSLT file. This is necessary because the XSLT 1.0 specification requires that every XSLT file define a specific value for each default namespace used in the data being transformed. The xmlns attribute in a JUNOS XML operational response tag element includes a code representing the JUNOS software version, such as 9.3R1 for the initial version of JUNOS Release 9.3. Because the same XSLT file can be applied to operational response tag elements from routing platforms running different versions of the JUNOS software, the XSLT file cannot predefine an xmlns namespace value that matches all versions. The translateXSLtoRelease function alters the namespace definition in the XSLT file identified by the $xslfile variable to match the value in the JUNOScript server’s response. It assigns the resulting XSLT file to the $nm variable.
my $nm = $res->translateXSLtoRelease('xmlns:lc', $xslfile, "$xslfile.tmp");
After verifying that the translateXSLtoRelease function succeeded, the function builds a command string and assigns it to the $command variable. The first part of the command string invokes the xsltproc command and specifies the names of the XSLT and configuration data files ($nm and $deviceinfo{hostname}.xml):
if ($nm) {
print "Transforming $xmlfile with $xslfile...\n" if $outputfile;
my $command = "xsltproc $nm $deviceinfo{hostname}.xml";
If the $outputfile variable is defined (the file for storing the result of the XSLT transformation exists), the script appends a string to the $command variable to write the results of the xsltproc command to the file. (If the file does not exist, the script writes the results to standard out [stdout].) The script then invokes the system function to execute the command string and prints status messages to stdout.
$command .= "> $outputfile" if $outputfile;
system($command);
print "Done\n" if $outputfile;
print "See $outputfile\n" if $outputfile;
}
If the translateXSLtoRelease function fails (the if ($nm) expression evaluates to “ false"), the script prints an error:
else {
print STDERR "ERROR: Invalid XSL file $xslfile\n";
}