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


In reply to XML::Twig 'cut' and 'paste' Question by prasadbabu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.