1

我正在尝试解析 Articulate eLearning 课程 (imsmanifest.xml) 的 XML 清单文件。

下面提供了 XML 结构的摘录(我正在尝试深入了解 adlcp:masteryscore):

<?xml version="1.0" encoding="UTF-8"?>
<manifest xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" version="1.0" identifier="Electrical_Design_Part_3">
    <metadata/>
    <organizations default="Electrical_Design_Part_3_ORG">
      <organization identifier="Electrical_Design_Part_3_ORG">
        <title>Electrical Design - Part 3</title>
        <item identifier="Electrical_Design_Part_3_SCO" identifierref="Articulate_Presenter_RES" isvisible="true">
          <title>Electrical Design - Part 3</title>
          <adlcp:masteryscore>65</adlcp:masteryscore>
        </item>
      </organization>
    </organizations>
    <resources/>
</manifest>

我尝试过使用 XML::Simple 和 XML::LibXML。我可以让这些模块与更简单的 XML 文件一起正常工作,但不是我实际需要解析的清单文件。

以下代码显示了我尝试使用 XML::LibXML 深入到标题标签:

use XML::LibXML;
$filename = "imsmanifest.xml";
$parser = XML::LibXML->new();
$xmldoc = $parser->parse_file($filename);

for my $sample ($xmldoc->findnodes('/manifest/organizations/organization/item/title')) {
    for my $property ($sample->findnodes('./*')) {
        print $property->nodeName(), ": ", $property->textContent(), "\n";
    }
    print "\n"; 
};

如何处理 adlcp:masteryscore 标签中的冒号?每当我尝试使用它时,都会出现错误 - 但也许我做得不对。

有人可以告诉我深入了解 adlcp:masteryscore 的正确方法吗?

非常感谢你。

4

3 回答 3

4

您要求定位manifest在 null 命名空间中命名的元素,但您想要manifest在命名空间中http://www.imsproject.org/xsd/imscp_rootv1p1p2命名的元素。

修复:

use strict;
use warnings;

use XML::LibXML               qw( );
use XML::LibXML::XPathContext qw( );

my $xml_qfn = 'imsmanifest.xml';

my $parser = XML::LibXML->new( no_network => 1 );
my $doc = $parser->parse_file($xml_qfn);

my $xpc = XML::LibXML::XPathContext->new();
$xpc->registerNs( a => "http://www.adlnet.org/xsd/adlcp_rootv1p2" );
$xpc->registerNs( i => "http://www.imsproject.org/xsd/imscp_rootv1p1p2" );

for my $item ($xpc->findnodes('/i:manifest/i:organizations/i:organization/i:item', $doc)) {
    my $title   = $xpc->find('i:title/text()', $item);
    my $mastery = $xpc->find('a:masteryscore/text()', $item);
    print "$title: $mastery\n"; 
}

a注意:在 XPath (和)中使用的前缀的实际选择i是任意的。您可以选择任何您想要的东西,就像编写 XML 文档一样。

注意:我添加no_network => 1了防止 libxml 在您每次解析 XML 文档时获取 DTD。

于 2013-03-29T23:01:21.113 回答
0

第一步,修复您的示例,使其格式正确 xml

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd" version="1.0" identifier="Electrical_Design_Part_3">
    <metadata>
    <organizations default="Electrical_Design_Part_3_ORG">
      <organization identifier="Electrical_Design_Part_3_ORG">
        <title>Electrical Design - Part 3</title>
        <item identifier="Electrical_Design_Part_3_SCO" identifierref="Articulate_Presenter_RES" isvisible="true">
          <title>Electrical Design - Part 3</title>
          <adlcp:masteryscore>65</adlcp:masteryscore>
        </item>
      </organization>
    </organizations>
    <resources/>
</metadata>
</manifest>

启动 perl 调试器

DB<2> use XML::Simple

  DB<3> $x=XMLin("example.xml")

  DB<4> x $x
0  HASH(0x2733c48)
   'identifier' => 'Electrical_Design_Part_3'
   'metadata' => HASH(0x2733828)
      'organizations' => HASH(0x2733288)
         'default' => 'Electrical_Design_Part_3_ORG'
         'organization' => HASH(0x272d7e8)
            'identifier' => 'Electrical_Design_Part_3_ORG'
            'item' => HASH(0x27285f8)
               'adlcp:masteryscore' => 65
               'identifier' => 'Electrical_Design_Part_3_SCO'
               'identifierref' => 'Articulate_Presenter_RES'
               'isvisible' => 'true'
               'title' => 'Electrical Design - Part 3'
            'title' => 'Electrical Design - Part 3'
      'resources' => HASH(0x27333d8)
           empty hash
   'version' => 1.0
   'xmlns' => 'http://www.imsproject.org/xsd/imscp_rootv1p1p2'
   'xmlns:adlcp' => 'http://www.adlnet.org/xsd/adlcp_rootv1p2'
   'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
   'xsi:schemaLocation' => 'http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd'

  DB<6> x keys %$x
0  'xmlns'
1  'xmlns:xsi'
2  'identifier'
3  'version'
4  'metadata'
5  'xsi:schemaLocation'
6  'xmlns:adlcp'
  DB<9> x keys %{$x->{metadata}}
0  'resources'
1  'organizations'
  DB<10> x keys %{$x->{metadata}{organizations}}
0  'default'
1  'organization'
  DB<11> x keys %{$x->{metadata}{organizations}{organizations}
Missing right curly or square bracket at (eval 22)[/usr/share/perl/5.14/perl5db.pl:640] line 4, at end of line
syntax error at (eval 22)[/usr/share/perl/5.14/perl5db.pl:640] line 4, at EOF
  DB<12> x keys %{$x->{metadata}{organizations}{organizations}}
  empty array
  DB<13> x keys %{$x->{metadata}{organizations}{organization}}
0  'identifier'
1  'item'
2  'title'
  DB<14> x keys %{$x->{metadata}{organizations}{organization}{item}}
0  'identifier'
1  'identifierref'
2  'isvisible'
3  'title'
4  'adlcp:masteryscore'
  DB<19> x $x->{metadata}{organizations}{organization}{item}{'adlcp:masteryscore'}
0  65
  DB<20> 

所以你所要做的就是

use XML::Simple;
$x=XMLIN("example.xml");
print $x->{metadata}{organizations}{organization}{item}{'adlcp:masteryscore'};

希望这可以帮助

于 2013-03-29T21:46:59.240 回答
0

xml 无效,您需要关闭标签元数据和资源

之后 XML::Simple 将使用此代码

#!/usr/bin/env perl 

use strict;
use warnings;
use XML::Simple;
use Data::Dumper;


use XML::Simple qw(:strict);

my $ref = XMLin('test.xml',ForceArray => [], KeyAttr => {});
print STDERR Dumper $ref;
于 2013-03-29T21:47:44.260 回答