Преобразование схемы XSD в JSON с помощью Perl - PullRequest
1 голос
/ 29 октября 2011

У меня есть следующая схема XSD http://release.niem.gov/niem/apco/2.1/apco.xsd, и Я написал следующий скрипт на Perl (я новичок в Perl, и, вероятно, в конечном итоге я напишу его на 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";
        }
    }
}

Я получил следующий вывод:

$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.

Теперь

  1. У меня есть правильное представление JSON? (Думаю нет, так как у меня ошибка)
  2. Каково будет правильное представление этой схемы в формате JSON?

Я не эксперт по Perl, меня больше беспокоит правильность представления JSON.

1 Ответ

1 голос
/ 01 ноября 2011
  1. Нет, но ошибка не имеет отношения к форматированию, Perl не может самостоятельно проверить JSON.

  2. Это очень сильно зависит от того, как вы хотите отформатировать JSON. Там нет ни одного ответа на вопрос. Но для начала вам следует ознакомиться с форматом JSON.

Хорошее первое место может быть http://json.org, http://json.org/example.html, приводит несколько примеров сравнения JSON и XML.

Основные сведения о JSON:

Массив

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

Объект, хэш, ассоциативный массив и т. Д.

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

где все k являются строками (см. Ниже), а v могут быть любого типа.

Основные типы:

Строка

"anything in double quotes"

номер

Любая строка цифр с . или без, например,

1
3.14

Другие основные типы

null
true
false

Их можно объединить следующим образом:

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

JSON :: XS - хороший модуль Perl для сериализации и десериализации структур данных Perl в JSON и из него.

EDIT:

Эта ссылка также может содержать некоторую полезную информацию: Создать схему Json из схемы XML (XSD)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...