1
votes

I'm looking to use a Perl module to leverage some publicly available xsd files as a template to perform credit check lookups.

XSD files are here: http://www.equifax.ca/XMLSchemas/UAT/

Documentation snippet:

The Equifax Namespace for the “Input Request for Consumer and Commercial” is:

The Equifax “Input Request for Consumer and Commercial” Namespace Schema Location is:

  • For the Production environment.

http://www.equifax.ca/XMLSchemas/Production/CNCustTransmitToEfx.xsd

  • For the User Acceptance Test environment also known as SS environment at Equifax.

http://www.equifax.ca/XMLSchemas/UAT/CNCustTransmitToEfx.xsd

Documentation for XML::Compile::Schema: http://search.cpan.org/~markov/XML-Compile-1.59/lib/XML/Compile/Schema.pod

The problem I am having is that I am unable to ingest the xsd files properly and use $schema->template('PERL', $type) to display the xsd template.

First attempt:

(following http://blogs.perl.org/users/brian_e_lozier/2011/10/using-xmlcompile-to-output-xsd-compliant-xml.html)

Example code from link above:

#!/usr/local/bin/perl

use warnings;
use strict;

use Data::Dumper;
use XML::Compile::Schema;
use XML::LibXML::Reader;

my $xsd = 'test.xsd';

my $schema = XML::Compile::Schema->new($xsd);

# This will print a very basic description of what the schema describes
$schema->printIndex();

# this will print a hash template that will show you how to construct a 
# hash that will be used to construct a valid XML file.
#
# Note: the second argument must match the root-level element of the XML 
# document.  I'm not quite sure why it's required here.
warn $schema->template('PERL', 'addresses');

My attempt:

use Data::Dumper;
use XML::Compile::Schema;
use XML::LibXML::Reader;

my $xsd = '/tmp/CNCustTransmitToEfx.xsd';

my $schema = XML::Compile::Schema->new($xsd);

warn $schema->template('PERL', 'CNCustTransmitToEfx');

Returns: error: cannot find element or attribute `CNCustTransmitToEfx'

$schema->printIndex(); returns something that looks good though: https://pastebin.com/rkED54eV

Second attempt:

Fixing previous error from: XML::Compile does not find the root element I give it

(Using pack_type to change $type to {[namespace]}CNCustTransmitToEfx)

use XML::Compile::Schema;
use XML::Compile::Util qw/pack_type/;

my $schema = XML::Compile::Schema->new("/tmp/CNCustTransmitToEfx.xsd");
my $type = pack_type 'http://www.equifax.ca/XMLSchemas/CustToEfx', 'CNCustTransmitToEfx';
warn $schema->template('PERL' => $type);

Returns: "error: cannot find type {http://www.equifax.ca/XMLSchemas/CustToEfx}CNConsAndCommRequestType at {http://www.equifax.ca/XMLSchemas/CustToEfx}CNCustTransmitToEfx"

I cannot find any targetNamespace in the 'CNConsAndCommRequestType' xsd file, not sure if this is the problem or not: http://www.equifax.ca/XMLSchemas/UAT/CNConsAndCommRequestType.xsd

Third attempt: Adding all XSD files locally first

my $xsds = ['/tmp/CNConsAndCommRequestType.xsd','/tmp/CNCustTransmitToEfx.xsd','/tmp/CNEfxCommon.xsd','/tmp/CNConsAndCommRequestSegTypes.xsd','/tmp/BDC-CNEfxReportType.xsd', '/tmp/BDC-CNEfxTransmitToCust.xsd', '/tmp/CNCommCreditSegTypesReport2.0.xsd', '/tmp/CNCommCreditSegTypes.xsd', '/tmp/CNCommCreditTypeReport2.0.xsd','/tmp/CNCommCreditType.xsd', '/tmp/CNConsCreditSegTypes.xsd', '/tmp/CNConsCreditType.xsd', '/tmp/CNEfxReportType.xsd', '/tmp/CNEfxTransmitToCust.xsd'];
my $schema = XML::Compile::Cache->new($xsds);

my $type = pack_type 'http://www.equifax.ca/XMLSchemas/CustToEfx', 'CNCustTransmitToEfx';
warn $schema->template('PERL' => $type);

Returns same error: "error: cannot find type {http://www.equifax.ca/XMLSchemas/CustToEfx}CNConsAndCommRequestType at {http://www.equifax.ca/XMLSchemas/CustToEfx}CNCustTransmitToEfx"

I'm having a really hard time figuring out if this is something wrong with the schema, or something that I'm doing wrong.

Any input is appreciated.

1

1 Answers

1
votes

I'm not an expert on XML::Compile, so I can't tell you if the following is the best solution. I did make the same observation that CNConsAndCommRequestType.xsd does not have a targetNamespace set, but XML::Compile::Schema allows us to override that. This appears to work for me (I downloaded the xsds locally):

use warnings;
use strict;
use XML::Compile::Schema;
use XML::Compile::Util qw/pack_type/;

my $schema = XML::Compile::Schema->new( schema_dirs=>[ '/path/to/xsds' ] );
$schema->importDefinitions($_) for qw/ CNCustTransmitToEfx.xsd
    CNConsAndCommRequestSegTypes.xsd CNEfxCommon.xsd /;
$schema->importDefinitions('CNConsAndCommRequestType.xsd',
    target_namespace => 'http://www.equifax.ca/XMLSchemas/CustToEfx');
my $type = pack_type('http://www.equifax.ca/XMLSchemas/CustToEfx',
    'CNCustTransmitToEfx');
print $schema->template(PERL => $type);