XPath to Line Number?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

In one sentence: Given an XML file and an XPath pointing to a certain
node, how can I find the line-number of that node in the original XML

I am building a script that reads an XML file, checks it against a
bunch of validation rules, and outputs any errors and/or warnings.
Currently, I read the XML file via XML::XPath->new(filename =>
$filename), but I am open to alternative suggestions.  When I do find
an error (for example, a certain attribute may have an invalid keyword
for its value), I want to print the line number of the original XML
file in which this error occurred.

Searching on the web, I found several references to XML::Twig.  I was
wondering if anyone could offer suggestions or advice about XML::Twig
or any other modules that could help.

Thanks in advance!

Re: XPath to Line Number?

Quoted text here. Click to load it

It sounds like you want to validate the XML file before even using
XPATH.  I am having trouble with finding a good DTD or Schema
validation package (as seen in the thread prior to this one), although
several are available and may meet your requirements.  Anyway, you
will need to create a DTD or Schema file to house valid/default
values. Search CPAN for DTD and "XML Schema" to find packages that
will validate XML files.

I prefer using SAX and coding my own data structure, so I am not much
help when it comes to XML::Twig. However, I just quickly read the POD
for XML::Twig and I do not think it actually processes the DTD; I
think it just makes DTD information available in the tree. If
XML::Twig does come with a validation process I will hop on the
bandwagon and try to use it too!!! But right now I am busy playing
with XML::DTD. Anyway, others can comment on this....

I hope I am on the right track here, but I am not 100% sure of what
you are doing.

Re: XPath to Line Number?

Quoted text here. Click to load it

Thanks for your input.  Actually, there is already a huge DTD, and the
first thing that happens is indeed an XML validation using that DTD.
However, there are many things to be validated afterward that can only
be done manually through Perl.  When errors are found, I need to
output the line number.

Here is an example.  Let's say this is my XML document:

1: <root>
2:   <parent>
3:     <child type="Comparison">Some text.</child>
4:     <child type="Deletion">Other text.</child>
5:   </parent>
6: </root>

I want some function, say 'getLineNumberOf(XPath)' such that when I

$line = getLineNumberOf("/root/parent/child-b[@type='Deletion']");

...then $line will become 4.

Subroutine, module, give me anything that helps!  Thanks.

Re: XPath to Line Number?

Quoted text here. Click to load it

I understand now and thanks for the DTD information - I might have to
look at XPATH and TWIG for my problem.
I do not know if this will help you, but I am going to say it anyway:
Once the XML file is read into a tree, I do not think line numbers of
the orignal file are saved anywhere in the tree. You may want to dump
the tree variables to take a look.

Good luck!

Re: XPath to Line Number?

Andrew wrote:
Quoted text here. Click to load it

Keeping the line number around is actually a FAQ:

And no, XML::Twig does not validate the XML (it's ultimately based on
expat which is a non-validating parser).

XML::LibXML has also that info available (even more conveniently: the
line_number method in XML::LibXML::Node).

I don't know those modules very well, but the XML::Schematron::* modules
might also be usefull, they are designed for that kind of task I think:


Re: XPath to Line Number?

Quoted text here. Click to load it

Thanks, everyone!  Currently, I'm busy with another part of the code,
but I will post back here after trying these suggestions.

Site Timeline