Re: Problems getting start_prefix_mapping to fire
mirod <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Kjetil Kjernsmo wrote:
> Hello again!
>
> I need to remove some namespace prefix mappings from my result document.
> Now, the problem is that these prefixes occur in attribute values, this is
> actually RDFa. My source document looks like this:
> http://svn.kjernsmo.net/RDF-RDFa-Template/trunk/t/data/dbpedia-mustang-range.input.xhtml
> and the result document looks like this:
> http://svn.kjernsmo.net/RDF-RDFa-Template/trunk/t/data/dbpedia-mustang-range.expected.xhtml
>
> As you can see, it needs to retain e.g. the dbp:-prefix but drop the sub:-
> prefix, both of which only occur in attribute values.
I am afraid your input and your output are really not enough to figure out what
to do: how do you get the 'http://dbpedia.org/resource/Cessna_Citation_Mustang'
url in the first div, and how do you go from '<rat:variable name="sub:range"/>'
to '2161 km' ? Those seem to be the 2 actual pieces of information in the entire
file! I assume some of it is done based on information in
http://www.kjetil.kjernsmo.net/software/rat/substitutions but that's hard to tell.
That aside, he following code gets you somewhere close to what you want:
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $IN = "dbpedia-mustang-range.input.xhtml";
my $OUT = "dbpedia-mustang-range.output.xhtml";
my $RAT_NS = "http://www.kjetil.kjernsmo.net/software/rat/xmlns";
my $RAT = "rot";
my $t= XML::Twig->new( map_xmlns => { $RAT_NS => $RAT },
twig_handlers => { "$RAT:variable" => \&subs_var,
"$RAT:graph" => \&unwrap,
'div[@datatype]' => sub { remove_att(
@_, 'datatype'); },
},
pretty_print => 'cvs',
)
->parsefile( $IN);
# there might be others
$t->root->del_att( "xmlns:$RAT", "xmlns:sub");
$t->print_to_file( $OUT);
sub subs_var
{ my( $t, $var)= @_;
# the real code _might_ be different :--)
$var->set_text( '2161 km')->unwrap;
}
# I assumed you would get the 'endpoint' attribute from the rat:graph element
# and use it to fill the 'about' for the inner div. That might not be what you
# need to do.
sub unwrap
{ my( $t, $graph)= @_;
$graph->first_child( 'div')->set_att( about => $graph->att( 'endpoint'));
$graph->unwrap;
}
# could be done directly in the call to new by writing
# 'div[@datatype]' => sub { $_->del_att( 'datatype'); },
sub remove_att
{ my( $t, $div, $att)= @_;
$div->del_att( $att);
}
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs