SVG的XSL转换将命名空间属性添加到新标记

时间:2010-03-29 14:35:45

标签: xslt svg xml-namespaces

我想通过向边缘和节点添加onclick处理程序来扩展SVG文件。我还想添加一个引用JavaScript的脚本标记。问题是脚本标记获得了一个空的命名空间属性。 我没有找到任何有关我理解的信息。为什么XSLT会添加一个空命名空间?

XSL文件:

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

1 个答案:

答案 0 :(得分:2)

未加前缀的文字结果元素script位于默认命名空间中,在这种情况下,它不是命名空间。在结果文档中,此元素通过xmlns=""显式放置在无名称空间中。

Namespaces in XML 1.0的第6.2节说:

  

默认情况下的属性值   名称空间声明可以为空。   这具有相同的效果   那里的声明范围   没有默认命名空间。

如果您希望在默认命名空间中将其设为svg:script,请将svg命名空间设为样式表的默认命名空间。您仍然需要该命名空间的名称空间前缀。

<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">
相关问题