use XML::Parser; use strict; use constant SVGNS => 'http://www.w3.org/2000/svg'; use constant XLINKNS => 'http://www.w3.org/1999/xlink'; sub xmlEscape { $_ = shift; s/&/&/g; s//>/g; return $_; } sub xmlSerialize { my ($expat,$element,@attrs) = @_; $element = "svg:$element" if $expat->namespace($element) eq SVGNS; while (@attrs) { my ($name, $value) = (shift(@attrs), xmlEscape(shift(@attrs))); $name = "xlink:$name" if $expat->namespace($name) eq XLINKNS; $value =~ s/"/"/g; $element .= " $name=\"$value\""; } return $element; } sub svgprefix { my $input = shift; my $output = ''; my $parser = new XML::Parser(Namespaces => 1); $parser->setHandlers( Char => sub {shift; $output .= xmlEscape(shift)}, Start => sub {$output .= '<' . xmlSerialize(@_) . '>'}, End => sub {$output .= ''}, ); eval {$parser->parse($input)} or return $input; $output =~ s/ sphere, cube, tetrahedron
Three Solids EOX