|
Posted by MM on June 26, 2007, 11:32 pm
Please log in for more thread options
>
>
>
>
>
>
> > > 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: <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
> 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!
|