File: xml-inspector

package info (click to toggle)
synopsis 0.8.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 10,112 kB
  • ctags: 12,996
  • sloc: cpp: 34,254; ansic: 33,620; python: 10,975; sh: 7,261; xml: 6,369; makefile: 773; asm: 445
file content (40 lines) | stat: -rwxr-xr-x 1,021 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
#
# Copyright (C) 2004 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

"""This simple tool can be used to introspect an AST as generated from the Dump.Formatter.
It requires the python binding for Daniel Veillard's libxml2 package (@see http://xmlsoft.org)"""

import sys
try:
    import libxml2
except ImportError:
    sys.stderr.write('this tool requires the libxml2 module\n')
    sys.exit(-1)

if len(sys.argv) != 3:

    print 'Usage : %s <xml> <xpath>'%sys.argv[0]
    sys.exit(-1)

doc = libxml2.parseFile(sys.argv[1])
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval(sys.argv[2])

if type(res) == float:
    print res

elif type(res) == list: #node set
    if len(res) == 0: print '1 match'
    elif len(res) == 1: print '1 match :'
    else: print '%d matches :'%len(res)

    for r in res:
        print '%s (%s)'%(r.content, r.nodePath())

else:
    print 'unsupported return type', type(res)