views:

234

answers:

1

I have a SVG file that I want to extend by adding onclick handlers to edges and nodes. I also want to add a script tag referring to a JavaScript. The problem is that the script tag gets an empty namespace attribute added to it. I haven't found any information regarding this that I understand. Why does XSLT add an empty namespace?

XSL file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"&gt;

<xsl:output method="xml" encoding="utf-8" />

<xsl:template match="/svg:svg">
  <xsl:copy>
    <script type="text/ecmascript" xlink:href="base.js" /> <!-- this tag gets a namespace attr -->
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

<!-- Identity transform http://www.w3.org/TR/xslt#copying -->
<xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

<!-- Check groups and add functions -->
<xsl:template match="svg:g">
  <xsl:copy>
    <xsl:if test="@class = 'node'">
      <xsl:attribute name="onclick">node_clicked()</xsl:attribute>
    </xsl:if>
    <xsl:if test="@class = 'edge'">
      <xsl:attribute name="onclick">edge_clicked()</xsl:attribute>
    </xsl:if>
    <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
+2  A: 

The unprefixed literal result element script is in the default namespace, which in this case is no namespace. In your result document, this element is explicitly placed in no namespace via xmlns="".

Section 6.2 of Namespaces in XML 1.0 says that:

The attribute value in a default namespace declaration MAY be empty. This has the same effect, within the scope of the declaration, of there being no default namespace.

If you want this to be a svg:script in the default namespace, make the svg namespace the default for your stylesheet. You will still need a namespace prefix for that namespace as well.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns="http://www.w3.org/2000/svg"&gt;
Lachlan Roche
Thank you for the solution and the link.
Steve