On Fri, Mar 05, 2010 at 02:26:59PM +0300, Alexandre Snarskii wrote:
> On Wed, Jan 28, 2009 at 03:40:10PM -0500, Ross Vandegrift wrote:
> > Can you do that without providing a map that maps the abbreviated
> > namespace back to the fully-qualified namespace?  If so, I'd love to
> > know how.
> 
> Just run into the same problem myself, and workaround is here:
> you can use local-name() function to do namespace-agnostic 
> searches, and you can use partial checks on namespaces to be
> sure that you getting what you want. 
> 
> Example: iterate over physical interfaces:  
> 
>     <xsl:for-each select="//*[local-name()='physical-interface' and 
>         starts-with(namespace-uri(), 'http://xml.juniper.net/junos/')]">
> 
> Select operational state: 
> 
>         <xsl:variable name="oper"
>             select="normalize-space(*[local-name()='oper-status'])"/>
> 
> Select dropped packets for first queue: 
> 
>         
> select="normalize-space(*[local-name()='queue-counters']/*[local-name()='queue'
>  and 
> *[local-name()='queue-number']=0]/*[local-name()='queue-counters-red-packets'])"/>
> 
> 
> Syntax is ugly, but it works... 

I've been meaning to write up my solution to this, but haven't had the
chance.  It turns out that lxml exposes namespace assignments in a
programmatically accessible manner.

Building the map I mention above ends up being very easy and means I
can rid myself of all the absurd XSLT mangling I had to do.  I wish
the exposure of the namespace information had been more clearly
documented - or that I had noticed it earlier!

For Python folks, suppose that ncdoc is a netconf document.  Then
here's the quick version of how to build an appropriate map of
specific namespaces from a given JUNOS response:

namepsaces = set()
nsmap = dict()

for i in ncdoc.getiterator():
        namespaces.update(set(i.nsmap.values()))
for ns in namespaces:
        shortname = ns.split('/')[-1]
        nsmap[shortname]

# Now you can use normal XML tree traversal techniques
e = nsdoc.getroot()
name = e.xpath("./junos-interface:name/text()", namespaces=nsmap)
operstat = e.xpath("./junos-interface:oper-status/text()", namespaces=nsmap)
...etc...

This should works for any JUNOS platform that uses the consistent
namespace abbreviations we're all used to reading.  Hopefully that's
all of them, forever :)

Ross

-- 
Ross Vandegrift
r...@kallisti.us

"If the fight gets hot, the songs get hotter.  If the going gets tough,
the songs get tougher."
        --Woody Guthrie

Attachment: signature.asc
Description: Digital signature

_______________________________________________
juniper-nsp mailing list juniper-nsp@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

Reply via email to