简体   繁体   English

perl将xsd转换为xml

[英]perl convert xsd to xml

Working on this for hours now, and even with this tutorial, posted by saberworks in another similar question, I don't get it to work ... 现在这方面的工作了几个小时,甚至与教程中,通过saberworks在另一个类似的问题贴出来,我没有得到它的工作...

So, I am trying to convert .xsd files into xml structures that can be accessed and completed afterwards through libXML or any other xml-parser . 因此,我正在尝试将.xsd文件转换为xml structures ,然后可以通过libXML或任何其他xml-parser访问并完成该xml structures

If you take a look into the linked tutorial, is it possible to easily automate the second step, where saberworks creates the final data structure by hand? 如果您查看链接的教程,是否可以轻松地自动进行第二步,即saberworks手动创建最终数据结构?

How I understood, he just uses the output to get a "clue", some kind of guide, how the xml should look like. 据我了解,他只是使用输出来获取“线索”(某种指导),以了解xml的外观。 But is it possible to generate a whole perl structure that can be used (parsed) by libXML ? 但是是否可以生成可以由libXML使用(解析)的整个perl结构?

And in case we already have the xml structure, how does XML::Compile/XML::LibXML handle with optional elements? 如果我们已经有了xml结构,那么XML::Compile/XML::LibXML处理带有可选元素的? If I don't put any values or elements in an optional element, does it appear in the final xml output? 如果我没有在可选元素中放置任何值或元素,它是否出现在最终的xml输出中? In case it does, is there any way to prevent this? 如果有的话,有什么办法可以防止这种情况发生?

use warnings;  
use strict;
use Data::Dumper;
use XML::Compile::Schema;  
use XML::LibXML;

my $xsd = 'schema.xsd';
my $schema = XML::Compile::Schema->new($xsd);
my $xml_temp = $schema -> template('PERL', 'addresses');

# ***convert $xml_temp to "real" perl hash structure***

my $doc    = XML::LibXML::Document->new('1.0', 'UTF-8'); 
my $write  = $schema -> compile(WRITER => 'addresses'); 
my $xml    = $write -> ($doc, $xml_temp);
$doc -> setDocumentElement($xml);
print $doc -> toString(1);

Usually you want to generated dynamically the data that will be passed to the XML writer. 通常,您要动态生成将传递给XML编写器的数据。 For helping you with generating the correct data structure the template() method gives you a string representation of such a data structure along with some useful comments. 为了帮助您生成正确的数据结构,template()方法为您提供了这种数据结构的字符串表示形式以及一些有用的注释。 If you want to see a minimal sample you can turn the string into an actual Perl datastructure with eval. 如果要查看最少的示例,可以使用eval将字符串转换为实际的Perl数据结构。

For simplicity I have included the sample schema from the tutorial in the adapted sample script. 为简单起见,我将本教程中的示例架构包含在改编后的示例脚本中。

Regarding your second question I would assume that optional elements are not put into the XML when you don't have them in your input. 关于第二个问题,我假设当您的输入中没有可选元素时,它们不会放入XML中。

use strict;
use warnings;

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

my $schema = XML::Compile::Schema->new(q{<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  <xs:element name="addresses">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="address" minOccurs='1' maxOccurs='unbounded'/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="address">
    <xs:complexType>
      <xs:sequence>
         <xs:element ref="name" minOccurs='0' maxOccurs='1'/>
         <xs:element ref="street" minOccurs='0' maxOccurs='1'/>
       </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="name" type='xs:string'/>
  <xs:element name="street" type='xs:string'/>
</xs:schema>
} );

# this returns a string representation of a sample Perl datastructure
my $template = $schema->template('PERL', 'addresses');
print "The template:\n $template\n\n";

# turn the string representation into an actual Perl data structure
my $sample;
eval "\$sample = $template;";
print "The template turned into a Perl data structure:\n";
print Dumper( $sample );
print "\n\n";

# create XML from the sample Perl data structure
my $doc    = XML::LibXML::Document->new('1.0', 'UTF-8'); 
my $write  = $schema->compile(WRITER => 'addresses'); 
my $xml    = $write->($doc, $sample);
$doc->setDocumentElement($xml);
print "The generated XML:\n";
print $doc->toString(1);
print "\n\n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM