XSD 文件的 XML::Compile::Schema 错误

标签 xml perl xsd

我希望使用 Perl 模块来利用一些公开可用的 xsd 文件作为模板来执行信用检查查找。

XSD 文件位于:http://www.equifax.ca/XMLSchemas/UAT/

文档片段:

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

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

我遇到的问题是我无法正确提取 xsd 文件并使用 $schema->template('PERL', $type) 来显示 xsd 模板。

第一次尝试:

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

上面链接中的示例代码:

#!/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');

我的尝试:

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');

返回:错误:找不到元素或属性“CNCustTransmitToEfx”

$schema->printIndex();返回一些看起来不错的东西:https://pastebin.com/rkED54eV

第二次尝试:

修复之前的错误:XML::Compile does not find the root element I give it

(使用 pack_type 将 $type 更改为 {[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);

返回:“错误:在 { http://www.equifax.ca/XMLSchemas/CustToEfx }CNCustTransmitToEfx 处找不到类型 { http://www.equifax.ca/XMLSchemas/CustToEfx }CNConsAndCommRequestType”

我在“CNConsAndCommRequestType”xsd 文件中找不到任何 targetNamespace,不确定这是否是问题所在:http://www.equifax.ca/XMLSchemas/UAT/CNConsAndCommRequestType.xsd

第三次尝试:首先在本地添加所有 XSD 文件

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);

返回相同的错误:“错误:在 { http://www.equifax.ca/XMLSchemas/CustToEfx }CNCustTransmitToEfx 处找不到类型 { http://www.equifax.ca/XMLSchemas/CustToEfx }CNConsAndCommRequestType”

我很难弄清楚这是否是架构有问题,或者是我做错了什么。

欢迎任何意见。

最佳答案

我不是XML::Compile方面的专家,所以我无法告诉您以下是否是最佳解决方案。我确实做了同样的观察,CNConsAndCommRequestType.xsd 没有设置 targetNamespace,但是 XML::Compile::Schema允许我们覆盖它。这似乎对我有用(我在本地下载了 xsd):

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);

关于XSD 文件的 XML::Compile::Schema 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49287276/

相关文章:

javascript - 在表单验证中,如果字段中没有单词,它会返回错误但仍会提交 cgi 文件?

java - 在 Saxon Java 外部函数中深度复制 XML 节点

python - Python 2.6.2 中的 ElementTree 处理指令支持吗?

android - 在 Seekbar Android 中使用 rotation = 270 时无法正确指定高度和宽度

java - 十六进制字节顺序

java - 如何在 Netbeans 8.2 中使用 XSD 1.1?

c# - 如何在 xsd 中使用枚举

java - 如何将 Java 的 XPath 库与 XML 命名空间结合使用?

arrays - 如何在 Perl 中按列对数组或表进行排序?

perl - mojolicious 引用了一个并不总是定义的 stash 变量