Using XML::Twig
am 20.12.2005 01:59:43 von Rich
I'm a little confused using the Perl XML::Twig module. What I'm trying
to do is output a subset of the matched twig_root entries in an XML
file. Essentially I call a method for the 'definition' twig_root and
then I only want to output some of the definitions based on a
condition. Of course I also want the root of the XML document to be
output. It isn't. What am I doing wrong? I expect the tag and
the selected and nothing else. I'm using the root
tag.
XML
===
xmlns="http://oval.mitre.org/XMLSchema/oval"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:redhat="http://oval.mitre.org/XMLSchema/oval#redhat"
xmlns:windows="http://oval.mitre.org/XMLSchema/oval#windows"
xmlns:solaris="http://oval.mitre.org/XMLSchema/oval#solaris" >
4.2
20051212102623
Red Hat Linux 9
Mutt
Microsoft Windows 2000
Microsoft Windows Server
2003
Microsoft Exchange Server 2003
This is some crap
Perl code to extract a subset of the definitions
==================================
use XML::Twig;
my $doc=new XML::Twig
(
twig_roots =>
{
"definition" => \&parseEntry
}
);
sub parseEntry
{
my ($twig,$element)=@_;
my $id=$element->att("id");
if ($id eq "OVAL3")
{
$element->flush();
return 1;
}
$twig->purge();
return 0;
}
$doc->parsefile("test.xml");
$doc->flush();
The output
========
Microsoft Windows 2000
Microsoft Windows Server
2003
Microsoft Exchange Server 2003
Re: Using XML::Twig
am 20.12.2005 14:43:55 von metaperl
I emailed the author of XML::Twig and he should help you shortly. But,
he visits perlmonks.org regularly and you can email him via his
contact info at http://www.xmltwig.com
Re: Using XML::Twig
am 21.12.2005 03:46:56 von Rich
Thanks. Got the reply at www.perlmonks.org and it worked great!
Re: Using XML::Twig
am 21.12.2005 03:57:55 von John Bokma
"Rich" wrote:
> Thanks. Got the reply at www.perlmonks.org and it worked great!
[a] quote
[b] it's nice to post a link to the solution
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
Re: Using XML::Twig
am 21.12.2005 07:42:29 von merlyn
>>>>> "Rich" == Rich writes:
Rich> Thanks. Got the reply at www.perlmonks.org and it worked great!
Which means you're evil, for having posted it multiple places without
disclosure.
Bad on you.
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
Re: Using XML::Twig
am 21.12.2005 14:58:49 von Rich
Here's the solution I got from mirod on www.perlmonks.org from...
in reply to XML::Twig question
I think the $twig->purge is a little too strong for what you are doing.
It seems to interfere with the way the twig is stored. Using
$element->delete instead when the element is not one you want to output
would work better.
I will investigate some more to understand better what's going on, but
in the mean time a handler like this seems to work:
sub parseEntry
{ my ($twig,$element)=@_;
my $id=$element->att("id");
if ($id eq "OVAL3")
{ $twig->flush();
return 1;
}
else
{ $element->delete; }
return 0;
}
Re: Using XML::Twig
am 21.12.2005 15:15:11 von Gunnar Hjalmarsson
Randal L. Schwartz wrote:
> Rich writes:
>> Thanks. Got the reply at www.perlmonks.org and it worked great!
>
> Which means you're evil, for having posted it multiple places without
> disclosure.
>
> Bad on you.
That remark was uncalled for, since the OP was advised to try Perl
Monks.
http://groups.google.com/group/comp.lang.perl.modules/msg/d1 355978ed399b68
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Re: Using XML::Twig
am 21.12.2005 19:39:05 von Darin McBride
Rich wrote:
> Here's the solution I got from mirod on www.perlmonks.org from...
Please ignore Randal. He can be a bit harsh. In less harsh language, I
think this is what you can take from this:
a) when cross-posting between newsgroups and perlmonks (or most web forums),
it is considered common courtesy (well, I'm not sure how common it is - but
it's still courtesy) to alert people as to where your original question
is/was. Since you seem to be using google already for newsgroup access,
you could easily have posted your original question as a URL on perlmonks
(in addition to copying it there):
Monks,
In [http://...|this newsgroup article], I asked the following question:
...
(fill in the "..." appropriately) This would have many side effects, the
least of which would be keeping Randal happy. More importantly, it would
show people the effort you're going through, and alert people who read both
to know that they only need to respond to one because you're the same
person in both places. It also tells people about other venues for posting
questions if they weren't aware of this newsgroup - it's kinda like subtle
advertising. ;-)
b) Having cross-posted, you should return to your other questions to post a
URL to the answer. Rather than reposting mirod's response, just post the
URL:
I posted this question to perlmonks here:
http://www.perlmonks.org/index.pl?node_id=518054
and mirod responded with this answer which is working perfectly:
http://www.perlmonks.org/index.pl?node_id=518089
Thanks, all!
This has many similar side effects: it allows people who do a google search
that come across your question to see the answer, and then they can search
perlmonks for other answers as well, and it provides some subtle
advertising for the site where you actually got your response. In this
case, with the link to mirod's answer is a link to mirod's user information
on perlmonks where people with XML::Twig questions would find further links
for further information. You can easily spend days following these links
for interesting stuff (trust me ;-}) - and through that simple update
message as I typed above, you will enable many people to learn much more
about this topic, and waste millions of dollars of employer's time. ;-)
I see nothing wrong with cross-posting. A little courtesy, however, is
appreciated. (In both directions - harshness is a discourteous response to
such a minor infraction.)
Re: Using XML::Twig
am 21.12.2005 22:29:07 von John Bokma
Darin McBride wrote:
> Rich wrote:
>
>> Here's the solution I got from mirod on www.perlmonks.org from...
>
> Please ignore Randal.
I'd rather not :-D
> He can be a bit harsh.
Odd, never noticed :-D.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)
Re: Using XML::Twig
am 22.12.2005 01:43:57 von Richard Koop
I'm almost worried to post another question... I'll get my lawyer to
check over it before I post!
Re: Using XML::Twig
am 22.12.2005 04:01:25 von John Bokma
"Richard Koop" wrote:
> I'm almost worried to post another question... I'll get my lawyer to
> check over it before I post!
Nah, just look how others post in this group, or comp.lang.perl.misc. For
one thing, make sure you quote (yes it can be done with Google, see:
http://groups.google.com/support/bin/answer.py?answer=14213
)
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
I ploink googlegroups.com :-)