[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Execute an XPath and get the line numbers

  • From: "Oleg A. Paraschenko" <olpa@x...>
  • To: xml-dev <xml-dev@l...>
  • Date: Tue, 6 Mar 2007 08:54:02 +0300

Execute an XPath and get the line numbers
Hello,

I'm investigating the structure of an XML file. The best tool is an usual
text editor (vim), but I need to look at some specific tags in some
specific contexts.

The idea is to execute an XPath and somehow to get to the location in the
editor. Remembering that I saw something interesting while playing with
libxml internals, I re-checked... And bingo! I've written a script which
executes an XPath and prints the result together with the line number in
the source XML file.

Usage:

python xpath.py document xpath

Code:

<xpath.py>
import sys,libxml2

doc_name = sys.argv[1]
xpath    = sys.argv[2]

doc  = libxml2.parseFile(doc_name)
ctxt = doc.xpathNewContext()
res  = ctxt.xpathEval(xpath)
for node in res:
  print node.lineNo()
  print node

doc.freeDoc()
ctxt.xpathFreeContext()
del ctxt
del doc
</xpath.py>

I think many will find this code useful. If you want bookmark it, here is
the permanent address:

http://uucode.com/blog/2007/03/05/execute-an-xpath-and-get-the-line-numbers/


-- 
Oleg Parashchenko  olpa@ http://xmlhack.ru/  XML news in Russian
http://uucode.com/blog/  Generative Programming, XML, TeX, Scheme
http://olpa.livejournal.com/


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.