0

我有一个 SVG 文件,我想通过向边缘和节点添加 onclick 处理程序来扩展它。我还想添加一个引用 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>
4

1 回答 1

2

无前缀的文字结果元素script位于默认命名空间中,在本例中为无命名空间。在您的结果文档中,此元素通过xmlns="".

XML 1.0中的命名空间第 6.2 节说:

默认命名空间声明中的属性值可以为空。在声明的范围内,这与没有默认命名空间的效果相同。

如果您希望它成为svg:script默认命名空间中的 a,请将 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">
于 2010-03-29T14:43:49.883 回答