2010-03-29 97 views
0

我有一个SVG文件,我想通过将onclick处理程序添加到边和节点来扩展它。我也想添加一个引用JavaScript的脚本标签。问题是脚本标签获得了一个空的名称空间属性添加到它。 我还没有找到任何有关我了解的信息。为什么XSLT添加一个空的名称空间?SVG的XSL转换将namespace属性添加到新标记

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> 

回答

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

谢谢您的解决方案和链接。 – Steve 2010-03-29 17:20:36