1

I have the following XSD schema http://release.niem.gov/niem/apco/2.1/apco.xsd, and I wrote the following Perl script (I am a novice with Perl and I will probably end up writing it in C#):

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

# use module
use XML::Simple;
use Data::Dumper;

my $data = XMLin('apco.xsd');    
print Dumper($data);      

for my $key1 (keys %$data) {
    print "$key1\n";
    for my $array_value (@{ $data->{$key1} }) {
        for my $key2 (keys %$array_value) {
            print "$key2 : $array_value->{$key2}\n";
        }
    }
}

I got the following output:

$VAR1 = {
          'xsd:import' => [
                          {
                            'namespace' => 'http://niem.gov/niem/structures/2.0',
                            'schemaLocation' => '../../structures/2.0/structures.xsd'
                          },
                          {
                            'namespace' => 'http://niem.gov/niem/appinfo/2.0',
                            'schemaLocation' => '../../appinfo/2.0/appinfo.xsd'
                          }
                        ],
          'version' => '1',
          'xmlns:s' => 'http://niem.gov/niem/structures/2.0',
          'targetNamespace' => 'http://niem.gov/niem/apco/2.1',
          'xmlns:i' => 'http://niem.gov/niem/appinfo/2.0',
          'xsd:simpleType' => {
                              'AlarmEventCategoryCodeSimpleType' => {
                                                                    'xsd:restriction' => {
                                                                                         'base' => 'xsd:token',
                                                                                         'xsd:enumeration' => [
                                                                                                              {
                                                                                                                'value' => 'Medical',
                                                                                                                'xsd:annotation' => {
                                                                                                                                    'xsd:documentation' => 'Medical Alarm'
                                                                                                                                  }
                                                                                                              },
                                                                                                              {
                                                                                                                'value' => 'FIRE',
                                                                                                                'xsd:annotation' => {
                                                                                                                                    'xsd:documentation' => 'Fire'
                                                                                                                                  }
                                                                                                              },
                                                                                                              {
                                                                                                                'value' => 'Comm',
                                                                                                                'xsd:annotation' => {
                                                                                                                                    'xsd:documentation' => 'Communication Fail'
                                                                                                                                  }
                                                                                                              },
                                                                                                              {
                                                                                                                'value' => 'BURG',
                                                                                                                'xsd:annotation' => {
                                                                                                                                    'xsd:documentation' => 'Burglary'
                                                                                                                                  }
                                                                                                              },
                                                                                                              {
                                                                                                                'value' => 'Holdup',
                                                                                                                'xsd:annotation' => {
                                                                                                                                    'xsd:documentation' => 'Holdup / Duress'
                                                                                                                                  }
                                                                                                              }
                                                                                                            ]
                                                                                       },
                                                                    'xsd:annotation' => {
                                                                                        'xsd:appinfo' => {
                                                                                                         'i:Base' => {
                                                                                                                     'i:name' => 'Object',
                                                                                                                     'i:namespace' => 'http://niem.gov/niem/structures/2.0'
                                                                                                                   }
                                                                                                       },
                                                                                        'xsd:documentation' => 'A data type for kinds of alarm events.'
                                                                                      }
                                                                  },
                              'AlarmEventResponseActionCodeSimpleType' => {
                                                                          'xsd:restriction' => {
                                                                                               'base' => 'xsd:token',
                                                                                               'xsd:enumeration' => [
                                                                                                                    {
                                                                                                                      'value' => 'respond',
                                                                                                                      'xsd:annotation' => {
                                                                                                                                          'xsd:documentation' => 'respond'
                                                                                                                                        }
                                                                                                                    },
                                                                                                                    {
                                                                                                                      'value' => 'notify',
                                                                                                                      'xsd:annotation' => {
                                                                                                                                          'xsd:documentation' => 'notify'
                                                                                                                                        }
                                                                                                                    }
                                                                                                                  ]
                                                                                             },
                                                                          'xsd:annotation' => {
                                                                                              'xsd:appinfo' => {
                                                                                                               'i:Base' => {
                                                                                                                           'i:name' => 'Object',
                                                                                                                           'i:namespace' => 'http://niem.gov/niem/structures/2.0'
                                                                                                                         }
                                                                                                             },
                                                                                              'xsd:documentation' => 'A data type for actions requested of an alarm event responder.'
                                                                                            }
                                                                        },
                              'AlarmEventLocationCategoryCodeSimpleType' => {
                                                                            'xsd:restriction' => {
                                                                                                 'base' => 'xsd:token',
                                                                                                 'xsd:enumeration' => [
                                                                                                                      {
                                                                                                                        'value' => 'commercial',
                                                                                                                        'xsd:annotation' => {
                                                                                                                                            'xsd:documentation' => 'commercial'
                                                                                                                                          }
                                                                                                                      },
                                                                                                                      {
                                                                                                                        'value' => 'residential',
                                                                                                                        'xsd:annotation' => {
                                                                                                                                            'xsd:documentation' => 'residential'
                                                                                                                                          }
                                                                                                                      }
                                                                                                                    ]
                                                                                               },
                                                                            'xsd:annotation' => {
                                                                                                'xsd:appinfo' => {
                                                                                                                 'i:Base' => {
                                                                                                                             'i:name' => 'Object',
                                                                                                                             'i:namespace' => 'http://niem.gov/niem/structures/2.0'
                                                                                                                           }
                                                                                                               },
                                                                                                'xsd:documentation' => 'A data type for the kinds of location at which an alarm event occurs.'
                                                                                              }
                                                                          }
                            },
          'xmlns:apco' => 'http://niem.gov/niem/apco/2.1',
          'xsd:annotation' => {
                              'xsd:appinfo' => {
                                               'i:ConformantIndicator' => 'true'
                                             },
                              'xsd:documentation' => 'Association of Public-Safety Communications Officials (APCO) - International, Inc.'
                            },
          'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
          'xsd:complexType' => {
                               'AlarmEventCategoryCodeType' => {
                                                               'xsd:annotation' => {
                                                                                   'xsd:appinfo' => {
                                                                                                    'i:Base' => {
                                                                                                                'i:name' => 'Object',
                                                                                                                'i:namespace' => 'http://niem.gov/niem/structures/2.0'
                                                                                                              }
                                                                                                  },
                                                                                   'xsd:documentation' => 'A data type for kinds of alarm events.'
                                                                                 },
                                                               'xsd:simpleContent' => {
                                                                                      'xsd:extension' => {
                                                                                                         'base' => 'apco:AlarmEventCategoryCodeSimpleType',
                                                                                                         'xsd:attributeGroup' => {
                                                                                                                                 'ref' => 's:SimpleObjectAttributeGroup'
                                                                                                                               }
                                                                                                       }
                                                                                    }
                                                             },
                               'AlarmEventResponseActionCodeType' => {
                                                                     'xsd:annotation' => {
                                                                                         'xsd:appinfo' => {
                                                                                                          'i:Base' => {
                                                                                                                      'i:name' => 'Object',
                                                                                                                      'i:namespace' => 'http://niem.gov/niem/structures/2.0'
                                                                                                                    }
                                                                                                        },
                                                                                         'xsd:documentation' => 'A data type for actions requested of an alarm event responder.'
                                                                                       },
                                                                     'xsd:simpleContent' => {
                                                                                            'xsd:extension' => {
                                                                                                               'base' => 'apco:AlarmEventResponseActionCodeSimpleType',
                                                                                                               'xsd:attributeGroup' => {
                                                                                                                                       'ref' => 's:SimpleObjectAttributeGroup'
                                                                                                                                     }
                                                                                                             }
                                                                                          }
                                                                   },
                               'AlarmEventLocationCategoryCodeType' => {
                                                                       'xsd:annotation' => {
                                                                                           'xsd:appinfo' => {
                                                                                                            'i:Base' => {
                                                                                                                        'i:name' => 'Object',
                                                                                                                        'i:namespace' => 'http://niem.gov/niem/structures/2.0'
                                                                                                                      }
                                                                                                          },
                                                                                           'xsd:documentation' => 'A data type for the kinds of location at which an alarm event occurs.'
                                                                                         },
                                                                       'xsd:simpleContent' => {
                                                                                              'xsd:extension' => {
                                                                                                                 'base' => 'apco:AlarmEventLocationCategoryCodeSimpleType',
                                                                                                                 'xsd:attributeGroup' => {
                                                                                                                                         'ref' => 's:SimpleObjectAttributeGroup'
                                                                                                                                       }
                                                                                                               }
                                                                                            }
                                                                     }
                             }
        };
xsd:import
namespace : http://niem.gov/niem/structures/2.0
schemaLocation : ../../structures/2.0/structures.xsd
namespace : http://niem.gov/niem/appinfo/2.0
schemaLocation : ../../appinfo/2.0/appinfo.xsd
version
Can't use string ("1") as an ARRAY ref while "strict refs" in use at C:\Users\asraina\Downloads\script.pl line 16.

Now,

  1. Do I have the correct JSON representation? (I think not, since I have an error)
  2. What would be the correct JSON representation of this schema?

I am not an expert in Perl, I am more worried about the correctness of the JSON representation.

ashutosh raina
  • 9,228
  • 12
  • 44
  • 80
  • I'm confused as to where the JSON comes in, I see where you're taking in the XML schema, but I don't see where you're trying to convert it into JSON. –  Nov 01 '11 at 02:28
  • 1
    I see now, when you're doing `print "$key2 : $array_value->{$key2}\n";` is when you're trying to output JSON. OK. –  Nov 01 '11 at 02:32

1 Answers1

1
  1. No, but the error has nothing to do with the formatting, Perl can't validate JSON on it's own.

  2. That would very much depend on how you'd like to format the JSON. There's not one answer to the question. But, to begin with you should become familiar with the JSON format.

A good first place may be http://json.org, http://json.org/example.html, gives some examples comparing JSON and XML.

For a basic summary of JSON:

Array

[ 1, 2, 3, ... , n ]

Object, Hash, Associative Array, etc.

{ k1 : v1, k2 : v2, ... , kn : vn }

where all the k's are strings (see below) and the v's can be any type.

The basic types are as follows:

String

"anything in double quotes"

Number

Any string of digits with or without a . e.g.

1
3.14

Other Basic Types

null
true
false

These can be combined as follows:

{ "names" : 
    [ "Newton", "Copernicus", "Einstein" ],
  "constants" : 
    [ 3.14159, 1.618033, 0 ],
  "valid" : true,
  "dates" : {
     "first" : "2001-02-14",
     "last"  : "2001-11-25"
  }
}

JSON::XS is a good Perl Module for Serializing and Deserializing Perl data structures to and from JSON.

EDIT:

This link may also contain some useful information: Generate Json schema from XML schema (XSD)

Community
  • 1
  • 1
  • yes i have been through this link before ..they provide links but nothing substantial that will get my job done . – ashutosh raina Nov 01 '11 at 05:26
  • i will go through the JSON::XS module and see if solves the issue for me ..thanks for the guidance ,i am just starting out with perl.. – ashutosh raina Nov 01 '11 at 05:26
  • Are you trying to use the XSD to validate JSON data? –  Nov 01 '11 at 07:28
  • my bigger goal is to convert XSD into JSON Schema ,unfortunately i haven't found anything that directly does that ....so i am trying a few workarounds ,i also posted the following question regarding the same http://stackoverflow.com/questions/7950702/is-this-correcr-jsonschema-json-for-the-xsd – ashutosh raina Nov 01 '11 at 07:32
  • 1
    Check out [JSON::Schema](https://metacpan.org/module/JSON::Schema) it looks like it might help. It looks like it'll take a Perl data structure and it will generate a schema for it. –  Nov 01 '11 at 07:44
  • thanks a ton , i am more into C# but if perl solves my problem then perl it shall be ... – ashutosh raina Nov 01 '11 at 07:45
  • NP, I don't mind C# myself but Perl is quite a flexible system especially with CPAN. –  Nov 01 '11 at 07:47