2014-01-30 1 views
2

지금 시간이에서 작업, 심지어 다른 비슷한 질문에 saberworks에 의해 게시 this 튜토리얼과 함께, 나는 그것이 작동하지 않는 XML로 변환 .xsd 파일을 xml structures으로 변환하십시오. 이후에 액세스하여 완료 할 수 있으며 libXML 또는 기타 xml-parser을 통해 완료 할 수 있습니다.펄 그래서, 내가하려고</p> <p>... XSD가

링크 된 자습서를 살펴보면 saberworks가 손으로 최종 데이터 구조를 만드는 두 번째 단계를 쉽게 자동화 할 수 있습니까?

내가 어떻게 이해했는지, 그는 단지 "실마리", 어떤 종류의 가이드, XML이 어떻게 보일 것인가에 대한 결과물을 사용한다. 하지만 libXML에 의해 (구문 분석 된) 전체 perl 구조체를 생성 할 수 있습니까?

이미 xml 구조가있는 경우 XML::Compile/XML::LibXML은 선택적 요소를 어떻게 처리합니까? 선택적 요소에 값이나 요소를 넣지 않으면 최종 xml 출력에 나타 납니까? 경우에이를 막을 수있는 방법이 있습니까?

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

테스트 해 보셨습니까? – simbabque

+0

확실하지만 perl 템플릿을 libXML에서 사용할 수있는 perl 데이터 구조로 변환하는 방법을 찾지 못했습니다. – Jonas93

+0

질문을 편집하고 지금까지 시도한 것을 보여주십시오. 그렇게 작업 할 수 있습니다. – simbabque

답변

0

는 일반적으로 동적으로 XML 작가에 을 통과 할 데이터를 생성하고자합니다. 올바른 데이터 구조를 생성하는 것을 돕기 위해 template() 메소드는 유용한 주석과 함께 이러한 데이터 구조의 문자열 표현을 제공합니다. 최소 샘플을보고 싶다면 문자열을 eval을 사용하여 실제 Perl 데이터 구조로 바꿀 수 있습니다.

편의상 예제 스크립트 the tutorial의 샘플 스키마를 포함 시켰습니다.

두 번째 질문에 대해서는 사용자가 입력하지 않아도 선택 요소가 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";