Dear Monks,
I am doing a big project using mainly XML::Twig. Today during testing i come across a strange problem. I tried in several ways but i could not able to find the why the problem comes. So i need some help here from you guys.
<?xml version="1.0"?>
<article>
<label>here label for testing</label>
<fig id="fig001" position="float"><label>Fig. 1.</label><caption><p>fi
+rst</p></caption></fig>
<fig id="fig002" position="float"><label>Fig. 2.</label><caption><p>se
+cond</p></caption></fig>
<fig id="fig003" position="float"><label>Fig. 3.</label><caption><p>th
+ird</p></caption></fig>
<fig id="fig004" position="float"><label>Fig. 4.</label><caption><p>fo
+urth</p></caption></fig>
<fig id="fig005" position="float"><label>Fig. 5.</label><caption><p>fi
+fth</p></caption></fig>
<fig id="fig006" position="float"><label>Fig. 6.</label><caption><p>si
+xth</p></caption></fig>
</article>
The above is the sample xml file. There i need to cut the 'label' element and place it as a first_child for the 'caption'. So i tried as shown below. The below code is sample code from my original code.
use strict;
use XML::Twig;
my $twig = new XML::Twig(
);
$twig->parsefile('1.xml');
my $count = $twig->get_xpath('//fig//label');
for my $c (0..($count-1))
{
my $t = $twig->get_xpath('//fig//label', $c); #//fig/la
+bel -here problem comes
my $pl = $twig->get_xpath('//fig//caption', $c);
#my $cc = $t->print;# print for testing
#my $aa = $twig->print; #print for testing
#print "$cc\n\n$c\n**$aa\n";
+
my $cut = $t->cut ;
$cut->paste('first_child', $pl) ;
+
}
$twig->print;
I got the exact output as shown below by running the above code.
Correct Output: XPath Expression - "//fig//label"
---------------
<?xml version="1.0"?>
<article>
<label>here label for testing</label>
<fig id="fig001" position="float"><caption><label>Fig. 1.</label><p>fi
+rst</p></caption></fig>
<fig id="fig002" position="float"><caption><label>Fig. 2.</label><p>se
+cond</p></caption></fig>
<fig id="fig003" position="float"><caption><label>Fig. 3.</label><p>th
+ird</p></caption></fig>
<fig id="fig004" position="float"><caption><label>Fig. 4.</label><p>fo
+urth</p></caption></fig>
<fig id="fig005" position="float"><caption><label>Fig. 5.</label><p>fi
+fth</p></caption></fig>
<fig id="fig006" position="float"><caption><label>Fig. 6.</label><p>si
+xth</p></caption></fig>
</article>
My question comes here, I am getting the exact output when i give the XPath Expression as
'//fig//label'. But if i change the XPath Expression as '//fig/label', i am getting error. To identify the problem i checked by printing in the loop every time and the final output i got is as shown below. The XPath Expression is correct, because it is very first child, but i am getting error and not able to get exact output. Where i am going wrong?
Wrong output: XPath Expression - "//fig/label"
-------------
<article>
<label>here label for testing</label>
<fig id="fig001" position="float"><caption><label>Fig. 1.</label><p>fi
+rst</p></caption></fig>
<fig id="fig002" position="float"><label>Fig. 2.</label><caption><labe
+l>Fig. 3.</label><p>second</p></caption></fig>
<fig id="fig003" position="float"><caption><p>third</p></caption></fig
+>
<fig id="fig004" position="float"><label>Fig. 4.</label><caption><p>fo
+urth</p></caption></fig>
<fig id="fig005" position="float"><label>Fig. 5.</label><caption><p>fi
+fth</p></caption></fig>
<fig id="fig006" position="float"><label>Fig. 6.</label><caption><p>si
+xth</p></caption></fig>
</article>
Regards,
Prasad