【发布时间】:2017-05-07 13:28:51
【问题描述】:
simpleXML 仍然让我感到困惑:
我很难理解如何设置插入组的位置...请参阅我的代码中的注释,现在我在错误的位置创建了一个副本(g#result1 应该在 g#results 下。
如何在这个新创建的组下进行更改? (如#twdTextLine0)
如何从 #twdBoxStar 获取 x,y,width,height 并用相同的 x,y,width,height 和 xlink:href="#Star" 替换它的位置?
最后我需要更改:svg 尺寸、viewbox 和样式块,codewise 最短的方法是什么?
有没有 simpleXML 的替代品?也许我可以在一条线上进入并设置在一条线上?我认为有人必须编写这样的课程。
<?php
$svg_data = <<<'SVG'
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 20.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="460px" height="200px" viewBox="0 0 460 200" style="enable-background:new 0 0 460 200;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;stroke:#00FFFF;stroke-width:0.7087;}
.st1{fill:#FF0000;}
.st2{font-family:ISOCPEUR, ISOCPEUR;}
.st3{font-size:37.3524px;}
.st4{fill:#ED1C24;}
.st5{font-family:Ebrima-Bold, Ebrima;font-weight: 700;}
.st6{font-size:68.0315px;}
.st7{fill:none;stroke:#000000;stroke-width:0.7087;stroke-miterlimit:10;}
.st8{font-size:11.0845px;}
.st9{font-size:27.6748px;}
.st10{font-size:18.9881px;}
.st11{font-size:12px;}
.st12{font-size:5.6693px;}
.st13{font-size:9.8585px;}
.st14{fill:#FF00FF;}
.st15{fill:#FFFFFF;}
.st16{font-family:ISOCPEURItalic, ISOCPEUR;font-style: italic;}
.st17{font-size:12.3452px;}
.st18{font-size:6.1644px;}
</style>
<symbol id="Star" viewBox="0 0 100 100">
<path id="Star" style="fill:#00FF00;" d="M50,3.58L62.5,39H100L70.83,60.95l10.42,34.93L50,74.81L18.75,95.53l10.42-34.51L0,39 h37.5L50,3.58z"/>
</symbol>
<g id="TemplateForm">
<g id="twe">
<rect id="tweCanvas" x="7.3" y="81.32" class="st0" width="263.03" height="72"/>
<text id="tweWeekdayShorttext" transform="matrix(1 0 0 1 247.1745 127.5469)" class="st1 st2 st3">V</text>
<line id="tweDateNumAliignerRight" class="st0" x1="132.02" y1="89.98" x2="132.02" y2="144.66"/>
<g id="tweDateNumber">
<text transform="matrix(1 0 0 1 132.2695 140.8158)" class="st4 st5 st6">24</text>
</g>
<line id="tweDivider" class="st7" x1="126.49" y1="81.32" x2="126.49" y2="153.31"/>
<g id="tweText">
<text id="tweTextLine3" transform="matrix(1 0 0 1 15.6379 100.736)" class="st2 st8">1234567890abcdef12345</text>
<text id="tweTextLine2" transform="matrix(1 0 0 1 15.6379 114.096)" class="st2 st8">1234567890abcdef12345</text>
<text id="tweTextLine1" transform="matrix(1 0 0 1 15.6379 127.4562)" class="st2 st8">1234567890abcdef12345</text>
<text id="tweTextLine0" transform="matrix(1 0 0 1 15.6379 140.8162)" class="st2 st8">1234567890abcdef12345</text>
</g>
<rect id="tweBoxMoon" x="216.21" y="121.63" class="st0" width="21" height="21"/>
<rect id="tweBoxStar" x="216.21" y="92.24" class="st0" width="21" height="21"/>
<rect id="tweBoxText" x="12.57" y="89.98" class="st0" width="107.92" height="54.68"/>
</g>
<g id="twd">
<rect id="twdCanvas" x="7.3" y="9.32" class="st0" width="263.03" height="72"/>
<text id="twdWeekdayshorttext" transform="matrix(1 0 0 1 249.5817 41.9174)"><tspan x="0" y="0" class="st2 st3">C</tspan><tspan x="-1.2" y="29.13" class="st2 st3">S</tspan></text>
<text id="twdDatenumber" transform="matrix(1 0 0 1 41.5544 69.5698)" class="st5 st6">26</text>
<line id="twdDivider" class="st7" x1="126.49" y1="9.32" x2="126.49" y2="81.32"/>
<g id="twdText">
<text id="twdTextLine3" transform="matrix(1 0 0 1 135.0475 28.1718)" class="st2 st8">1234567890abcdef12345</text>
<text id="twdTextLine2" transform="matrix(1 0 0 1 135.0475 41.5319)" class="st2 st8">1234567890abcdef12345</text>
<text id="twdTextLine1" transform="matrix(1 0 0 1 135.0475 54.892)" class="st2 st8">1234567890abcdef12345</text>
<text id="twdTextLine0" transform="matrix(1 0 0 1 135.0475 68.252)" class="st2 st8">1234567890abcdef12345</text>
</g>
<line id="twdDateNumAliignerLeft" class="st0" x1="120.02" y1="17.39" x2="120.02" y2="72.07"/>
<rect id="twdBoxStar" x="15.68" y="19.75" class="st0" width="21" height="21"/>
<rect id="twdBoxText" x="132.49" y="17.39" class="st0" width="107.92" height="54.68"/>
<rect id="twdBoxMoon" x="15.68" y="49.15" class="st0" width="21" height="21"/>
</g>
<g id="tcl">
<rect id="tclCanvas" x="7.3" y="153.31" class="st0" width="263.03" height="36"/>
<line id="tclDivider" class="st7" x1="126.49" y1="153.31" x2="126.49" y2="189.31"/>
<text id="tclYear" transform="matrix(1 0 0 1 59.1393 183.5269)" class="st2 st9">YYYY</text>
<text id="tclMonthname" transform="matrix(1 0 0 1 130.9372 182.7798)" class="st2 st10">monthname</text>
</g>
</g>
<g id="results">
<!-- I want to have a copy the whole g#TeplateForm/g#twd with id, attribs and values changed to here in a loop -->
</g>
</svg>
SVG;
$svg = new SimpleXMLElement($svg_data);
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');
$svg->registerXPathNamespace('xlink', 'http://www.w3.org/1999/xlink');
$tType='twd';
$gTemplate = $svg->xpath('svg:g[@id="TemplateForm"]/svg:g[@id="'.$tType.'"]');
if (!isset($gTemplate[0])) {
die("#TemplateForm/#twd group not found\n");
}
$gResult = clone $gTemplate[0];
$gResult['id'] = 'result1';
$x=40;
$y=40;
$gResult['transform'] = 'matrix(1 0 0 1 '.$x.' '.$y.')';
$dom_res = dom_import_simplexml($gResult);
$dom_svg = dom_import_simplexml($svg);
$dom_svg->appendChild($dom_res);
echo $svg->asXML();
?>
【问题讨论】: