Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I have a problem with a piece of code using XML::Twig. As I don't seem to be able to access the XML::Twig site at the moment I thought I'd ask for some help from my fellow monks.

I maintain some perl code which uses XML::Twig to run checks on dictionary entries written in XML. One check raises a warning if an entry is labelled as "rare" but also contains more than three example quotations.

The rare label would usually look like this:
<header><la>rare</la></header>
but more complex examples could be like:
<header>Now <la>rare</la></header>
or even:
<header>Now <la>hist.</la> and <la>rare</la></header>

For the purposes of the check, the first example is truly rare; the others could be allowed more than three quotes as they were once not-rare. The Perl subroutine which works out whether an entry is to be treated as rare for this check is:

sub is_la_rare { my $elt = shift; foreach my $label ($elt->children('la') ) { next unless $label->text eq "rare"; my $isNowRare = 0; if ( $label->prev_sibling && $label->prev_sibling->text =~ m/[ +nN]ow $/ ) { $isNowRare = 1; } if ( !$isNowRare ) { return 1; } } return 0; }

This works correctly for examples 1 (which it treats as "rare") and example 2 (which it treats as "not-rare"), but it treats example 3 as "rare", which is wrong.

I thought that the prev_sibling->text should pick up the bit of text that does match "Now" even though it is not the nearest prev_sibling to the "<la>rare</la>" but that does not seem to be happening. Have I misunderstood how prev_sibling works or is there some other error with this code?


In reply to XML::Twig prev_sibling by mertserger

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (10)
    As of 2014-12-17 22:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (40 votes), past polls