使用 Perl 脚本创建和填充复杂的 XML [英] Create and populate complex XML with Perl script

查看:60
本文介绍了使用 Perl 脚本创建和填充复杂的 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 文件模板,我想用 Perl 脚本创建和填充这些模板.XML 属性的所有值都通过不同的查询来自 SQL 数据库.我的 XML 包含很少的属性集合类型.

I have following XML file template that I want to create and populate with Perl script. All the values of XML attributes are coming from SQL database by different queries. My XML contain few collection type of attributes.

我发现应该使用哪个 perl 模块很困难,因为 CPAN 上有很多替代方案.另外,我想知道我应该如何处理这个问题.

I'm finding it difficult that which perl module should I use because there are a lot of alternatives available on CPAN. Also, I want to know how should I approach to this problem.

非常感谢任何帮助.

`

<TumorDetails>
    <personUpi>String</personUpi>
    <ageAtDiagnosis>3.14159E0</ageAtDiagnosis>
    <biopsyPathologyReportSummary>String</biopsyPathologyReportSummary>
    <primarySiteCollection>
        <tissueSite>
            <description>String</description>
            <name>String</name>
        </tissueSite>
    </primarySiteCollection>
    <distantMetastasisSite>
        <description>String</description>
        <name>String</name>
    </distantMetastasisSite>
    <siteGroup>
        <description>String</description>
        <name>String</name>
    </siteGroup>
    <tmStaging>
        <clinicalDescriptor>String</clinicalDescriptor>
        <clinicalMStage>String</clinicalMStage>
        <siteGroupEdition5>
            <description>String</description>
            <name>String</name>
        </siteGroupEdition5>
        <siteGroupEdition6>
            <description>String</description>
            <name>String</name>
        </siteGroupEdition6>
    </tmStaging>
    <pediatricStaging>
        <doneBy>String</doneBy>
        <group>String</group>
    </pediatricStaging>
    <histologicTypeCollection>
        <histologicType>
            <description>String</description>
            <system>String</system>
            <value>String</value>
        </histologicType>
    </histologicTypeCollection>
    <histologicGradeCollection>
        <histologicGrade>
            <gradeOrDifferentiation>String</gradeOrDifferentiation>
        </histologicGrade>
    </histologicGradeCollection>
    <familyHistoryCollection>
        <familyHistory>
            <otherCancerDiagnosed>String</otherCancerDiagnosed>
            <sameCancerDiagnosed>String</sameCancerDiagnosed>
        </familyHistory>
    </familyHistoryCollection>
    <comorbidityOrComplicationCollection>
        <comorbidityOrComplication>
            <value>String</value>
        </comorbidityOrComplication>
    </comorbidityOrComplicationCollection>
    <tumorBiomarkerTest>
        <her2NeuDerived>String</her2NeuDerived>
        <her2NeuFish>String</her2NeuFish>
    </tumorBiomarkerTest>
    <patientHistoryCollection>
        <patientHistory>
            <cancerSite>String</cancerSite>
            <sequence>2147483647</sequence>
        </patientHistory>
    </patientHistoryCollection>
    <tumorHistory>
        <cancerStatus>String</cancerStatus>
        <cancerStatusFollowUpDate>1967-08-13</cancerStatusFollowUpDate>
        <cancerStatusFollowUpType>String</cancerStatusFollowUpType>
        <qualityOfSurvival>String</qualityOfSurvival>
    </tumorHistory>
    <placeOfDiagnosis>
        <initials>String</initials>
    </placeOfDiagnosis>
    <followUp>
        <dateFollowUpChanged>String</dateFollowUpChanged>
        <dateOfLastCancerStatus>1967-08-13</dateOfLastCancerStatus>
        <nextFollowUpHospital>
            <initials>String</initials>
        </nextFollowUpHospital>
        <lastFollowUpHospital>
            <initials>String</initials>
        </lastFollowUpHospital>
        <tumorFollowUpBiomarkerTest>
            <her2NeuDerived>String</her2NeuDerived>
            <her2NeuFish>String</her2NeuFish>
        </tumorFollowUpBiomarkerTest>
    </followUp>
</TumorDetails>

`

推荐答案

首先,非常非常重要的概念:没有XML 模板"这样的东西!使用 XML 的全部意义在于能够根据一些模式读/写数据.如果您有(一致的)XML 示例但没有模式定义 (XSD),请使用 trang弄清楚:

First, the very, very important concept: there is no such thing as "XML template"! The whole point of using XML is being capable to read/write data according to some schema. If you have a (consistent) XML sample but no schema definition (XSD), use trang to figure it out:

java -jar trang.jar sample.xml sample.xsd

对于提供的示例,生成的 XSD 文件如下所示:

For provided sample, the generated XSD file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="TumorDetails">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="personUpi"/>
        <xs:element ref="ageAtDiagnosis"/>
        <xs:element ref="biopsyPathologyReportSummary"/>
        <xs:element ref="primarySiteCollection"/>
        <xs:element ref="distantMetastasisSite"/>
        <xs:element ref="siteGroup"/>
        <xs:element ref="tmStaging"/>
        <xs:element ref="pediatricStaging"/>
        <xs:element ref="histologicTypeCollection"/>
        <xs:element ref="histologicGradeCollection"/>
        <xs:element ref="familyHistoryCollection"/>
        <xs:element ref="comorbidityOrComplicationCollection"/>
        <xs:element ref="tumorBiomarkerTest"/>
        <xs:element ref="patientHistoryCollection"/>
        <xs:element ref="tumorHistory"/>
        <xs:element ref="placeOfDiagnosis"/>
        <xs:element ref="followUp"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
...
</xs:schema>

现在,最好的部分称为 XML::Compile.它采用您的 XSD 模式,对其进行编译并适合/验证本机 Perl 结构,生成 XML 作为输出:

And now, the best part, called XML::Compile. It takes your XSD schema, compiles it and fits/validates native Perl structures, producing XML as an output:

#!/usr/bin/env perl
use strict;
use warnings;
use XML::Compile::Schema;

my $node = {
    personUpi                    => 'String',
    ageAtDiagnosis               => '3.14159E0',
    biopsyPathologyReportSummary => 'String',
    primarySiteCollection        => {
        tissueSite => {
            description => 'String',
            name        => 'String',
        },
    },
    ...
};

my $schema = XML::Compile::Schema->new('sample.xsd');
my $writer = $schema->compile(WRITER => 'TumorDetails');
my $doc = XML::LibXML::Document->new(q(1.0), q(UTF-8));

print $writer->($doc, $node)->toString;

这篇关于使用 Perl 脚本创建和填充复杂的 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆