开发者

XSL transformation of SVG adds namespace attribute to new tag

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">

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

<xsl:template match="/svg:svg">
  <xsl:copy>
    &开发者_如何转开发lt;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>


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">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜