Re: Retrieving XML attribute using XML::XPath::Node::Attribute



vikrant wrote:

I am trying to retrieve an attribute of a particular node from my XML
using "XML::XPath::Node::Attribute", but couldn't come across on how to
successfully use it in my code. For example, if my xml is:

<?xml version="1.0" ?>
	<data>
		<server_address port="40">10.0.0.1</server_address>
	</data>

How do I go about fetching the "port" attribute from "server_address"
element?


Use a regular XPath query, ending with @port to get the port attribute:

#!/usr/bin/perl
use strict;
use warnings;

use XML::XPath;

my $xp = XML::XPath->new( ioref => \*DATA);

print "port: ", $xp->findvalue('/data/server_address/@port'), "\n";


__DATA__ <?xml version="1.0" ?> <data> <server_address port="40">10.0.0.1</server_address> </data> .