2010-03-29 96 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