change svg:g id to inkscape:label

6k views Asked by At

I made a vector graphic in Inkscape, including layers and sub-layers for further use in Processing. I named all the layers in the UI, and realized that the final SVG only creates an inkscape:label attribute with that name, but id remains generic:

<svg:g id="layer1" inkscape:label="My custom label">

I know I can manually edit the labels in the XML editor, but is there a setting somewhere to automatically use the layer name as id?

3

There are 3 answers

0
Stonecrusher On

I don't know about a setting to automatically use the layer name as id. But why not do it the other way round: if you remove the inkscape:label attribute, then the layer name automatically becomes the id of the svg:g in the inkscape UI. The attribute inkscape:groupmode=layer is enough to make the svg:g a layer element.

3
TerrenceSun On

I have the same requirement when I am creating a svg for Fritzing, because fritzing doesn't refer to the inkscape:label. In such circumstance, I can make sure that label holds the legit value for id. So I make a script to do saving myself out of the dirty and heavy job.

Please note that the script ONLY READ 'Plain SVG' format. https://gist.github.com/TerrenceSun/972ef4eea97f331af1e6abfcafb7c6e5

0
Kiwi On

I recently came across this question, as I was looking for the same topic. As it turned out, Inkscape (v0.92) has functions for that purpose now.

You can set IDs, and Labels in the Inkscape GUI in Object Properties menu, and they will be applied to the XML code then.

Example

Inkscape GUI

  • Draw a yellow rectangle and select it
  • Click on Object -> Object Properties...
  • In the menu set ID to yellow_rect and Label to #yellow_rect
  • Apply changes by a click on Set
  • To complete this example, repeat the steps above to create red_rect, set Label and ID
  • Eventually, group both rectanbles and set identifiers for the group as well.

enter image description here

XML Code

When I open the SVG file, Inkscape put my identifiers to the appropriate XML tags.

    <g
       id="rect_group">
      <rect
         rx="0.11797347"
         y="250.69791"
         x="5.0270834"
         height="18.785416"
         width="30.427082"
         id="yellow_rect1"
         style="fill:#f4ff00;fill-opacity:1;stroke:#000000;stroke-width:0.52916667;stroke-linejoin:round;stroke-miterlimit:3.79999995;stroke-dasharray:none;stroke-opacity:1" />
      <rect
         rx="0.11797347"
         y="258.89999"
         x="24.606249"
         height="16.933332"
         width="33.602081"
         id="red_rect1"
         style="fill:#f40000;fill-opacity:1;stroke:#000000;stroke-width:0.52916664;stroke-linejoin:round;stroke-miterlimit:3.79999995;stroke-dasharray:none;stroke-opacity:1" />
    </g>