in reply to Re^2: XML::Twig question
in thread XML::Twig question
Here is what it would look like:
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my( $ac_n_value, $ai_att_cond)= @ARGV; XML::Twig->new( twig_handlers => { qq{AC[\@n="$ac_n_value"]//AI[$ai_at +t_cond]} => \&print_ai_data }) ->parsefile( "test_thandi.xml"); sub print_ai_data { my( $t, $ai)= @_; print "DESC: ", $ai->first_child( 'Desc')->sprint, "\n", "ID : ", $ai->first_child( 'ID') ->sprint, "\n" ; }
You call this with the value you want for the n attribute of the AC element, and the condition for the AI element:
perl test_thandi CCC '@n=AAA' perl test_thandi CCC '@n="AAA" or @set="y"'
A couple of comments on the code: Perl and XPath strings don't really mix very well: you can use alternate quotes (qq{}) to avoid the collision of perl interpolating quotes and of XPath attribute quotes (or you can use ' instead of " in the XPath expression), but you need to backslash the @ used for attribute conditions in XPath, so it is not interpolated as an array by Perl. An alternate method is to use sprintf to build the XPath expression.
This code loads the entire document in memory, which you may or may not want. There are techniques to avoid this described in the XML::Twig Tutorial.
Also, you need the development version of XML::Twig to be able to run this, you can get it from xmltwig.com.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: XML::Twig question
by Bizza (Initiate) on Dec 19, 2006 at 21:50 UTC | |
by mirod (Canon) on Dec 19, 2006 at 22:28 UTC | |
by thandi (Novice) on Dec 20, 2006 at 10:23 UTC | |
by mirod (Canon) on Dec 20, 2006 at 13:35 UTC | |
by thandi (Novice) on Dec 20, 2006 at 14:33 UTC |