smunro16 has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys I want to create something in xml twig that outputs data like this.
jobname: A Days:1,2,3 jobname: B Days: 1,3 OR jobname: A days:1,2,3 jobname: B days:1,3
(The data is taken from a mixed xml file that looks a bit like:
<DEFTABLE DATA... <SCHED_GROUP DATA... <JOB DATA...JOBNAME...WEEKDAYS... </JOB> </SCHED_TABLE> </DEFTABLE> At the moment my output is <code> jobnamedaysjobnamedays
my code is
#!/usr/bin/perl -w use warnings; no warnings 'uninitialized'; use strict; use XML::Twig; use XML::LibXML; my $file = 'PYR.xml'; my $twig = new XML::Twig(KeepSpaces => 'true',TwigHandlers => { "JOB " + => \&Output}); open(INFO3, "+>$file.xml"); $twig->parsefile($file); sub Output { my( $twig, $s )= @_; print $s->att('JOBNAME',"\n"); print $s->att('WEEKDAYS',"\n"); print INFO3 $s->att("<JOBNAME>",'JOBNAME',"\n"); print INFO3 $s->att("<WEEKDAYS>",'WEEKDAYS',"\n"); print "Converted XML file into WORD file\n\n"; print $file." WORD docu +ment generated"; close(INFO3);
Can someone tell me how to output the text in the format i want please?

Replies are listed 'Best First'.
Re: XML::Twig Output Help
by Corion (Patriarch) on Aug 04, 2008 at 18:05 UTC

    Most likely, it's because this line:

    print INFO3 $s->att("<JOBNAME>",'JOBNAME',"\n");

    won't print any newline. This line maybe does:

    print INFO3 $s->att("<JOBNAME>"),'JOBNAME',"\n";

    But I'm not familiar with XML::Twig. It strikes me as odd that XML::Twig would use <...> within attributes. Maybe your usage of XML::Twig needs some changes?

Re: XML::Twig Output Help
by Lawliet (Curate) on Aug 04, 2008 at 18:34 UTC

    Déjà vu...

    Why did you post the same question thrice?

    <(^.^-<) <(-^.^<) <(-^.^-)> (>^.^-)> (>-^.^)>
Re: XML::Twig Output Help
by toolic (Bishop) on Aug 05, 2008 at 01:38 UTC
    smunro16,

    Please provide a small snippet of legal XML code. That way, no one will have to guess what the attributes and values are.

      Hi Guys, Sorry here is a snippet of the xml
      <?xml version='1.0' encoding='ISO-8859-1' ?> <!DOCTYPE DEFTABLE SYSTEM "C:\Program Files\BMC Software\CONTROL-M EM +6.2.01\Data\resource\deftable.dtd"> <DEFTABLE > <SCHED_GROUP GROUP="BEPYRG-NA-HKEEP" OWNER="pyruat11" AUTHOR="u038451" CONFIRM="0" JOBNAME="BPYRG000-NA-HKEEP" MAXWAIT="0" MEMNAME="BPYRD007-PYRPACK" DATACENTER="D015-E1200-IB" TABLE_NAME="BEPYRG-NA-HKEEP" ADJUST_COND="0" APPLICATION="BEPYR_NA_PYRPACK" MULTY_AGENT="N" USED_BY_CODE="0" CHANGE_USERID="u147393" CREATION_DATE="20080731" > <OUTCOND NAME="PL-BPYRG000-NA-HKEEP-OK" SIGN="ADD" ODATE="ODAT"/> <AUTOEDIT EXP="%%PYR_INST=eqny" /> <AUTOEDIT EXP="%%ORDER_TABLENAME=BEPYR" /> <AUTOEDIT EXP="%%APPENV=devctm01" /> <TAG APR="1" AUG="1" DEC="1" FEB="1" JAN="1" JUL="1" JUN="1" MAR="1" MAY="1" NOV="1" OCT="1" SEP="1" RETRO="0" SHIFT="IGNOREJOB" MAXWAIT="05" SHIFTNUM="+00" TAG_NAME="CMTWTF__-TAG" WEEKDAYS="1,2,3,4,5" DAYS_AND_OR="OR" /> <TAG APR="1" AUG="1" DEC="1" FEB="1" JAN="1" JUL="1" JUN="1" MAR="1" MAY="1" NOV="1" OCT="1" SEP="1" DAYS="ALL" RETRO="0" SHIFT="IGNOREJOB" MAXWAIT="05" SHIFTNUM="+00" TAG_NAME="CMTWT___S-TAG" WEEKDAYS="1,2,3,4,0" DAYS_AND_OR="AND" /> <JOB APR="1" AUG="1" DEC="1" FEB="1" JAN="1" JUL="1" JUN="1" MAR="1" MAY="1" NOV="1" OCT="1" SEP="1" DAYS="ALL" OWNER="pyruat11" RETRO="0" SHIFT="IGNOREJOB" SYSDB="0" AUTHOR="u038451" CYCLIC="0" NODEID="BEPYR_NA_HKEEP" CMDLINE="Dummy Job" CONFIRM="0" JOBNAME="BPYRD000-PYRPACK-START-HOUSEKEEP" MAXDAYS="0" MAXRUNS="0" MAXWAIT="5" MEMNAME="BPYRD000-PYRPACK-HOUSEKEEP" AUTOARCH="0" CRITICAL="0" INTERVAL="00000M" MAXRERUN="0" PRIORITY="5" SHIFTNUM="+00" TASKTYPE="Dummy" TIMEFROM="0800" WEEKDAYS="1,2,3,4,5" IND_CYCLIC="START" APPLICATION="BEPYR_NA_PYRPACK" DAYS_AND_OR="OR" CHANGE_USERID="u147393" CREATION_DATE="20080731" CREATION_TIME="092311" CREATION_USER="u147393" TAG_RELATIONSHIP="OR" USE_INSTREAM_JCL="0" > <OUTCOND NAME="PL-BPYRD000-PYRPACK-START-HOUSEKEEP-OK" SIGN="ADD" +ODATE="ODAT" /> <TAG_NAMES TAG_NAME="CMTWT___S-TAG"/> </JOB> </SCHED_GROUP> </DEFTABLE>
        Corion was basically right. I tidied up your code, removed the redundant output to a file (for simplicity) so that output just goes to STDOUT, and fixed the print statements:
        use warnings; use strict; use XML::Twig; my $file = 'PYR.xml'; my $twig = new XML::Twig( KeepSpaces => 'true', TwigHandlers => { "JOB " => \&Output} ); $twig->parsefile($file); sub Output { my ($twig, $s) = @_; print 'Jobname = ', $s->att('JOBNAME') , "\n"; print 'Weekdays = ', $s->att('WEEKDAYS'), "\n"; }

        prints to STDOUT:

        Jobname = BPYRD000-PYRPACK-START-HOUSEKEEP Weekdays = 1,2,3,4,5