繁体   English   中英

如何在Perl中使用正则表达式拆分xml文件

[英]How can I split the xml file using regular expression in Perl

这里

<root>
    <SchoolOrInstitution schoolType="highschool">
        <SchoolName>BUTLER HIGH SCHOOL</SchoolName>
        <Degree degreeType="highschool"/>
    </SchoolOrInstitution>
    <SchoolOrInstitution schoolType="highschool">
        <SchoolName>BUTLER HIGH SCHOOL</SchoolName>
        <Degree degreeType="highschool"/>
    </SchoolOrInstitution>
    <SchoolOrInstitution schoolType="highschool">
        <SchoolName>BUTLER HIGH SCHOOL</SchoolName>
        <Degree degreeType="highschool"/>
    </SchoolOrInstitution>
    ..............
</root>

我想根据SchoolOrInstitution标签的数量将此xml拆分为多个文件。

每个XML应该有3个这样的标签。

因此,如果邮件文件具有9个SchoolOrInstitution标记,则应创建3个子文件。

root标记也将分配给这3个文件。

xml_splitXML :: Twig附带的工具,看上去与您要寻找的非常接近。 xml_split -g3 -l1 my.xml将为您提供大部分帮助,唯一的不同是,顶级元素将具有由该工具分配的标签名称,而不是原始名称。

使用xsh ,它是XML :: LibXML的包装器:

my $old := open 19741254.xml ;
my $n = 1;
while $old/root/SchoolOrInstitution[1] {
      my $new := create root ;
      xmv $old/root/SchoolOrInstitution[position() <= 3] into $new/root ;
      save :f concat($n, '.xml') $new ;
      $n = $n + 1 ;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM