Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The XML:#! /usr/bin/perl use strict; use XML::Twig; my $gobjXML; my $ghshAvail = {}; eval { $gobjXML = XML::Twig->new( twig_handlers => { availability_messages => \&xxx } ); $gobjXML->parsefile("test.xml"); $gobjXML->purge(); $gobjXML->dispose(); undef($gobjXML); }; sub xxx { my($objt,$objnode) = @_; my $exp = qq(./message[\@id]); foreach my $node ($objnode->get_xpath($exp)) { if( ! defined($ghshAvail->{$node->{att}->{'id'}})) { $ghshAvail->{$node->{att}->{'id'}} = $node->te +xt(); } print "The attrib is " . $node->{att}->{"id"} . " the +value is " . $node->text() . "\n"; } }
The above mentioned code and the sample XML file are cut from the production code,where the process parses a XML which is really huge in size. The above code works right with the sample XML attached. The issue is when I run the same code with the larger XML where I have the above mentioned elements plus lots of other stuff , the value for the attribute "id" are swapped or random. That is once i do an xpath like "./message@id", I get "currently in stock" as the value for "a1" and "a3" gets "temporarily out of stock..." . Do you know if there is any issue with XML twig with this kind of situation.<?xml version="1.0"?> <xmlfeed> <availability_messages> <message id="a1"><![CDATA[temporarily out of stock - will ship + in 1-2 weeks]]></message> <message id="a2"><![CDATA[This item is temporarily<br> out of +stock and will<br> ship in approximately<br> <b>1 - 2 weeks.</b> <fon +t size=1><a href="http://www.drugstore.com/cat/10661/tmpl/default.asp +?catid=15604#15448">learn more</a></font>]]></message> <message id="a3"><![CDATA[currently in stock]]></message> <message id="a4"><![CDATA[* This item is currently <b>in stock +.</b> *]]></message> <message id="a5"><![CDATA[temporarily out of stock - will ship + in 2-4 weeks]]></message> <message id="a6"><![CDATA[This item is temporarily<br> out of +stock and will<br> ship in approximately<br> <b>2 - 4 weeks.</b> <fon +t size=1><a href="http://www.drugstore.com/cat/10661/tmpl/default.asp +?catid=15604#15448">learn more</a></font>]]></message> <message id="a7"><![CDATA[temporarily unavailable from manufac +turer - will ship in 4-6 weeks]]></message> <message id="a8"><![CDATA[This item is currently unavailable<b +r> from the manufacturer and will<br> ship in approximately <b>4 - 6 +weeks.</b><br> <font size=1><a href="http://www.drugstore.com/cat/10661/tmpl/default.asp?catid=15604# +15448">learn more</a></font>]]></message> </availability_messages> </xmlfeed>
Edited by castaway: added code tags.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Twig issues
by mirod (Canon) on Sep 22, 2003 at 20:00 UTC |