如何从libxml+;perl

如何从libxml+;perl,xml,perl,xml-parsing,Xml,Perl,Xml Parsing,我想从父标记样式设置中提取属性lang值。我如何获得这个 我正在使用libxml 我尝试了getAttribute,但在父标记上不起作用 <styling lang="en-US"> <style id="jason" tts:color="#00FF00" /> <style id="violet" tts:color="#FF0000" /> <style id="sarah" tts:color="#FFCC00" /> &l

我想从父标记
样式设置
中提取属性
lang
值。我如何获得这个

我正在使用libxml

我尝试了
getAttribute
,但在父标记上不起作用

<styling lang="en-US">
  <style id="jason" tts:color="#00FF00" />
  <style id="violet" tts:color="#FF0000" />
  <style id="sarah" tts:color="#FFCC00" />
  <style id="eileen" tts:color="#3333FF" />
</styling>

我认为“父标记”是指根元素。您可能需要
documentElement
方法,即la:

#!/usr/bin/env perl

use v5.12;
use XML::LibXML 1.70;

my $doc = 'XML::LibXML'->new(recover => 1)->parse_fh(\*DATA);

say "GOT: ", $doc->documentElement->getAttribute('lang');

__DATA__
<styling lang="en-US">
  <style id="jason" tts:color="#00FF00" />
  <style id="violet" tts:color="#FF0000" />
  <style id="sarah" tts:color="#FFCC00" />
  <style id="eileen" tts:color="#3333FF" />
</styling>
#/usr/bin/env perl
使用v5.12;
使用XML::libxml1.70;
my$doc='XML::LibXML'->new(recover=>1)->parse\u fh(\*数据);
说“GOT:”,$doc->documentElement->getAttribute('lang');
__资料__
我认为“父标记”是指根元素。您可能需要
documentElement
方法,即la:

#!/usr/bin/env perl

use v5.12;
use XML::LibXML 1.70;

my $doc = 'XML::LibXML'->new(recover => 1)->parse_fh(\*DATA);

say "GOT: ", $doc->documentElement->getAttribute('lang');

__DATA__
<styling lang="en-US">
  <style id="jason" tts:color="#00FF00" />
  <style id="violet" tts:color="#FF0000" />
  <style id="sarah" tts:color="#FFCC00" />
  <style id="eileen" tts:color="#3333FF" />
</styling>
#/usr/bin/env perl
使用v5.12;
使用XML::libxml1.70;
my$doc='XML::LibXML'->new(recover=>1)->parse\u fh(\*数据);
说“GOT:”,$doc->documentElement->getAttribute('lang');
__资料__

正如您提到的
getAttribute
我假设您使用的是
XML::LibXML
。下面是一个示例,其中包含两种获取属性值的方法,一种使用XPath,另一种使用
getAttribute
调用:

#!/usr/bin/perl

use strict;
use XML::LibXML;

my $xml = <<'EOF';
<styling lang="en-US" xmlns:tts="something">
  <style id="jason" tts:color="#00FF00" />
  <style id="violet" tts:color="#FF0000" />
  <style id="sarah" tts:color="#FFCC00" />
  <style id="eileen" tts:color="#3333FF" />
</styling>
EOF

print XML::LibXML->new->parse_string($xml)->findvalue('/styling/@lang'), "\n";
print XML::LibXML->new->parse_string($xml)->documentElement->getAttribute('lang'), "\n";
#/usr/bin/perl
严格使用;
使用XML::LibXML;
my$xml=parse_string($xml)->findvalue('/styleing/@lang'),“\n”;
打印XML::LibXML->new->parse_string($XML)->documentElement->getAttribute('lang'),“\n”;

正如您提到的
getAttribute
我假设您使用的是
XML::LibXML
。下面是一个示例,其中包含两种获取属性值的方法,一种使用XPath,另一种使用
getAttribute
调用:

#!/usr/bin/perl

use strict;
use XML::LibXML;

my $xml = <<'EOF';
<styling lang="en-US" xmlns:tts="something">
  <style id="jason" tts:color="#00FF00" />
  <style id="violet" tts:color="#FF0000" />
  <style id="sarah" tts:color="#FFCC00" />
  <style id="eileen" tts:color="#3333FF" />
</styling>
EOF

print XML::LibXML->new->parse_string($xml)->findvalue('/styling/@lang'), "\n";
print XML::LibXML->new->parse_string($xml)->documentElement->getAttribute('lang'), "\n";
#/usr/bin/perl
严格使用;
使用XML::LibXML;
my$xml=parse_string($xml)->findvalue('/styleing/@lang'),“\n”;
打印XML::LibXML->new->parse_string($XML)->documentElement->getAttribute('lang'),“\n”;