The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

Name

Data::Edit::Xml::Lint - Lint an xml file using xmllint

Synopsis

Create a sample file with an error, lint it and retrieve the number of errors:

  my $x = Data::Edit::Xml::Lint::new();                                         # New xml file linter
     $x->file = 'zzz.xml';                                                      # Target file

  my $c = filePathExt(qw(/home phil hp dtd Dtd_2016_07_12 catalog-hpe xml));    # Possible catalog

  $x->catalog = $c if -e $c;                                                    # Use catalog if possible

  $x->source = <<END;                                                           # Sample source
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE concept PUBLIC "-//HPE//DTD HPE DITA Concept//EN" "concept.dtd" []>
<concept>
 <title>Title</title>
 <conbody>
   <p>Body</p>
 </conbody>
</concept>
END
  $x->lint(aaa=>1, bbb=>2);                                                     # Write the source to the target file, lint using xmllint, include some attributes to be included as comments at the end of the target file

  my $X = Data::Edit::Xml::Lint::read($x->file);                                # Reload the written file
  ok $X->{aaa}  == 1;                                                           # Check the reloaded attributes
  ok $X->{bbb}  == 2;
  ok $X->errors == 1;
  is $X->$_, $x->$_ for qw(catalog file source);                                # Check linter fields

Produces:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE concept PUBLIC "-//HPE//DTD HPE DITA Concept//EN" "concept.dtd" []>
 <concept>
  <title>Title</title>
  <conbody>
    <p>Body</p>
  </conbody>
 </concept>

 <!--linted: 2017-Jul-13 -->

 <!--aaa: 1 -->
 <!--bbb: 2 -->
 <!--catalog: /home/phil/hp/dtd/Dtd_2016_07_12/catalog-hpe.xml -->
 <!--file: zzz.xml -->
 <!--linted: 2017-Jul-13 -->
 <!-- -:8: element concept: validity error : Element concept does not carry attribute id -->
 <!-- </concept> -->
 <!--           ^ -->

 <!--errors: 1 -->

Description

Methods

Methods in this package

new

Create a new xml linter - call this method statically as in Data::Edit::Xml::Lint::new()

file

File that the xml will be written to and read from

catalog

Optional catalog file containing the locations of the DTDs used to validate the xml

dtds

Optional directory containing the DTDs used to validate the xml

errors

Number of lint errors detected by xmllint

linted

Date the lint was performed

source

String containing the xml to be written or the xml read

lint

Store some xml in a file and apply xmllint

     Parameter    Description
  1  $lint        Linter
  2  %attributes  Attributes to be recorded as xml comments

read

Reload a linted xml file and extract attributes

     Parameter  Description
  1  $file      File containing xml

Index

catalog

dtds

errors

file

lint

linted

new

read

source

Installation

This module is written in 100% Pure Perl and is thus easy to read, use, modify and install.

Standard Module::Build process for building and installing modules:

  perl Build.PL
  ./Build
  ./Build test
  ./Build install

Author

philiprbrenan@gmail.com

http://www.appaapps.com

Copyright

Copyright (c) 2016-2017 Philip R Brenan.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.