473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to get line numbers when evaluating XPath-expressions?

Hello everyone!

I evaluate XPath expressions using XPathNavigator and need to know for
each result, which line number (or et least file position) it has
inside the XML file.

Any ideas?

Thanks in advice!
Nov 21 '07 #1
2 5903
Yes, it is possible but quite involved.

Your task is a lot easier if you know that each element, comment &
processing instruction starts on a new line and that there are no empty
lines. If this isn't the case you may want to create your own version of the
XML file first, preserving whitespace is an issue here though.

XmlReader can be used but it does not return its location within the file,
so you will need to keep track of this yourself, or you may want to use
TextReader with regular expressions.

The key thing you need is the ordinal position of the element immediately
preceding the returned node from the expression (this helps cover the case
when a node returned is a mixed-content text node) using an xpath expression.

SketchPath is an example XPath application that uses this technique on its
own representation of the XML file.

The following rough code snippet (from SketchPath) might give your some
ideas, this is incomplete, only caters for element nodes and doesn't take
account of comments and processing-instructions.
XPathExpression expr = xnav.Compile(contextXpath);
expr.SetContext(namespaceData);
if (expr.ReturnType == XPathResultType.NodeSet)
{
XPathNodeIterator iterator =
(XPathNodeIterator)xnav.Evaluate(expr);
iterator.MoveNext();
if (iterator.Current.NodeType == XPathNodeType.Element)
{
xnav = (XPathNavigator)iterator.Current.Clone(); //
preserve location of context element in navigator

// find number of preceding elements
double elCount =
(double)xnav.Evaluate("count(./preceding::*) + count(./ancestor::*)");
....

Phil Fearon
http://www.sketchpath.com

"li********@yandex.ru" wrote:
Hello!

Isn't it possible to "locate" an XPathNavigator (XPathNode?) after it
was returned, using XmlReader or something else?

Nov 22 '07 #2
Thanks a lot!
I'll try it out


On Nov 22, 7:40 am, Phil Fearon <PhilFea...@discussions.microsoft.com>
wrote:
Yes, it is possible but quite involved.

Your task is a lot easier if you know that each element, comment &
processing instruction starts on a new line and that there are no empty
lines. If this isn't the case you may want to create your own version of the
XML file first, preserving whitespace is an issue here though.

XmlReader can be used but it does not return its location within the file,
so you will need to keep track of this yourself, or you may want to use
TextReader with regular expressions.

The key thing you need is the ordinal position of the element immediately
preceding the returned node from the expression (this helps cover the case
when a node returned is a mixed-content text node) using an xpath expression.

SketchPath is an example XPath application that uses this technique on its
own representation of the XML file.

The following rough code snippet (from SketchPath) might give your some
ideas, this is incomplete, only caters for element nodes and doesn't take
account of comments and processing-instructions.

XPathExpression expr = xnav.Compile(contextXpath);
expr.SetContext(namespaceData);
if (expr.ReturnType == XPathResultType.NodeSet)
{
XPathNodeIterator iterator =
(XPathNodeIterator)xnav.Evaluate(expr);
iterator.MoveNext();
if (iterator.Current.NodeType == XPathNodeType.Element)
{
xnav = (XPathNavigator)iterator.Current.Clone(); //
preserve location of context element in navigator

// find number of preceding elements
double elCount =
(double)xnav.Evaluate("count(./preceding::*) + count(./ancestor::*)");
....

Phil Fearonhttp://www.sketchpath.com

"linuxad...@yandex.ru" wrote:
Hello!
Isn't it possible to "locate" an XPathNavigator (XPathNode?) after it
was returned, using XmlReader or something else?
Nov 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
I'm developing an application for medical use that will be used to capture patient background and visit data. The application will have approximately 50 forms, with an average of about 20 fields...
2
by: John Wilkinson | last post by:
Hi, I am creating a table of values. One column is the addition of the other 2 columns.Both columns are floating point, and may be any value 0-100 For some values we find that we get a good output...
4
by: Tomas Rivas | last post by:
I am trying to validate an xml file and schema and when I am trying to validate I am getting the following error. I have been trying to come out with a solution but I have failed so far. The...
4
by: David Thielen | last post by:
I have the following xml: <root> <name>abc "123" xyz</name> </root> When I get an XPathNavigator to root and then call navigator.Evaluate("name = \"abc \\\"123\\\" xyz"), I get an...
8
by: Arun Bhalla | last post by:
Hi, I'm developing an Explorer bar using VS.NET 2003 (C#) on Windows XP. For some time, I've noticed that I don't have filenames and line numbers appearing in my exceptions' stack traces. On...
3
by: Jim Bancroft | last post by:
Hi all, In VB6 I used a 3rd party tool for line numbering my source code, to help with debugging. However, in experimenting with VB .Net I've noticed that my exceptions automatically provide...
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
3
by: Rahul | last post by:
Hi, Is there a way to get the line numbers to display in the exception when you have published your web site? Thanks Rahul
35
by: aarklon | last post by:
Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) )...
0
by: coco09 | last post by:
Hi all I have the following line of text in my script $var = do {local $/; <$FILE>};#<-- slurp whole file in scalar The problem i am having is that the input file is a pcl file and has new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.