svg - adding use tag

N

no_ti

SVG structure

<g id="someID" gmwmsvg:typ="l" gmwmsvg:pri="1000">
<g id="gwmg4" class="D5F2B3w3j6" gmwmsvg:hi="D5F2B3w3j6 R2v1p1o6r3">
<use xlink:href="#D5F2B3w3j6" x="9916" y="2711"
gmwmsvg:act="JavaScript:SetFeatureID('P' + '10380');"></use>
<use xlink:href="#D5F2B3w3j6" x="6995" y="2834"
gmwmsvg:act="JavaScript:SetFeatureID('P' + '10390');"></use>
<use xlink:href="#D5F2B3w3j6" x="4644" y="3775"
gmwmsvg:act="JavaScript:SetFeatureID('P' + '10379');"></use>
<use xlink:href="#D5F2B3w3j6" x="8604" y="6546"
gmwmsvg:act="JavaScript:SetFeatureID('P' + '10375');"></use>
</g>
</g>

Code: (variable X,Y,sAction appropriately set)

ele=doc.getElementById("someID");
nodes=ele.getElementsByTagName("g");
ch=doc.createElement("use");
ch.setAttribute("xlink:href",nodes.item(0).getAttribute("class"));
ch.setAttribute("x",X);
ch.setAttribute("y",Y);
ch.setAttribute("gmwmsvg:act",sAction);
nodes.item(0).appendChild(ch);

nothing happens - any ideas?
 
M

Martin Honnen

no_ti wrote:

ele=doc.getElementById("someID");
nodes=ele.getElementsByTagName("g");

Does that call find elements? Generally using namespace aware methods is
the proper and safer way to indentify elements in a namespace like the
SVG namespace
var nodes = ele.getElementsByTagNameNS('http://www.w3.org/2000/svg',
'g');

SVG is XML with namespaces so you need to use namespace aware methods to
create an element in the SVG namespace or an attribute in the XLINK
namespace
ch=doc.createElement("use");

ch = doc.createElementNS('http://www.w3.org/2000/svg', 'use');
ch.setAttribute("xlink:href",nodes.item(0).getAttribute("class"));

ch.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
nodes.item(0).getAttribute("class"));
ch.setAttribute("x",X);

ch.setAttributeNS(null, 'x', X);
ch.setAttribute("y",Y);

ch.setAttributeNS(null, 'y', Y);
ch.setAttribute("gmwmsvg:act",sAction);

I don't know the namespace URI for gmwmsvg but you need
ch.setAttributeNS('proper namespace URI', 'gmwmsvg:act', sAction);


See also these notes
<http://jwatt.org/svg/authoring/#namespace-aware-methods>
 
N

no_ti

Martin said:
no_ti wrote:



Does that call find elements? Generally using namespace aware methods is
the proper and safer way to indentify elements in a namespace like the
SVG namespace
var nodes = ele.getElementsByTagNameNS('http://www.w3.org/2000/svg',
'g');

SVG is XML with namespaces so you need to use namespace aware methods to
create an element in the SVG namespace or an attribute in the XLINK
namespace

ch = doc.createElementNS('http://www.w3.org/2000/svg', 'use');


ch.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href',
nodes.item(0).getAttribute("class"));


ch.setAttributeNS(null, 'x', X);


ch.setAttributeNS(null, 'y', Y);


I don't know the namespace URI for gmwmsvg but you need
ch.setAttributeNS('proper namespace URI', 'gmwmsvg:act', sAction);


See also these notes
<http://jwatt.org/svg/authoring/#namespace-aware-methods>

txn :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,261
Members
46,859
Latest member
VallieMcKe

Latest Threads

Top