【问题标题】:Create hash from XSD without an XML Example Using XML::Compile使用 XML::Compile 从没有 XML 示例的 XSD 创建哈希
【发布时间】:2014-07-25 21:25:24
【问题描述】:

我想 1) 根据我的 XSD 定义文件创建一个散列 结构,2) 初始化该散列的元素,然后 3) 写出一个 XML 文件。这是我所在的位置:

我设法做的是创建一个哈希,但我是通过读取示例 XML 文件来做到这一点的——我这样做是因为我正在遵循我找到的代码示例,但我认为这些示例试图完成我需要做的事情以外的事情。我只需要知道哈希的样子我可以设置其元素的值,我不想或不需要知道某个特定的哈希初始化对于一些任意 XML 文件的样子。

这是我到目前为止所做的:

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;
use XML::LibXML;
use XML::Compile::Schema;
use File::Spec;
use File::Copy;
use Getopt::Long;
use String::Util;

my $xsd          = 'My_XMLSchema.xsd';
my $xml_in       = 'example.xml';

my $schema       = XML::Compile::Schema->new($xsd);
my $reader       = $schema->compile(READER => '{http://tempuri.org/XMLSchema.xsd}RootElement');

my $hash         = $reader->($xml_in);
my $doc         = XML::LibXML::Document->new('1.0', 'UTF-8');
my $write       = $schema->compile(WRITER => '{http://tempuri.org/XMLSchema.xsd}RootElement');
my $xml_out     = $write->($doc, $hash);

如果我这样做

打印 Dumper $hash

如果我想创建我提供的 XML 文件,输出是我要编写的代码——这不是我需要的。我只想生成我的 $hash,然后我可以在其中粘贴数据值并创建一个 XML。似乎我应该只需要和 XSD 文件来执行此操作。这有意义吗?

我不明白 $reader 实际上是什么

【问题讨论】:

    标签: xml perl xsd


    【解决方案1】:

    您需要创建一个与示例数据具有相同结构的数据结构。 示例 XML 文件可以作为数据结构外观的灵感来源,但在最终程序中不需要。

    这是一个基于sample schema from w3schools的示例。

    use warnings;
    use strict;
    use Data::Dumper;
    use XML::LibXML;
    use XML::Compile::Schema;
    
    my $xsd          = 'shiporder.xsd';
    my $xml_in       = 'shiporder.xml';
    
    my $schema       = XML::Compile::Schema->new($xsd);
    
    # This can be removed for production
    # as the output serves only as an inspiration for $real_hash
    {
        my $reader       = $schema->compile(READER => 'shiporder');
        my $sample_hash = $reader->($xml_in);
    
        print Dumper( $sample_hash );
    }
    
    # a hashref just like $sample_hash, but with my data
    my $real_hash = {
              'orderid' => '42',
              'shipto' => {
                          'address' => 'Lindenstrasse 23',
                          'city' => 'Munich',
                          'country' => 'Germany',
                          'name' => 'Max Mustermann',
                        },
              'item' => [
                        {
                          'quantity' => '1',
                          'price' => bless( {
                                            '_es' => '-',
                                            '_e' => [
                                                      1
                                                    ],
                                            '_m' => [
                                                      109
                                                    ],
                                            'sign' => '+'
                                          }, 'Math::BigFloat' ),
                          'title' => 'dog feed',
                          'note' => 'delicious'
                        },
                        {
                          'price' => bless( {
                                            '_m' => [
                                                      99
                                                    ],
                                            '_e' => [
                                                      1
                                                    ],
                                            '_es' => '-',
                                            'sign' => '+'
                                          }, 'Math::BigFloat' ),
                          'title' => 'Enjoy',
                          'quantity' => '1'
                        }
                      ],
              'orderperson' => 'Martina Musterfrau'
            };
    
    my $doc         = XML::LibXML::Document->new('1.0', 'UTF-8');
    my $write       = $schema->compile(WRITER => 'shiporder');
    my $xml_out     = $write->($doc, $real_hash);
    print "\n";
    print $xml_out;
    

    【讨论】:

    • 谢谢。哈希结构已经创建——这是使用 XML::Compile 为我完成的。我用来创建散列的示例似乎同时使用了 XSD 模式定义文件和示例 XML 文件。我试图了解是否真的需要示例文件,如果需要,为什么。当 XSD 已经提供时,我真的需要使用 XML 对我来说没有意义。
    • 我已经编辑了我的答案,澄清可以删除示例文件的读取。
    猜你喜欢
    • 2014-02-27
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2012-10-05
    相关资源
    最近更新 更多