SohamC
SohamC

Reputation: 2437

Change value of an XML object

I am new to perl and cannot quite figure out how to solve this problem.

I have a variable which contains an SVG image in the form of an XML. I need to change the value of the tspan tag in the SVG.

The code is as follows.

    my $tempSvgImage = 
qw~<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="3200" height="3200" xml:space="preserve">
    <desc>Created with Fabric.js 1.4.0</desc>
    <defs></defs>
    <g transform="translate(482.36 1400) scale(8 8)">
        <text font-family="Arial, Helvetica, sans-serif" font-size="50" font-style="normal" font-weight="normal" text-decoration="none" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #C9FA51; opacity: 1;" transform="translate(-40.3 49)">
            <tspan x="0" y="-32.5" fill="#C9FA51">SampleCode</tspan>
        </text>
    </g>
</svg>~

In the above code I need to change the value of tspan tag from Sample Code to New value. How am I supposed to do it?

Upvotes: 0

Views: 130

Answers (1)

7stud
7stud

Reputation: 48619

From the XML::Simple docs:

The use of this module in new code is discouraged.

And in 2005, the author of a tutorial on XML::LibXML at perlmonks posted:

Also, XML::XPath is buggy and no-longer maintained so I don't recommend that.

Here it is with XML::LibXML:

use strict;
use warnings;
use 5.016;
use XML::LibXML;


my $dom = XML::LibXML->load_xml(string => <<'END_OF_XML');
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="3200" height="3200" xml:space="preserve">
    <desc>Created with Fabric.js 1.4.0</desc>
    <defs></defs>
    <g transform="translate(482.36 1400) scale(8 8)">
        <text font-family="Arial, Helvetica, sans-serif" font-size="50" font-style="normal" font-weight="normal" text-decoration="none" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #C9FA51; opacity: 1;" transform="translate(-40.3 49)">
            <tspan x="0" y="-32.5" fill="#C9FA51">SampleCode</tspan>
        </text>
    </g>
</svg>
END_OF_XML

#Deal with the namespace declared in the svg tag(explanation below):
my $xpc = XML::LibXML::XPathContext->new($dom); 
$xpc->registerNs('ns', 'http://www.w3.org/2000/svg');
my ($tspan) = $xpc->findnodes('//ns:tspan');

#Change the tspan's text:
$tspan->removeChildNodes();
$tspan->appendTextNode("New data");

#Prove that the tspan's text changed:
say $tspan; 
say $dom->toString;

All elements inside the svg tag have names of the form:

http://www.w3.org/2000/svg:tspan
|<----- namespace ------>|:tag name

...because of this directive:

<svg xmlns="http://www.w3.org/2000/svg"

That says that every tag name inside the <svg> tag is prefixed by the namespace name:

 http://www.w3.org/2000/svg

So the tspan's name is actually:

<http://www.w3.org/2000/svg:tspan ...>

Because names like that are too unwieldy to work with, you can alias the url:

$xpc->registerNs('ns', 'http://www.w3.org/2000/svg');

Then the name of the tspan tag in the perl code becomes:

ns:tspan

However, the XML::LibXML parser is intolerably slow--even for that short XML document(Edit: the DOCTYPE along with a slow connection is why it took my program ~5 seconds to complete. Removing the DOCTYPE solved that problem). So, here it is with XML::Twig:

use strict;
use warnings;
use 5.016;
use XML::Twig;

XML::Twig->new( 
    map_xmlns => {'http://www.w3.org/2000/svg' => "svg"},

    twig_handlers => { 
        'svg:text/svg:tspan' => sub { $_->set_text('New Stuff') } 
    },

    keep_original_prefix => 1,
    pretty_print => 'indented', 
)
->parse(<<'END_OF_XML')
<?xml version="1.0" encoding="UTF-8"?>
<my-root>
  <text>
    <tspan>Sample Code1</tspan>
  </text>

  <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="200px">
      <desc>Created with Fabric.js 1.4.0</desc>
      <defs></defs>
      <g transform="translate(482.36 1400) scale(8 8)">
        <text font-family="Arial, Helvetica, sans-serif" font-size="50" font-style="normal" font-weight="normal" text-decoration="none" style="stroke: none; stroke-width: 1; stroke-dasharray: ; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 10; fill: #C9FA51; opacity: 1;" transform="translate(-40.3 49)">
          <tspan x="0" y="-32.5" fill="#C9FA51">SampleCode2</tspan>
        </text>
      </g>
  </svg>
</my-root>
END_OF_XML
->print;

Upvotes: 1

Related Questions