SVG:Namespace: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(→‎declare namespaces for svg and xlink: mention SVG doctype as well)
Line 1: Line 1:
= Declare namespaces for SVG and XLink=
= Declare namespaces for SVG and XLink=


the namespace declaration for svg is required if you don't specify it, nothing will be rendered. the xlink namespace is required if you use <use/>, <image/>, <a/> ...
The namespace declaration for SVG is required. If you don't specify it, nothing will be rendered. The XLink namespace is required if you use <use/>, <image/>, <a/> ...


*the svg namespace: http://www.w3.org/2000/svg
*The SVG namespace: http://www.w3.org/2000/svg
*the xlink namespace: http://www.w3.org/1999/xlink
*The XLink namespace: http://www.w3.org/1999/xlink


a simple SVG file may look like this:
A simple SVG file may look like this:


<?xml version="1.0" standalone="no"?>
<?xml version="1.0" standalone="no"?>
  <svg xmlns="http://www.w3.org/2000/svg"  
  <svg xmlns="http://www.w3.org/2000/svg"  
     xmlns:xlink="http://www.w3.org/1999/xlink">
     xmlns:xlink="http://www.w3.org/1999/xlink">
Line 19: Line 19:
Similarly, declaring a correct DOCTYPE for .svg files is important as well, instances have been observed where an SVG file without namespace and incorrect doctype did work in Adobe SVG, but with the namespace added it didn’t work, until the DOCTYPE was corrected as well. The doctype is supposed to be:
Similarly, declaring a correct DOCTYPE for .svg files is important as well, instances have been observed where an SVG file without namespace and incorrect doctype did work in Adobe SVG, but with the namespace added it didn’t work, until the DOCTYPE was corrected as well. The doctype is supposed to be:


<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">


This should solve the cases where the addition of the namespaces required by Mozilla breaks rendering with Adobe SVG.
This should solve the cases where the addition of the namespaces required by Mozilla breaks rendering with Adobe SVG.

Revision as of 15:40, 14 June 2005

Declare namespaces for SVG and XLink

The namespace declaration for SVG is required. If you don't specify it, nothing will be rendered. The XLink namespace is required if you use <use/>, <image/>, <a/> ...

A simple SVG file may look like this:

<?xml version="1.0" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink">
 <title>title of drawing</title>

</svg>

Declare DOCTYPE for SVG

Similarly, declaring a correct DOCTYPE for .svg files is important as well, instances have been observed where an SVG file without namespace and incorrect doctype did work in Adobe SVG, but with the namespace added it didn’t work, until the DOCTYPE was corrected as well. The doctype is supposed to be:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

This should solve the cases where the addition of the namespaces required by Mozilla breaks rendering with Adobe SVG.

use namespace-aware methods in javascript

when using js, make sure you use the namespace aware methods like:

  • getElementsByTagNameNS()
  • createElementNS()
  • setAttributeNS()
  • getAttributeNS()
  • ...

svg elements are in the svg namespace. svg attributes are in the null namespace. for example, to dynamically create an <image> element, do it like this:

const svgns="http://www.w3.org/2000/svg";
const xlinkns="http://www.w3.org/1999/xlink";
var newIM=document.createElementNS(svgns,"image");
newIM.setAttributeNS(null,"width",100);
newIM.setAttributeNS(null,"height",100);
newIM.setAttributeNS(xlinkns,"href","bild2.png");