2

I have a small editor that creates .svg files with diagrams (using svg.js).

The editor stores data about how the file created in its own simple text format, not unlike this one.

I would like to embed the source text directly in the SVG file, so I can load it back without losing any metainformation user specified (like comments and formatting).

What is the proper way to do that? The editor I linked above stores its data in the <source> tag right under <svg>. Is this a good and standards-compliant approach?

<svg 
  xmlns="http://www.w3.org/2000/svg" 
  width="445" height="319" 
  xmlns:xlink="http://www.w3.org/1999/xlink"
>
  <source><![CDATA[Andrew->China: Says Hello
  Note right of China: China thinks\nabout it
  China-->Andrew: How are you?
  Andrew->>China: I am good thanks!]]></source>
  <!-- SVG content here -->
</svg>
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160

1 Answers1

4

Create a custom namespace and store whatever you want under elements in that custom namespace. You can call the custom namespace element source if you want.

Wrapping your custom content in a <metadata> tag is recommended in the SVG specification. Thanks to Alexander Gladysh for reminding me of that.

Robert Longson
  • 118,664
  • 26
  • 252
  • 242
  • Thanks! Is there a standard namespace I can reuse for my needs? – Alexander Gladysh Jun 02 '15 at 05:47
  • Of course not, it's your editor and your format of metadata. For example, Inkscape uses sodipodi because it started as a fork of sodipodi. – Robert Longson Jun 02 '15 at 05:51
  • I thought that, since I store my data as plain text, there could be something reusable (similar to the `` tag editor I linked to in the question uses). But OK, nevermind. – Alexander Gladysh Jun 02 '15 at 05:52
  • The link for the `` tag doesn't work any more. – Bernard May 29 '19 at 07:46
  • @RobertLongson I expected the link to go to some part of the SVG specification, but link goes to a page that says that the metadata chapter that is no longer part of the SVG specification. – Bernard May 29 '19 at 09:50
  • @Bernard And also has a link to where metadata has moved to. I've fixed the link to point directly to the new location. – Robert Longson May 29 '19 at 09:53