2011-10-29 94 views
3

我需要通過XSLT將SVG文件轉換爲HTML5畫布。 問題是關於Javascript。 在SVG文件我有一個腳本標籤有一些代碼,就像在這個簡單的例子:XSLT:如何將Javascript代碼從xml複製到html,修改它的某些行

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="transform.xsl"?> 
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" 
id="slot" width="320" height="245"> 

    <script type="application/javascript"><![CDATA[ 
     var test = 2; 
     alert("This is a test!"); 
    ]]></script> 

    <----here svg drawing tags---> 

</svg> 

我要輸出到html在一些地方改進Javascript代碼,並添加一些新的代碼,如下所示:

<html> 
    <head> 
    <title> SVG to HTML5!</title> 

    <script type="application/javascript"> 
     var new_test;     <---the new code---> 

     var test = 4;     <---this is the modified code from 
     alert("This is a new test!");     svg file---> 
    </script> 
    </head> 
    <body> 
    </body> 
</html> 

我該怎麼做?

+0

您的示例看起來不太清楚。你想要執行什麼樣的JavaScript代碼修改? – liori

+0

我需要將代碼行更改爲其他...例如: document.getElementById(「x」)。firstChild.nodeValue = x;到 document.getElementById(「x」)。setAttribute('value',x); 因此,如果有辦法解決代碼行並將其改爲我想要的,我就解決了這個問題! – MrMoog

+0

我對XSLT的瞭解比先進的知識少一點,但我認爲這種文本編輯在XSLT中是非常困難的,如果不是不可能的話。當然,我可能是錯的。 – liori

回答

1

雖然可以編寫將執行單獨的管線的更換/插入/缺失,一個更容易的辦法是更換整個script元素(有點複雜)代碼:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:svg="http://www.w3.org/2000/svg" 
xmlns="http://www.w3.org/2000/svg"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:param name="pNewScript"> 
    <script type="application/javascript"> 
     var new_test;     

     var test = 4;     
     alert("This is a new test!");     
    </script> 
</xsl:param> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="svg:script"> 
    <xsl:copy-of select="$pNewScript"/> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉型施加在提供的XML文檔(校正爲良好的形成):

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" 
id="slot" width="320" height="245"> 

    <script type="application/javascript"><![CDATA[ 
     var test = 2; 
     alert("This is a test!"); 
    ]]></script> 

    <!-- here svg drawing tags --> 

</svg> 

有用,正確的結果產生

<svg xmlns="http://www.w3.org/2000/svg" 
xmlns:xlink="http://www.w3.org/1999/xlink" 
id="slot" width="320" height="245"> 
    <script xmlns:svg="http://www.w3.org/2000/svg" type="application/javascript"> 
     var new_test;     

     var test = 4;     
     alert("This is a new test!");     
    </script><!-- here svg drawing tags --> 
</svg> 
+0

謝謝,順便說一句,如果我能夠以一種不太複雜的方式,我會代替dinamically的代碼..你能解釋我怎樣才能替代一條線? – MrMoog

+0

我認爲這是您可以提交的新問題的主題。恭喜您成功縮小了您的初始問題的主題和精確度。今後,請儘量做到儘可能精確和具體。一定要提供完整和合格的例子,不需要工作人員糾正/猜測。我想(再次)你沒有XSLT 2.0可用。使用XSLT 2.0這種行號操作非常簡單。使用XSLT 1.0,您需要編寫遞歸調用的命名模板以導航到想要的行號行。 –

+0

哦,是的,我可以使用XSLT 2.0,如果它可以幫助我輕鬆!我提出了另一個問題! http://stackoverflow.com/questions/7952425/xml-to-html-xslt-copy-a-node-replacing-some-lines-and-adding-new-ones – MrMoog

相關問題