Reputation: 478
I want to extract the attribute lang
value from the parent tag styling
. How do i obtain this?
I am using libxml.
I tried getAttribute
, but it does not work on parent tag.
<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>
Upvotes: 1
Views: 655
Reputation: 4581
As you mentioned getAttribute
I assume you're using XML::LibXML
. Here's a sample with two methods to get to the attribute value, one with XPath, another with getAttribute
call:
#!/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";
Upvotes: 3
Reputation: 13664
I think by "parent tag", you mean the root element. You probably want the documentElement
method, a 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>
Upvotes: 3
Reputation: 12363
#!/usr/bin/perl
# use module
use XML::Simple;
use Data::Dumper;
# create object
$xml = new XML::Simple;
# read XML file
$data = $xml->XMLin("data.xml");
$data->{styling}{lang};
Upvotes: 1