Forum Moderators: coopster

Message Too Old, No Replies

Hyphens in XML attribute Name

         

SeanF

11:15 pm on May 9, 2020 (gmt 0)

5+ Year Member Top Contributors Of The Month



Hi:

I am trying to parse an SVG file in PHP by using SimpleXML. (Basically, I want to parse the SVG code, manipulate some elements to change color and write the new values back to the file.

I am able to read the file successfully and I am able to read the elements and the @attributes, so I think I'm on the right track.

The problem I am having is that some of the attribute elements have a hyphen (-) in their names and my code does not like that. I have tried various combinations of quotes and escaping the character but I am still getting errors.

The original SVG code looks like this:
 <g class="layer">
<title>Layer 1</title>
<image height="1735" id="Background" width="1071" x="0" xlink:href="data:img/png;base645ErkJggg==" y="-2"/>
<rect fill="#d6d6d1" height="43" id="B709" width="43" x="771.5" y="125.5"/>
<text class="cls-1" data-name="P 709 " fill="black" id="P_709_" x="791.559" y="142.076">
<tspan id="svg_11" x="791.559">P</tspan>
<tspan class="cls-2" dy="20.167" id="svg_12" x="791.559">709</tspan>
</text>
<rect fill="#d6d6d2" height="43" id="B710" width="43" x="816.5" y="126.5"/>
<rect fill="#d6d6d3" height="43" id="B700" width="43" x="363.5" y="125.5"/>


I read the file using :
$xml = simplexml_load_file($file) or die("Can't read the AML data");


Checking what I have, I use:
print_r($xml);


A sample of what is returned is:
[rect] => Array ( 
[0] => SimpleXMLElement Object ([@attributes] => Array ( [fill] => #d6d6d6 [height] => 43 [id] => B709 [width] => 43 [x] => 771.5 [y] => 125.5 ) )
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [fill] => #d6d6d6 [height] => 43 [id] => B710 [width] => 43 [x] => 816.5 [y] => 126.5 ) )
[2] => SimpleXMLElement Object ( [@attributes] => Array ( [fill] => #d6d6d6 [height] => 43 [id] => B700 [width] => 43 [x] => 363.5 [y] => 125.5 ) )
[3] => SimpleXMLElement Object ( [@attributes] => Array ( [fill] => #d6d6d6 [height] => 43 [id] => B703 [width] => 43 [x] )
[text] => Array (
[0] => SimpleXMLElement Object ( [@attributes] => Array ( [class] => cls-1 [data-name] => P 709 [fill] => black [id] => P_709_ [x] => 791.559 [y] => 142.076 ) [tspan] => Array ( [0] => P [1] => 709 ) )
[1] => SimpleXMLElement Object ( [@attributes] => Array ( [class] => cls-1 [data-name] => P 700 [fill] => black [id] => P_700_ [x] => 383.971 [y] => 142.494 ) [tspan] => Array ( [0] => P [1] => 700 ) )
[2] => SimpleXMLElement Object ( [@attributes] => Array ( [class] => cls-1 [data-name] => P 703 [fill] => black [id] => P_703_ [x] => 519.945 [y] => 142.084 ) [tspan] => Array ( [0] => P [1] => 703 ) )
)


The problem I am having is the "data-name" attribute.

I am trying to assign it to a variable name using:
$dataname = $text->attributes()->data-name;


But it keeps failing.

Can someone suggest a solution? (or maybe there's an easier way to programmatically manipulate the SVG file?)

phranque

12:36 am on May 10, 2020 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I am trying to assign it to a variable name using:
$dataname = $text->attributes()->data-name;


i'm not a PHP expert but i would try using quoting like this:
$dataname = $text->attributes()->"data-name";

nordberg25

10:19 am on Aug 9, 2020 (gmt 0)

5+ Year Member



Try
$dataname = $text->attributes()->{'data-name'};