Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

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

XML::Twig only returning first node, not all nodes matching as it carries over the previous JobActiveDate value. If I flush the sub DumpOldAds, it throws out all ads!/p>

I have this xml I'm massaging for a feed of job ads:

<?xml version="1.0" encoding="UTF-8"?> <nis:Jobs xsi:schemaLocation="http://schemas.monster.com/Monster http: +//schemas.monster.com/Current/XSD/Job.xsd http://schemas.monster.com/ +Monster/NIS http://schemas.monster.com/Current/Extensions/NIS/XSD/NIS +Job.xsd" xmlns:nis="http://schemas.monster.com/Monster/NIS" xmlns="ht +tp://schemas.monster.com/Monster" xmlns:xsi="http://www.w3.org/2001/X +MLSchema-instance"> <nis:NISJob> <Job jobRefCode="NewsMinimumXMLSample" jobAction="addOrUpdate"> <RecruiterReference> <UserName>nis123456_jsmith</UserName> </RecruiterReference> <CompanyReference> <CompanyXCode>xnis123456_jsmithx</CompanyXCode> <CompanyName><![CDATA[John Smith Press]]></CompanyName> </CompanyReference> <Channel monsterId="4770"/> <JobInformation> <JobTitle><![CDATA[Printer]]></JobTitle> <Contact> <StructuredName> <GivenName><![CDATA[John]]></GivenName> <FamilyName><![CDATA[Smith]]></FamilyName> </StructuredName> <Address> <StreetAddress>1 Main Street</StreetAddress> <City>Maynard</City> <State>MA</State> <CountryCode>US</CountryCode> <PostalCode>01754</PostalCode> </Address> <Phones> <Phone phoneType="contact">8885551212</Phone> </Phones> <E-mail>news.dev.sample@monster.com</E-mail> </Contact> <JobBody><![CDATA[<font face="StdNewtonABold" size=1 color +="#000000"><dl><dl><dd>Job Body goes here</font></dl></dl>]]></JobBod +y> </JobInformation> <JobPostings> <JobPosting> <Location> <CountryCode>US</CountryCode> <PostalCode>01754</PostalCode> <Continent>NA</Continent> </Location> <JobCategory monsterId="2"/> <JobOccupations> <JobOccupation monsterId="11713"/> </JobOccupations> <BoardName monsterId="1"/> <JobPostingDates> <JobActiveDate>2011-11-28T00:00:00</JobActiveDate> <JobExpireDate>2011-12-28T00:00:00</JobExpireDate> </JobPostingDates> </JobPosting> </JobPostings> </Job> </nis:NISJob> </nis:Jobs>

Using this code:

#!/usr/bin/perl -w use strict; use warnings; use XML::Twig; use Date::Manip; use Data::Dumper; my $datetoday = UnixDate("today","%m-%d-%Y"); chomp $datetoday; my $xmlFile = "WacoTri_pre_$datetoday.xml"; my $xmlOut = "WacoTri_$datetoday.xml"; open (XMLOUT, ">$xmlOut"); my $twig = new XML::Twig( twig_handlers => { 'CompanyXCode' => \&lc +cXCode, 'JobPostingDates' => \&DoDate, 'nis:NISJob' => \&DumpOldAds }, pretty_print => 'indented', ); $twig->parsefile ($xmlFile); $twig->flush; $twig-> print (\*XMLOUT); close XMLOUT; sub DoDate { my ( $twig, $JobPostingDates)= @_; my $dtidate = $JobPostingDates->first_child('JobActiveDate')-> +text; my $dater = substr $dtidate, 0, 10; my $newDate = (DateCalc($dater,"+ 7 days")); my $outputdate = UnixDate($newDate, "%Y-%m-%d"); chomp $outputdate; substr $dtidate, 0, 10, "$outputdate"; $JobPostingDates->first_child('JobExpireDate')->set_text($dtid +ate); #$twig->flush; } sub lccXCode { my ( $twig, $CompanyXCode ) = @_; my $XCode = $CompanyXCode->text; my $lowercaseXCode = lc ($XCode); $CompanyXCode->set_text ($lowercaseXCode); } sub DumpOldAds { my ($twig,$nisNISJob)=@_; my $date = UnixDate("today", "%Y-%m-%d".'T00:00:00'); chomp $date; my $adate = $nisNISJob->findvalue('.//JobActiveDate'); print "\n$adate -|- $date\n"; if( $adate ne $date) { $nisNISJob->delete; } else { print $nisNISJob; }; }

What happens is that everything works as expected - Except that the output file contains only the first ad, not all the ads with a JobActiveDate of today as specified in the conditional sub DumpOldAds. I should get:

<?xml version="1.0" encoding="UTF-8"?> <nis:Jobs xsi:schemaLocation="http://schemas.monster.com/Monster http: +//schemas.monster.com/Current/XSD/Job.xsd http://schemas.monster.com/ +Monster/NIS http://schemas.monster.com/Current/Extensions/NIS/XSD/NIS +Job.xsd" xmlns:nis="http://schemas.monster.com/Monster/NIS" xmlns="ht +tp://schemas.monster.com/Monster" xmlns:xsi="http://www.w3.org/2001/X +MLSchema-instance"> <nis:NISJob> <Job jobRefCode="NewsMinimumXMLSample" jobAction="addOrUpdate"> <RecruiterReference> <UserName>nis123456_jsmith</UserName> </RecruiterReference> <CompanyReference> <CompanyXCode>xnis123456_jsmithx</CompanyXCode> <CompanyName><![CDATA[John Smith Press]]></CompanyName> </CompanyReference> <Channel monsterId="4770"/> <JobInformation> <JobTitle><![CDATA[Printer]]></JobTitle> <Contact> <StructuredName> <GivenName><![CDATA[John]]></GivenName> <FamilyName><![CDATA[Smith]]></FamilyName> </StructuredName> <Address> <StreetAddress>1 Main Street</StreetAddress> <City>Maynard</City> <State>MA</State> <CountryCode>US</CountryCode> <PostalCode>01754</PostalCode> </Address> <Phones> <Phone phoneType="contact">8885551212</Phone> </Phones> <E-mail>news.dev.sample@monster.com</E-mail> </Contact> <JobBody><![CDATA[<font face="StdNewtonABold" size=1 color +="#000000"><dl><dl><dd>Job Body goes here</font></dl></dl>]]></JobBod +y> </JobInformation> <JobPostings> <JobPosting> <Location> <CountryCode>US</CountryCode> <PostalCode>01754</PostalCode> <Continent>NA</Continent> </Location> <JobCategory monsterId="2"/> <JobOccupations> <JobOccupation monsterId="11713"/> </JobOccupations> <BoardName monsterId="1"/> <JobPostingDates> <JobActiveDate>2011-11-28T00:00:00</JobActiveDate> <JobExpireDate>2011-12-28T00:00:00</JobExpireDate> </JobPostingDates> </JobPosting> </JobPostings> </Job> </nis:NISJob> <nis:NISJob> ... Job with today's active date </nis:NISJob> <nis:NISJob> ... Job with today's active date </nis:NISJob> </nis:Jobs>

I can't seem to find the solution, though I feel like it is some simple thing staring me in the face! I seek the wisdom!


In reply to XML::Twig output problem by plockhart

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 making s'mores by the fire in the courtyard of the Monastery: (9)
    As of 2014-12-28 10:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

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





      Results (180 votes), past polls