I wrote this originally on blogs.perl.org: Using XML::Compile to output XSD compliant XML. It may help if you decide to make an XSD file.

As part of a recent project I was given an XSD file (xml schema definition) and asked to output compliant XML. CPAN to the rescue. I found XML::Compile::Schema which is a cool module that allowed me to do this with very little fuss. The documentation is really good but I think a tutorial-style post might be helpful.

To do this you’ll need to install XML::Compile and XML::LibXML.

You can use XML::Compile::Schema to read in your xsd file and output a perl hash template. Then you can use that example template to construct a hash of real data and then have XML::Compile::Schema output a valid XML file.

For this tutorial, download a sample .xsd file from here. Then write a perl script like so to dump a perl hash template.

#!/usr/local/bin/perl use warnings; use strict; use Data::Dumper; use XML::Compile::Schema; use XML::LibXML::Reader; my $xsd = 'test.xsd'; my $schema = XML::Compile::Schema->new($xsd); # This will print a very basic description of what the schema describe +s $schema->printIndex(); # this will print a hash template that will show you how to construct +a # hash that will be used to construct a valid XML file. # # Note: the second argument must match the root-level element of the X +ML # document. I'm not quite sure why it's required here. warn $schema->template('PERL', 'addresses');

The relevant output looks like this:

# is an unnamed complex { # sequence of address # is an unnamed complex # occurs 1 <= # <= unbounded times address => [ { # sequence of name, street # is a xs:string # is optional name => "example", # is a xs:string # is optional street => "example", }, ], }

The comments are helpful (and were provided by XML::Compile::Schema directly, not by me). It basically says your data structure should start as a hashref which should contain an entry called “address” which is a reference to an array. This array should be a list of hash references which each contain two elements, name and street.

From this you can deduce that a valid hash will look something like this.

my $data = { address => [ { name => 'name 1', street => 'street 1', }, { name => 'name 2', street => 'street 2', } ], };

In order to output the XML, you have to do this:

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

My output looks like this:

<?xml version="1.0" encoding="UTF-8"?> <addresses> <address> <name>name 1</name> <street>street 1</street> </address> <address> <name>name 2</name> <street>street 2</street> </address> </addresses>

The actual XSD and resulting XML files I was dealing with were much more complicated but I followed this process and had no trouble whatsoever.


In reply to Re: Module for XML output by saberworks
in thread Module for XML output by rebugger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.