Topic: tech juniper ijaut prev next

tech juniper ijaut > 05: XML and XPath

Introduction to Junos Platform Automation and Devops

Module 05: XML and XPath

XML Document Format

Junos uses XML internally. CLI commands are converted into XML, executed on the system, and the XML output converted into human readable output.

The XML API provides an XML equivalent for all statements in the configuration and most operations mode commands.

XML namespaces ensure that element names and values are unique. The ‘xmlns’ attribute defines the namespace. XML documents support more than one namespace. For instance,

graphics:xmlns="..."

means that any tag starting ‘graphics:’ is from this other namespace.

An XSD (XML Schema Definition) files defines the elements in an XML document. XSD documents are themselves XML.

XML in the Junos OS

Display the internal XMLRPC for a command.

show ... | display xml rpc

Display the XML output for a command (don’t translate it for the CLI).

show ... | display xml rpc

Juniper publishes XSD’s for every Junos OS software release. The schema for configuration data can be retrieved via NETCONF;

<rpc>
    <get-xmn-information>
        <type>xml-schema</type>
        <namespace>junos-configuration</namespace>
    </get-xmn-information>
</rpc>
]]>]]>

There is a separate XSD for operational data.

XML defines a Document Object Model (DOM). An XML element consists of matching opening and closing tags and any included data. A document contains one root element.

XSLT/SLAC/Python use XPath to identify and locate elements in an XML document.

XPath axes: * Ancester * Parent * Self * Sibling * Child * Descendant

Path Syntax - which XML nodes to access based on their location in the hierarchy

Example:

system/login/user/uid

Predicate Syntax -

Example (within square brackets):

system/login/user[name=='lab']/uid

Attributes

Access attributes with ‘@’ in XPath.

system/login/user[@name=='lab']/uid

The ‘junos:changed’ attribute is attaced to nodes that have changes in the candidate configuration. ‘junos:group’ is attached to nodes that have inherited changes from a configuration group (these nodes will typically be below those which have changed).

XML and XPath Lab

Analysing XML operational mode output

show route 10.0.0.0/24 exact
show route 10.0.0.0/24 | display xml rpc
show route 10.0.0.0/24 | display xml

Analysing XML configuration output. Enter configuration mode and display the candidate configuration as an XML document.

configure
show | display xml

Using XPath

cat xpath_config.py

from jnpr.junos import Device
from lxml.etree import dump, _Element
from sys import argv
conf_xpath = argv[1] if len(argv) > 1 else "."
device = "..."
with Device(host=device) as dev:
    full_config = dev.rpc.get_config()
    filtered_config = full_config.xpath(conf_xpath)
    for entry in filtered_config:
        if type(entry) is _Eleemnt:
            dump(entry)
        else:
            print(entry)


python3 xpath_config.py
python3 xpath_config.py system/services
python3 xpath_config.py ./@changed-localtime
python3 xpath_config.py system/login/user/uid
python3 xpath_config.py "system/login/user[name='lab']/uid"
python3 xpath_config.py "system/login/user[1]/uid"
python3 xpath_config.py 'interfaces/interface[starts-with(name, "ge")]'
python3 xpath_config.py 'interfaces/interface[name="lo0" or name="fxp0"]'