04-04-2012 09:53 AM - edited 04-04-2012 11:12 AM
I’m having problems looping through get-interface-information output loaded from a file. I get the expected output when looping through the output from RPC call directly.
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"; ns exsl extension = "http://exslt.org/common"; import "../import/junos.xsl"; match / { <op-script-results> { var $file = document("/var/tmp/interface_stats.xml"); for-each ( $file/physical-interface ) { if (oper-status == "up") { <output> "Interface " _ name; } } } }
The file appears almost-identical to what you get from the RPC call:
user> file show /var/tmp/interface_stats.xml
<?xml version="1.0"?>
<interface-information xmlns="http://xml.juniper.net/junos/10.4R9/junos-interfac
<physical-interface>
<name>ge-0/0/0</name>
<admin-status junos:format="Enabled">up</admin-status>
<oper-status>down</oper-status>
<local-index>138</local-index>
<snmp-index>506</snmp-index>
<generation>141</generation>
[output truncated]
user> show interfaces detail | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.2R1/junos">
<interface-information xmlns="http://xml.juniper.net/junos/11.2R1/junos-interfac
<physical-interface>
<name>ge-0/0/0</name>
<admin-status junos:format="Enabled">up</admin-status>
<oper-status>down</oper-status>
<local-index>138>/local-index>
<snmp-index>506</snmp-index>
<generation>141</generation>
[output truncated]
I've messed about with the location path operators to no avail. I’m obviously missing something but I'm struggling to see what!
Cheers,
Sam
Just to add I do have interfaces that are up so it's not the if logic.
Also, the script runs there's just no output.
04-04-2012 11:40 AM
The node-set returned by the document() function will be pointing at the root node of the XML document rather than the <interface-information> element like the RPC results would be, so you need to include the full path in your location path:
$file/interface-information/physical-interface
04-04-2012 12:34 PM
Hey ccall, thanks for the reply.
Unfortunately it's no better.
I've simplied the code:
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"; ns exsl extension = "http://exslt.org/common"; import "../import/junos.xsl"; match / { <op-script-results> { var $file = document("/var/tmp/interface_stats.xml"); for-each ( $file/interface-information/physical-interface ) { <output> .; } } }
But still no output ![]()
No issues present in the trace file (attached).
Cheers,
Sam
04-05-2012 07:49 AM
After reading the automation ref, document() doesn’t appear to work as described.
I’ve manage to do what I need to do though using local- name(). Here’s the code for anyone else that might be struggling with this:
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> { var $stats = document("/var/tmp/interface_stats.xml"); var $ifd = $stats//*[local-name() == "physical-interface"]; for-each ($ifd){ var $oper = ./*[local-name() == "oper-status"]; if ($oper == "up") { var $ifd_name = ./*[local-name() == "name"]; <output> $ifd_name; } } } }
Cheers,
Sam