| 1 | package oEdtk::XML::XSD; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | our $VERSION = '2.1131'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # Validate a file against an XSD schema. XML::LibXML::Schema::validate returns a | ||||||||||||||||||||
| 10 | # false value when the document is valid and dies with the validation errors when | ||||||||||||||||||||
| 11 | # it is not, so the check keys on the exception. That is stable across XML::LibXML | ||||||||||||||||||||
| 12 | # releases; the fork's "not validate(...)" keyed on the return value instead and | ||||||||||||||||||||
| 13 | # was only accidentally correct. | ||||||||||||||||||||
| 14 | 0 | ▶ | sub parse_and_validate_xml_file_xsd { | ||||||||||||||||||
|
Subroutine
parse_and_validate_xml_file_xsd: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 15 | 0 | my ( $file, $cfg ) = @_; | |||||||||||||||||||
| 16 | 0 | my $xml_doc = XML::LibXML->load_xml( location => $file ); | |||||||||||||||||||
| 17 | 0 | my $xsd_doc = XML::LibXML::Schema->new( location => $cfg ); | |||||||||||||||||||
| 18 | 0 | my $ok = eval { $xsd_doc->validate($xml_doc); 1 }; | |||||||||||||||||||
| 19 | 0 | ▶ | croak "XML ($file) does not validate with XSD ($cfg): " . ( $@ // q{} ) | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$@ // ""
MC/DC0%0/1
$@ // ""
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||
| 20 | unless $ok; | ||||||||||||||||||||
| 21 | 0 | return 1; | |||||||||||||||||||
| 22 | } | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 1 | 1; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | =head1 NAME | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | oEdtk::XML::XSD - validate an XML file against an XSD schema | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 SYNOPSIS | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | use oEdtk::XML::XSD; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | oEdtk::XML::XSD::parse_and_validate_xml_file_xsd($xml_file, $xsd_file); | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Thin wrapper over L<XML::LibXML::Schema>. It dies when the document does not | ||||||||||||||||||||
| 39 | validate. Moved into the mainline from the 0.8032 fork at S21 of the fork-merge | ||||||||||||||||||||
| 40 | plan. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =head1 FUNCTIONS | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head2 parse_and_validate_xml_file_xsd | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | parse_and_validate_xml_file_xsd($xml_file, $xsd_schema); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | Load C<$xml_file>, validate it against the XSD schema C<$xsd_schema> and return | ||||||||||||||||||||
| 49 | true. The function croaks with the validation errors, prefixed by the two file | ||||||||||||||||||||
| 50 | names, when the document is invalid. Note that C<XML::LibXML::Schema::validate> | ||||||||||||||||||||
| 51 | returns a false value on success and dies on failure, which is what the check | ||||||||||||||||||||
| 52 | relies on; see F<t/int/35_xml_dtd_xsd.t>. | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =head1 AUTHOR | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | The oEdtk team. | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | =cut | ||||||||||||||||||||