0

XSLTを使用して別のXMLファイルに変換しているXML(XHTML)ファイルがあります。すべてのコンテンツを正常に変換できますが、含めることができない1つのJavascriptを出力ファイルに含める必要があります。XSLTを使用して出力ファイルにJavaScriptを含めるにはどうすればよいか教えてください。

私の入力ファイル:

<?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
    </head>  
    <body>
     <div class="TF" id="id8">
      <div class="iDev">
        <div class="q">
              T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/> 
            </div>
      </div>
     </div>
    </body>
    </html>

必要な出力

<?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
        </head>  
        <body>
         <div class="QT" id="id10">
         <script type="text/javascript">
         <!-- //<![CDATA[
            var numQuestions = 4;
            var rawScore = 0;
            var actualScore = 0;
           function getAnswer()
           {
          }
            function calulate()
           {
          }
          //]]> -->
         </script>
          <div class="iDev">
            <div class="q">
              T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/> 
            </div>
          </div>
         </div>
        </body>
       </html>

XSLTパート:

<xsl:template match="xhtml:div[@id='id8']/@*">
  <xsl:attribute name="class">QT</xsl:attribute>
   <xsl:attribute name="id">id10</xsl:attribute>
   <script type="text/javascript">
   <![CDATA[
            var numQuestions = 4;
            var rawScore = 0;
            var actualScore = 0;
           function getAnswer()
           {
          }
            function calulate()
           {
          }
          ]]>
          </script>
</xsl:template>

<div class="TF" id="id8">IDテンプレートを作成し、属性の値を変更しました。目的の出力に応じて必要な場合はいつでも、このタグの後にjavascriptを含める方法がわかりません。ありがとう!

4

3 に答える 3

1

次のxslを試してください。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="div[@id='id8']">
    <xsl:element name="div">
      <xsl:attribute name="class">QT</xsl:attribute>
      <xsl:attribute name="id">id10</xsl:attribute>
    </xsl:element>
    <script type="text/javascript">
      <![CDATA[         
       var numQuestions = 4;             
       var rawScore = 0;             
       var actualScore = 0;            
       function getAnswer()            
       {           
       }             
       function calulate()            
       {           
       }           
       ]]>
    </script>
  </xsl:template>
</xsl:stylesheet>   

編集済み

入力xmlファイルは次のとおりです。

    <?xml version="1.0" encoding="utf-8"?>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
  </head>
  <body>
    <div id="id8">
    </div>
      <div class="iDev">
        <div class="q">
          T <input type="radio" name="o0" id="t0" onclick="getFeedback()"/>
        </div>
      </div>
  </body>
</html>  

出力xmlは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;  charset=utf-8" />
</head>
<body>
<div class="QT" id="id10" /><script type="text/javascript">

       var numQuestions = 4;             
       var rawScore = 0;             
       var actualScore = 0;            
       function getAnswer()            
       {           
       }             
       function calulate()            
       {           
       }           

    </script>
  <div class="iDev">
    <div class="q">
      T <input type="radio" name="o0" id="t0" onclick="getFeedback()" />
    </div>
  </div>
</body>
</html>  

これがお役に立てば幸いです...

于 2012-08-06T07:09:11.763 に答える
1

コメントを忘れて、JavaScriptコードをCDATAセクションに配置するだけです。<script>最近では、裸のスクリプトがノードにあるという問題を抱えているブラウザはほとんどありません。

于 2012-08-06T03:53:00.377 に答える
0

私はここで与えられた提案に従って次の方法を試しました、そしてそれは完全に機能します。他の誰かが同じ問題に直面している場合は、これを試してください-

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xhtml="http://www.w3.org/1999/xhtml"
   xmlns="http://www.w3.org/1999/xhtml"
   exclude-result-prefixes="xhtml">
<xsl:output method="html" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*" />
   <xsl:template match="@*|node()">
     <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>

    <xsl:template match="xhtml:div[@id='id8']" xmlns="http://www.w3.org/1999/xhtml">
    <div id="id10" class="QT">
        <xsl:apply-templates select="
          (@*[local-name()!='id']
             [local-name()!='class'])
          | node()"/>
      </div>
        <script type="text/javascript">
          <![CDATA[         
           var numQuestions = 4;             
           var rawScore = 0;             
           var actualScore = 0;            
           function getAnswer()            
           {           
           }             
           function calulate()            
           {           
           }           
           ]]>
        </script>
    </xsl:template>
<xsl:stylesheet>
于 2012-08-07T00:22:42.987 に答える