2

I have following XML that I want to create and populate with data through Perl script. In this XML I have some collection type of attributes i.e primarySiteCollection.

How do I dynamically populate and generate the 'primarySiteCollection' node. In this XML the primarySiteCollection contain 3 tissue sites, but there can be 1 or many.

<TumorDetails>
    <personUpi>String</personUpi>
    <ageAtDiagnosis>3.14159E0</ageAtDiagnosis>
    <biopsyPathologyReportSummary>String</biopsyPathologyReportSummary>
    <primarySiteCollection>
        <tissueSite>
            <description>String1</description>
            <name>String1</name>
        </tissueSite>
        <tissueSite>
            <description>String2</description>
            <name>String2</name>
        </tissueSite>
        <tissueSite>
            <description>String3</description>
            <name>String3</name>
        </tissueSite>       
    </primarySiteCollection>
</TumorDetails>

This is my Perl script. I wanted to generate the node dynamically containing collection type of attributes.

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;
5
  • It's not entirely clear what you're trying to do. Do you want to add data to this using a perl script? If so, can you show us the perl script? Otherwise this comes across as a 'do my work for me' question, which generally aren't well received. Commented Dec 12, 2012 at 13:20
  • Absolutely not intended as you are saying above! Commented Dec 12, 2012 at 13:46
  • Can you clarify your question? Commented Dec 12, 2012 at 15:34
  • 1
    xsd always looked like overkill to me. I'd just use a template. Commented Dec 12, 2012 at 18:28
  • This XML is so simple, you could do it with XML::Simple. It's not the best around, the stuff you have there is a lot more powerful, but still. It's simple. Commented Dec 12, 2012 at 21:45

2 Answers 2

2

I think this is simple enough to do it safely with XML::Simple. If it was a lot more complicated, and you would have to take care of complex data types that are derived from objects, a schema would be very helpful. But for this, XML::Simple will do.

#!/usr/bin/perl
use strict;
use warnings;

use XML::Simple;

my $tissueSite = [
  {
    description => 'String',
    name        => 'String',
  },
  {
    description => 'String2',
    name        => 'String2',
  },
];

my $data = {
  tumorDetails => {
    personUpi => 'String',
    ageAtDiagnosis => '3.14159E0',
    biopsyPathologyReportSummary => 'String',
    primarySiteCollection => {
        tissueSite => $tissueSite,
    },
  },
};

print XMLout($data, KeepRoot => 1, noAttr => 1 );

You can add more hashes to $tissueSite.


Remember, if it gets a lot more complicated, another approach is more suited.

Sign up to request clarification or add additional context in comments.

2 Comments

On a different note, the keys in this data structure are kind of morbid. Brr.
Thanks! This is what I was expecting.
0

I followed above approach , works fine.

But what if i don't know size of $tissueSite ( the number of hashes it will have ?) . It can extend/shrink to any size .

Suppose if i collect them in arrays : @decription and @names as per user inputs.

Is it possible to have $tissueSite creation with runtime values from above two arrays ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.