【发布时间】:2011-12-17 21:56:29
【问题描述】:
我有以下 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.
现在,
- 我有正确的 JSON 表示吗? (我认为不是,因为我有一个错误)
- 此架构的正确 JSON 表示形式是什么?
我不是 Perl 专家,我更担心 JSON 表示的正确性。
【问题讨论】:
-
我对 JSON 的来源感到困惑,我知道您在 XML 模式中的位置,但我看不到您尝试将其转换为 JSON 的位置。
-
我现在明白了,当你在做
print "$key2 : $array_value->{$key2}\n";的时候就是你试图输出 JSON 的时候。好的。