Home: Perl Programming Help: Intermediate:
XML Xpath Perl help


Sep 8, 2007, 9:17 AM

Views: 1553
XML Xpath Perl help


I am using XPath to retrieve the node data from an XML file. I used the following code to extract the attribute data from the file. I manually entered the attribute name in the code and I am able to extract the data properly. But when I tried to read the attribute name into a variable and use this variablename instead of actual data it is giving me errors.

Here is my XML file:
<authordata authorid="1">Name Author</authordata>
<address>name address</address>
<publication>name publication</publication>

Code with manually entered attribute name:
my $read = XML::XPath->new(filename=>'sample.xml');
my $nodedata = $read->find('//@authorid');

Code with the variablename:
my $nodedata=$read->find('//$authoridno');

Could anyone please help me with this and also what should i do to extract nodename instead of attribute name in the code. Mean what should i do to extract the data inside the node authordata in the XML file. I'm doing this because ill be parsing the dtd and ill use the nodename to extract the respective data in the node.
Thanks in advance.


Sep 11, 2007, 5:32 AM

Views: 1551
Re: [akhilak] XML Xpath Perl help


When you are using variable you need to give double quote not the single quote

i am sure the below code will work for you

my $nodedata=$read->find("//\@$authoridno");