printf() Function (Python, SLAX, and XSLT)
Namespaces
http://xml.juniper.net/junos/commit-scripts/1.0 http://xml.libslax.org/slax
Python Syntax
jcs.printf(expression)
SLAX Syntax
expr prefix:printf(expression);
XSLT Syntax
<xsl:value-of select="prefix:printf(expression)"/>
Description
Generate formatted output text. The function supports most standard printf
formats, in addition to some Junos OS–specific formats. The function
returns a formatted string but does not print it on call. To use the
following Junos OS modifiers, place the modifier between the percent
sign (%) and the conversion specifier.
-
jc—Capitalize the first letter of the associated output string. -
jh—(SLAX scripts only) Transform large numbers into a human-readable format by applying a divisor of 1024 and adding the appropriate unit suffix (K, M, G, T, and so on). For example, a value of 65536 becomes 64K. -
jH—(SLAX scripts only) Transform large numbers into a human-readable format by applying a divisor of 1000 and adding the appropriate unit suffix (K, M, G, T, and so on). For example, a value of 65536 becomes 66k. -
j1—Emit a field only if it changed from the last time the function was called. This assumes that the expression’s format string is unchanged. -
jt{TAG}—Emit the tag if the associated argument is not empty. -
j{flag1, flag2, ...}—(SLAX scripts only) When using the%jhor%jHformats, apply one or more additional flags that modify the output.-
Values:
-
b—Append 'B' (bytes) as the suffix when the value is less than 1000. -
suffix=string—Append the given suffix after the number and the unit suffix. -
whole—Round the value to the nearest integer value.
-
-
The prefix associated with the namespace URI should be defined in the prefix-to-namespace mapping in the style sheet.
Parameters
expression |
Format string containing an arbitrary number of format specifiers and associated arguments to output. |
Usage Examples
In the following XSLT example, the j1 operator
suppresses printing the interface identifier ge-0/0/0 in the second
line of output, because the value has not changed from the first
printing. The jc operator capitalizes the output
strings up and down. The
jt{--} operator does not print the
{--} tag in the first line of output,
because the associated output argument is an empty string. However,
the operator prints the tag in the second line because the
associated output is the non-empty string test.
<xsl:value-of select="jcs:printf('%-24j1s %-5jcs %-5jcs %s%jt{ -- }s\n',
'ge-0/0/0', 'up', 'down', '10.1.2.3', '')"/>
<xsl:value-of select="jcs:printf('%-24j1s %-5jcs %-5jcs %s%jt{ -- }s\n',
'ge-0/0/0', 'down', 'down', '10.1.2.3', 'test')"/>The function produces the following output:
ge-0/0/0 Up Down 10.1.2.3
Down Down 10.1.2.3 -- test
The following SLAX version 1.3 script formats some data values using the
%jh and %jH flags for
human-readable output.
version 1.3;
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";
ns slax = "http://xml.libslax.org/slax";
import "../import/junos.xsl";
var $data := {
<n> 512;
<n> 4096;
<n> 1048576;
<n> 1073741824;
}
main <data> {
for-each $data/* {
<test n=.> {
<out> slax:printf("%jh%12s", .);
<out> slax:printf("%jH%12s", .);
<out> slax:printf("%j{b}h%12s", .);
<out> slax:printf("%j{space,whole}H%-12s", .);
<out> slax:printf("%j{space,suffix=B}h%-12s", .);
}
}
}The script produces the following output:
<data>
<test n="512">
<out> 512</out>
<out> 512</out>
<out> 512B</out>
<out>512 </out>
<out>512 B </out>
</test>
<test n="4096">
<out> 4.0K</out>
<out> 4.1k</out>
<out> 4.0K</out>
<out>4 k </out>
<out>4.0 KB </out>
</test>
<test n="1048576">
<out> 1.0M</out>
<out> 1.0M</out>
<out> 1.0M</out>
<out>1 M </out>
<out>1.0 MB </out>
</test>
<test n="1073741824">
<out> 1.0G</out>
<out> 1.1G</out>
<out> 1.0G</out>
<out>1 G </out>
<out>1.0 GB </out>
</test>
</data>
Release Information
Function introduced in Junos OS Release 7.6.
Support for the slax namespace http://xml.libslax.org/slax added in Junos OS Release 12.2.
Support for Python added in Junos OS Release 16.1R1.
Support for %jh, %jH, and
%j{flag} added
in SLAX version 1.3.