2011-10-31 8 views
0

<script>ノードをxmlからhtmlにコピーする必要がありますが、いくつかの行をdinamically置き換えて新しいものを追加する必要があります。したがって、ノード全体がコピーされている間に、文字列を検索して置換する必要があります。XMLからHTML(XSLT):いくつかの行を置き換えて新しいノードを追加する

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 a = 2; 
     var b = "abc"; 
     var c = new Array(0,0,0); 
     alert("Input!"); 
    ]]></script> 

    <!-- here svg drawing tags --> 

</svg> 

HTML出力例:

<html> 
    <head> 
     <title>Example!</title> 

     <script type="application/javascript"> 
      var a = 2;       <!---same as xml---> 
      var b = "def";      <!---modified---> 
      var c = new Array(0,0,0);   <!---same as xml---> 
      alert("This is a new example!"); <!---modified---> 
      var new = "new var";    <!---new code--->    
     </script> 
    </head> 
    <body> 
    </body> 
</html> 

それははるかに簡単だ場合、私はXTLS 2.0を使用することができます。 どうすればいいですか?

+0

これはクライアントまたはサーバーで行われていますか?サーバー上で実行されている場合は、標準のXSLTではなく、拡張をコーディングする方がはるかに簡単です。 –

+0

残念ながらクライアント側で完了しました – MrMoog

答えて

1

この変換

<xsl:stylesheet version="2.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
     xmlns:svg="http://www.w3.org/2000/svg" 
     xmlns:my="my:my"> 
     <xsl:output omit-xml-declaration="yes" indent="yes"/> 

     <xsl:param name="pSrcEdits"> 
     <line num="2" act="del"/> 
     <line num="4" act="rep" 
       newLine="var c = new Array(1,1,1);"/> 
     <line num="5" act="ins" 
       newLine="/* Inserted comment */"/> 
     </xsl:param> 

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

<xsl:template match="svg:script/text()"> 
    <xsl:variable name="vLines" select="tokenize(., '&#xD;?&#xA;')"/> 

    <xsl:sequence select="my:editLines($vLines, $pSrcEdits/*)"/> 
</xsl:template> 

<xsl:function name="my:editLines" as="xs:string*"> 
    <xsl:param name="pLines" as="xs:string*"/> 
    <xsl:param name="pCommands" as="element()*"/> 

    <xsl:for-each select="$pLines"> 
    <xsl:variable name="vLineNum" as="xs:integer" 
    select="position()"/> 
    <xsl:variable name="vCommand" 
    select="$pCommands[number(@num) eq $vLineNum]"/> 
    <xsl:sequence select="my:editSingle(., $vCommand)"/> 
    </xsl:for-each> 
</xsl:function> 

<xsl:function name="my:editSingle" as="xs:string?"> 
    <xsl:param name="pLine" as="xs:string"/> 
    <xsl:param name="pCommand" as="element()?"/> 

    <xsl:sequence select= 
    "if(not($pCommand)) 
    then concat($pLine, '&#xA;') 
    else 
     if($pCommand/@act eq 'del') 
     then() 
     else 
      if($pCommand/@act eq 'rep') 
      then concat($pCommand/@newLine, '&#xA;') 
      else (: 'ins' :) 
       concat($pCommand/@newLine, '&#xA;', $pLine, '&#xA;') 
    "/> 

</xsl:function> 
</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 a = 2; 
     var b = "abc"; 
     var c = new Array(0,0,0); 
     alert("Input!"); 
    ]]></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 type="application/javascript"> 
     var b = "abc"; 
var c = new Array(1,1,1); 
/* Inserted comment */ 
     alert("Input!"); 

</script> 

    <!-- here svg drawing tags --> 

</svg> 
+0

ありがとうございます!素晴らしい! :D – MrMoog

+0

@MrMoog:どうぞよろしくお願いいたします。 –

関連する問題