XPath to Line Number?
am 26.06.2007 20:39:43 von Andrew
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.
Thanks in advance!
Re: XPath to Line Number?
am 27.06.2007 01:20:42 von 1234marlon
On Jun 26, 1:39 pm, 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.
>
> Thanks in advance!
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?
am 27.06.2007 05:03:10 von Andrew
On Jun 26, 7:20 pm, MM <1234mar...@gmail.com> wrote:
> On Jun 26, 1:39 pm, 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.
>
> > Thanks in advance!
>
> 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.
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:
2:
3: Some text.
4: Other text.
5:
6:
I want some function, say 'getLineNumberOf(XPath)' such that when I
call...
$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?
am 27.06.2007 05:32:56 von 1234marlon
On Jun 26, 10:03 pm, Andrew wrote:
> On Jun 26, 7:20 pm, MM <1234mar...@gmail.com> wrote:
>
>
>
>
>
> > On Jun 26, 1:39 pm, 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.
>
> > > Thanks in advance!
>
> > 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.
>
> 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:
> 2:
> 3: Some text.
> 4: Other text.
> 5:
> 6:
>
> I want some function, say 'getLineNumberOf(XPath)' such that when I
> call...
>
> $line = getLineNumberOf("/root/parent/child-b[@type='Deletion']");
>
> ...then $line will become 4.
>
> Subroutine, module, give me anything that helps! Thanks.- Hide quoted text -
>
> - Show quoted text -
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?
am 27.06.2007 09:48:53 von mirod
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
Re: XPath to Line Number?
am 28.06.2007 01:20:34 von Andrew
On Jun 27, 3:48 am, mirod wrote:
> 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
Thanks, everyone! Currently, I'm busy with another part of the code,
but I will post back here after trying these suggestions.