Re: XPath to Line Number?



Andrew wrote:
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
file?

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.

Keeping the line number around is actually a FAQ:
http://xmltwig.com/xmltwig/XML-Twig-FAQ.html#Q18

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:
http://search.cpan.org/dist/XML-Schematron/

--
mirod
.