【问题标题】:Converting XSD Schema to JSON with Perl使用 Perl 将 XSD Schema 转换为 JSON
【发布时间】: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.

现在,

  1. 我有正确的 JSON 表示吗? (我认为不是,因为我有一个错误)
  2. 此架构的正确 JSON 表示形式是什么?

我不是 Perl 专家,我更担心 JSON 表示的正确性。

【问题讨论】:

  • 我对 JSON 的来源感到困惑,我知道您在 XML 模式中的位置,但我看不到您尝试将其转换为 JSON 的位置。
  • 我现在明白了,当你在做print "$key2 : $array_value->{$key2}\n"; 的时候就是你试图输出 JSON 的时候。好的。

标签: json perl xsd


【解决方案1】:
  1. 否,但错误与格式无关,Perl 无法自行验证 JSON。

  2. 这在很大程度上取决于您希望如何格式化 JSON。这个问题没有一个答案。但是,首先您应该熟悉 JSON 格式。

一个好的第一名可能是http://json.orghttp://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 模块,用于从 JSON 序列化和反序列化 Perl 数据结构。

编辑:

此链接可能还包含一些有用的信息:Generate Json schema from XML schema (XSD)

【讨论】:

  • 是的,我之前已经通过此链接..他们提供了链接,但没有什么实质性的东西可以完成我的工作。
  • 我将通过 JSON::XS 模块,看看是否为我解决了这个问题..感谢您的指导,我刚开始使用 perl..
  • 您是否尝试使用 XSD 验证 JSON 数据?
  • 我更大的目标是将 XSD 转换为 JSON Schema,不幸的是我还没有找到任何可以直接做到这一点的东西....所以我正在尝试一些解决方法,我还发布了以下关于同样stackoverflow.com/questions/7950702/…
  • 查看JSON::Schema,它看起来可能会有所帮助。看起来它需要一个 Perl 数据结构,并且会为它生成一个模式。
猜你喜欢
  • 1970-01-01
  • 2014-02-22
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 2015-04-11
  • 2015-04-11
  • 2015-11-27
相关资源
最近更新 更多