Re: XPath to Line Number?



On Jun 26, 1:39 pm, Andrew <acheon...@xxxxxxxxx> 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.

.



Relevant Pages

  • RE: Quick question about XML manipulation
    ... generates an XML file. ... Do I understand this correctly - If I include a reference to an XSD or DTD ... specifying a schema will allow you to validate against it, but the validation ... // Invalid root element. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: InvalidOperationException: ReadElementContentAsString method is not supported on node type None
    ... I want to import a user uploaded XML file into a database. ... XmlReaderSettings readerSettings = new XmlReaderSettings; ... now I have an XmlReader object with a schema so I check for validation. ...
    (microsoft.public.dotnet.xml)
  • Re: default namespace / Xmlvalidatingreader
    ... You dont get an exception in the case of the second file, ... validation warning should be thrown. ... XmlReader readerToUse = validatingReader; ...
    (microsoft.public.dotnet.xml)
  • Re: XmlValidatingReader / Default Namespace
    ... You dont get an exception in the case of the second file, ... validation warning should be thrown. ... XmlReader readerToUse = validatingReader; ...
    (microsoft.public.dotnet.xml)
  • Problem with partial validation
    ... I have a problem when I select node elements from an xml file and validata ... I have defined a schema for details of a particular service request. ... Is the definition of namespace in the selected node causing the validation ...
    (microsoft.public.dotnet.xml)